-1

我试图通过从下拉菜单中选择新闻事件来从数据库中删除新闻事件。当用户选择新闻标题并按下提交按钮时,应从相关表中删除新闻项目。无论我尝试什么,我都无法让它正常工作。对不起,我是 PHP 新手。有人可以帮忙吗?这是下拉菜单:

    $sql="SELECT newstitle FROM $tbl_name";
    $result=mysql_query($sql);
    ?>

    <select name="select1">
    <?php
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    ?>
    <option value="<?php echo $row['newstitle'];?>"> <?php echo $row['newstitle'];?>         </option>

    <?php
    }
    ?>
    </select>


    <td><center><input type="submit" name="delete" value="delete"></center></td>

按下按钮时,我还有要从数据库中删除的代码吗?

    $delete=$_POST['delete'];
    $delete = mysql_real_escape_string($delete);

    mysql_query ("DELETE FROM $tbl_name WHERE newstitle='$delete'") or die ("Error- news has not been deleted");
        echo "News has been deleted";
        header("Location: newsdeleted.php");
4

2 回答 2

0
// Your check for the "delete" submit value goes here
// Submit name and value are both 'delete'
if (isset($_POST['delete']) && $_POST['delete'] == 'delete') {
  // This is the value passed from the form (use select element's name)
  $selected_newstitle = $_POST['select1'];
  ...
}
于 2012-05-02T16:28:33.293 回答
0
  • $tbl_name未在您的删除脚本上设置。
  • 请阅读有关 SQL 注入、过滤输入、转义查询的信息
  • 你应该使用PHP PDO而不是旧的 mysql funcs
  • 除非您正在缓冲,否则您的标题行将不起作用,因为您已经输出到标准输出。
  • 您的选择输入被称为select1delete
于 2012-05-02T15:53:49.993 回答