dongmeng0317 2014-11-29 18:29
浏览 21
已采纳

如果entry是数字,PHP仅删除行

This code is set up on a LAMP stack. I'm having trouble with the Delete page. The Delete function in PHP only works if the entry in the mysql table is a number. I need it to delete the row regardless of the entry. The email field is set up as "varchar(25)" in mysql. I don't define the variables in the code so I don't understand why it is limiting it to numbers.

Here's the page:

<!DOCTYPE HTML>
<html>
<head>

</head>
<body>
<div id="gradientbackground">

    <table class="beachpictures" align="center" border="1">
        <thead>

        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Website</th>
            <th>Gender</th>
            <th>Delete</th>
        </tr>
        </thead>
        <tbody>
    <?php

        require 'project_db.php';
        mysql_connect("$servername", "$username", "$password")or die("cannot connect");
        mysql_select_db("$database")or die("cannot select DB");

        //execute the SQL query and return records
        if (!$result = mysql_query("SELECT *  FROM $table"))
        echo 'mysql error: ' .mysql_error();

        //fetch tha data from the database
        while ($row = mysql_fetch_array($result)) {
    ?>

        <tr>
            <td><?php echo $row['Name']; ?></td>
            <td><?php echo $row['Email']; ?></td>
            <td><?php echo $row['Website']; ?></td>
            <td><?php echo $row['Gender']; ?></td>
            <td class="record-delete">
                <form action='delete.php?Email="<?php echo $row['Email']; ?>"' method="post">
                    <input type="hidden" name="Email" value="<?php echo $row['Email']; ?>">
                    <input type="submit" name="submit" value="Delete">
                </form>
            </td>
        </tr>
   <?php }
    ?>
        </tbody>
    </table>
<br>
</div>
</body>
</html>

And here's the delete.php File that performs the actual query:

<?php
    require 'project_db.php';
    mysql_connect("$servername", "$username", "$password")or die("cannot connect");
    mysql_select_db("$database")or die("cannot select DB");

//Define the query
$query = "DELETE FROM $table WHERE Email={$_POST['Email']} LIMIT 1";

//sends the query to delete the entry
mysql_query ($query);

if (mysql_affected_rows() > 0) {
//if it updated
?>
            <strong>Record Has Been Deleted</strong><br /><br />
<?php
 } else {
//if it failed
?>
            <strong>Deletion Failed</strong><br /><br />
<?php
}


?>
  • 写回答

1条回答 默认 最新

  • doushou3814 2014-11-29 18:35
    关注

    You need to add quotes: otherwise, mysql will expect $_POST['email'] to be a number.
    Plus, you should switch to PDO or MySQLi, as mysql_* functions are deprecated, and you're opened to SQL injection. So your code should be:

    <?php
        require 'project_db.php';
        $connect = mysqli_connect($servername,$username,$password,$database)or die("cannot connect");
    
    //Define the query
    $query = "DELETE FROM $table WHERE Email='".mysqli_real_escape_string($connect,$_POST['Email'])."' LIMIT 1";
    
    //sends the query to delete the entry
    $execute = mysqli_query ($connect,$query);
    
    if (mysqli_affected_rows($execute) > 0) {
    //if it updated
    ?>
                <strong>Record Has Been Deleted</strong><br /><br />
    <?php
     } else {
    //if it failed
    ?>
                <strong>Deletion Failed</strong><br /><br />
    <?php
    }
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?