简体   繁体   中英

List & Delete records in mysql using php error

I have written a PHP code. I am not able to retrieve the results from database and delete as well. On Submit it just gives a blank page without throwing any error. I am new to this so please, reply even if u think its a silly question. Refer to the code and suggest me some changes which will make my code work.

  <!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

在此处输入图片说明

Before querying the database you have to make a connection first. Follow the following code snippet example to first create a connection with mysql and then query the database.

<?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);
?>

try this code, Don't use id directly in mysql query, before using your id directly to mysql query pass this from mysql_real_escape_string , it will clear id from sql injection. see SQL injection, 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>";
}

NOTE: When your code work then remove this "or die(mysql_error())" with ";" it is only used in production mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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