从所选菜单中删除数据库中的数据

时间:2012-12-17 21:13:48

标签: php mysql sql-delete

我想在PHP代码中选择一个数据,并从mysql数据库中显示所选数据,然后删除所选数据!这是我的一般想法。

我写的代码,第一个代码是从数据库打印数据,以允许用户选择数据,这是第一个代码:

<form method="POST" action="deleteacc.php" name='monitor' id='monitor'>
<select name="drivers"><option>Please Select the sensor id that you want to delete :</option>  
<?php

$uid=$_SESSION['uid'];

  $dbName = "senior"; //Database name
        $db = mysql_connect ("localhost","root","hahaha1") or die("Unable To Connect "); //Connect to database
        $test= mysql_select_db ($dbName,$db) or die("Unable to select database"); //Test Connection

        $result=mysql_query("SELECT * FROM accelerometer ");


        while ($row=mysql_fetch_array($result))
        {
            $sid = $row['sid'];
            $x=$row['x'];
            $y=$row['y'];
            $z=$row['z'];
            $date=$row['date'];
            $time=$row['time'];
            echo "<option  name=$sid' value='$sid'>  Device ID: $sid</option>"; 
        }           

        mysql_close($db);
        ?>
</select></center>
<br>
 <center> <input type="submit" value= "   Monitor   " name="submit" ><br /><br /> </center>
</form>

删除所选数据的第二个代码:

<?php
$dbh=mysql_connect("localhost", "root", "hahaha1") or die (mysql_error());
mysql_select_db ("senior");
extract($_POST);
$mySql="DELETE FROM accelerometer WHERE sid=$drivers"; 
mysql_query($mySql) or die (mysql_error()); 
echo "<h2> User info was deleted successfully</h2>";
echo "<h3>To go back to adminstration control click <a href='member.php'>Here!</a> or to delete another accelerometer value click <a href='deleteacc2.php'>Here!</a></h3>";
mysql_close($dbh);
?>

我的问题是当我从菜单中选择数据并按下监视器时,此错误显示给我:

Unknown column 'bedoor' in 'where clause'

请问如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

此:

echo "<option  name=$sid' value='$sid'>  Device ID: $sid</option>";

应该是:

echo "<option  value=\"".$sid."\">  Device ID: ".$sid."</option>";

你真的不应该使用mysql ext,因为它已被折旧。查看mysqli或PDO。

以下是如何在通话页面中设置您的帖子变量:

    <?php
$dbh=mysql_connect("localhost", "root", "hahaha1") or die (mysql_error());
mysql_select_db ("senior");
if(isset($_POST['drivers']))
    {
        $drivers = $_POST['drivers'];
    }
$mySql="DELETE FROM accelerometer WHERE sid = '".$drivers."'"; 
mysql_query($mySql) or die (mysql_error()); 
echo "<h2> User info was deleted successfully</h2>";
echo "<h3>To go back to adminstration control click <a href='member.php'>Here!</a> or to delete another accelerometer value click <a href='deleteacc2.php'>Here!</a></h3>";
mysql_close($dbh);
?>