简体   繁体   English

从表WHERE ID ='$ id'中删除-变量拒绝粘贴

[英]DELETE FROM table WHERE ID='$id' — Variable refuses to stick

Trying to perform a very simple task here. 在这里尝试执行一个非常简单的任务。

I have an <ol> that contains 4 rows of data in some handy <li> s. 我有一个<ol> ,其中包含一些方便使用的<li> s中的4行数据。 I want to add a delete button to remove the row from the table. 我想添加一个删除按钮以从表中删除行。 The script in delete.php appears to have finished, but the row is never removed when I go back and check dashboard.php and PHPMyAdmin for the listing. delete.php中的脚本似乎已经完成,但是当我返回并检查dashboard.php和PHPMyAdmin中的列表时,该行从未删除。

Here's the code for the delete button (inside PHP): 这是删除按钮的代码(在PHP内部):

Print "<form action=delete.php method=POST><input name=".$info['ID']." type=hidden><input type=submit name=submit value=Remove></form>";

Moving on to delete.php: 继续删除.php:

<? 
//initilize PHP

if($_POST['submit']) //If submit is hit
{
   //then connect as user
   //change user and password to your mySQL name and password
   mysql_connect("mysql.***.com","***","***") or die(mysql_error()); 

   //select which database you want to edit
   mysql_select_db("shpdb") or die(mysql_error()); 

   //convert all the posts to variables:
   $id = $_POST['ID'];


   $result=mysql_query("DELETE FROM savannah WHERE ID='$id'") or die(mysql_error()); 

    //confirm
   echo "Patient removed. <a href=dashboard.php>Return to Dashboard</a>"; 
}
?>

Database is: shpdb Table is: savannah 数据库是:shpdb表是:萨凡纳

Ideas? 有想法吗?

It's refusing to stick because you're calling it one thing and getting it with another. 它拒绝坚持,因为您将其称为一件事,而将其与另一件事联系在一起。 Change: 更改:

"<input name=".$info['ID']." type=hidden>"

to

"<input name=ID value=".$info['ID']." type=hidden>"

because in delete.php you're trying to access it with: 因为在delete.php中,您尝试通过以下方式访问它:

$id = $_POST['ID'];

You should really quote attribute values as well ie: 您还应该真正引用属性值,即:

print <<<END
form action="delete.php" method="post">
<input type="hidden" name="ID" value="$info[ID]">
<input type="submit" name="submit" value="Remove">
</form>
END;

or even: 甚至:

?>
form action="delete.php" method="post">
<input type="hidden" name="ID" value="<?php echo $info['ID'] ?>">
<input type="submit" name="submit" value="Remove">
</form>
<?

Please, for the love of the web, don't built an SQL query yourself. 请,出于对网络的热爱,不要自己构建SQL查询。 Use PDO . 使用PDO

Just another point I'd like to make. 我想提出的另一点是。 I'm 95% sure that you can't give an input a numeric name/id attribute. 我95%的确定您不能为输入提供数字名称/ id属性。 It has to be like "id_1" not "1". 它必须像“ id_1”而不是“ 1”。

Also with php you can do arrays. 也可以用php做数组。

So you could do this 所以你可以这样做

<input name="delete[2]">

then in your php 然后在你的PHP

if(isset($_POST['delete']))
  foreach($_POST['delete'] as $key=>$val)
    if($_POST['delete'][$key])  delete from table where id = $val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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