dongmi4035 2016-11-14 16:02
浏览 54
已采纳

我想使用PhP删除组合框中的选定项目,并且还必须反映在数据库中

This is the php code to delete.

<?php
$namez = $_POST['vendel'];
$name = mysql_real_escape_string($namez);
mysql_connect("localhost","root",
"") or die("Wrong username or password");
mysql_select_db("vendor") or die( "Unable to select database");
mysql_query("DELETE FROM vendoradd WHERE venname='$name'");
?>

And here is the code where I select from a combobox.

<form action="delete.php" method="post">
                     <select name="vendel">
            <?php
                $con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
                $query = mysqli_query($con,"select venname from vendoradd");
                while ($row=$query->fetch_assoc()){
                    echo "<option value=\"vendel1\">".$row['venname']."</option>";
                }
            ?>
            </select>

                <input type="submit" value="Delete" id="del">
            </form>

I am not getting any error, the page is redirecting to delete.php file but when I cross check the database the selected data is not deleted. Any help is appreciated. Thanks in advance

  • 写回答

1条回答 默认 最新

  • doukai1226 2016-11-14 16:39
    关注

    Well... Its because you're posting a value "vendel1", which means that you're always sending the same value, just for you to know... The name of the select combobox is the index in the $_POST and the value in the option is the value, for example, if you have:

    <form action="" method="post">
    <select name="foo">
        <option value="1">Stack</option>
        <option value="2">Overflow</option>
    </select>
    <input type="submit">
    </form>
    <?php
         echo $_POST['foo'];
    ?>  
    

    And you select the option "Stack" the output of

    $_POST['foo']

    will be 1.

    So if you re going to post with a select combobox, you'll have to give an unique value to each option, so you can delete the correct one in your database. My recommendation for you is to create an unique ID to each user in your table, its better to compare unique ID's than strings... Why? Because if you have two people with the same name, you'll delete both, but, with an unique ID you'll not have that problem. If you want to delete

    <form action="delete.php" method="post">
     <select name="vendel">
            <?php
                $con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
                $query = mysqli_query($con,"select TRIM(venname) from vendoradd");
                while ($row=$query->fetch_assoc()){
                    echo "<option value=".$row['venname'].">".$row['venname']."</option>";
                }
            ?>
     </select>
     <input type="submit" value="Delete" id="del"></form>
    

    And your delete should be like this:

       <?php
    
          $namez = $_POST['vendel'];
          $name = mysql_real_escape_string($namez);
          mysql_connect("localhost","root", "") or die("Wrong username or password");
          mysql_select_db("vendor") or die( "Unable to select database");
          mysql_query("DELETE FROM vendoradd WHERE TRIM(venname) = TRIM('$name')");
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP