繁体   English   中英

使用PHP错误列出和删除MySQL中的记录

[英]List & Delete records in mysql using php error

我已经写了一个PHP代码。 我无法从数据库中检索结果,也无法删除。 在提交时,它只给出一个空白页而不会引发任何错误。 我对此并不陌生,所以即使您认为这是一个愚蠢的问题,也请回复。 请参考代码,并向我提出一些更改,这些更改将使我的代码正常工作。

  <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <title>EDIT SCREEN</title> <form action="test4.php" method="post"> <ul> <li> Employee ID:</br> <input type="text" name="eid"> </li> <li> <input type="submit" value="SUBMIT"> </li> </ul> </form> </body> </html> //test.php <?php define('DB_NAME', 'test'); define('DB_USER', '**'); define("DB_PASSWORD", '**'); define('DB_HOST', 'localhost'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); $db_selected = mysql_select_db(DB_NAME, $link); if(isset($_POST['ok'])){ $value1 = $_POST['eid']; $res = mysql_query("SELECT * from 'add' WHERE empid = '".$value1."'"); echo "<table border='1'> <tr><th>Name</th> <th>EmployeeID</th><th>Address</th></tr>"; while($row = mysql_fetch_array($res)) { echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['empid'] . "</td>"; echo "<td>" . $row['desig'] . "</td>"; echo "<td><a href='test5.php?del=$row[empid]'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; } if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } mysql_close(); ?> //test5.php <?php define('DB_NAME', 'test'); define('DB_USER', ''); define("DB_PASSWORD", ''); define('DB_HOST', 'localhost'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); $db_selected = mysql_select_db(DB_NAME, $link); $value1 = $_POST['del']; mysql_query("DELETE FROM add WHERE empid = '$value1'") ?> 

<?php
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];

$res = mysql_query("SELECT * from `add` WHERE empid = '".$value1."'");
echo "<table border='1'>
      <tr><th>Name</th>
      <th>EmployeeID</th><th>Address</th></tr>";

         while($row = mysql_fetch_array($res))
         {
            echo "<tr>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['empid'] . "</td>";
            echo "<td>" . $row['add'] . "</td>";
            echo "</tr>";
         }
         echo "</table>";
      }   
?>




New Code

<!DOCTYPE html>
    <html>

    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>

    <body>

        <title>EDIT SCREEN</title>


        <form action="result.php" method="post">
            <ul>

                <li>
                    Employee ID:</br>
                    <input type="text" name="eid">
                </li>

                <li>
                    <input type="submit" value="SUBMIT" name="ok">
                </li>
            </ul>

        </form>

    </body>

    </html>



                <?php
                mysql_connect("localhost","root","");
                mysql_select_db("fdd");
                if(isset($_POST['ok'])){
                $value1 = $_POST['eid'];

                $res = mysql_query("SELECT * from `jobs` WHERE id = '".$value1."'");
                echo "<table border='1'>
                      <tr><th>Name</th>
                      <th>EmployeeID</th><th>Address</th></tr>";

                         while($row = mysql_fetch_array($res))
                         {
                            echo "<tr>";
                            echo "<td>" . $row['job_date'] . "</td>";
                            echo "<td>" . $row['client_code'] . "</td>";
                            echo "<td>" . $row['department'] . "</td>";
                            echo "</tr>";
                         }
                         echo "</table>";
                      }   
                ?>

     output

在此处输入图片说明

在查询数据库之前,您必须先建立连接。 按照下面的代码片段示例,首先创建与mysql的连接,然后查询数据库。

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Perform queries 
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) 
VALUES ('Glenn','Quagmire',33)");

mysqli_close($con);
?>

试试这个代码,不要直接在mysql查询中使用id,直接在mysql查询中使用id之前,请从mysql_real_escape_string传递它,它将从sql注入中清除id。 请参阅SQL注入REF

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <title>EDIT SCREEN</title>
        <form action="test4.php" method="post">
        <ul>
        <li>Employee ID:</br><input type="text" name="eid"></li>
        <li><input type="submit" value="SUBMIT"></li>
        </ul>
    </form>
</body>
</html>

********* test4.php file ***********

<?php

$value1 =   mysql_real_escape_string($_POST['eid']);
if($value1)
{
    $Connection     =   mysql_connect('localhost','root','') or die(mysql_error());
    $ConnectionDB   =   mysql_select_db($Connection) or die(mysql_error());
    $res = mysql_query("SELECT * from `add` WHERE empid = '".$value1."'") or die(mysql_error());
    echo "<table border='1'>
           <tr>
            <th>Name</th>
            <th>EmployeeID</th>
            <th>Address</th>
           </tr>";
         while($row = mysql_fetch_array($res))
         {
            echo "<tr>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['empid'] . "</td>";
            echo "<td>" . $row['add'] . "</td>";
            echo "</tr>";
         }
         echo "</table>";
}

注意:当您的代码工作时,请使用“;”删除此“或die(mysql_error()) ”。 它仅在生产模式下使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM