php DELETE FROM table WHERE ID='$id' — 变量拒绝坚持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1569610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DELETE FROM table WHERE ID='$id' — Variable refuses to stick
提问by chris m
Trying to perform a very simple task here.
试图在这里执行一个非常简单的任务。
I have an <ol>that contains 4 rows of data in some handy <li>s. 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.
我有一个<ol>在一些方便的<li>s中包含 4 行数据的数据。我想添加一个删除按钮以从表中删除该行。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:
继续delete.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 表是:savannah
Ideas?
想法?
回答by cletus
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>
<?
回答by Paul Tarjan
回答by pablo
Just another point I'd like to make. I'm 95% sure that you can't give an input a numeric name/id attribute. It has to be like "id_1" not "1".
还有一点我想说。我 95% 确定您不能为输入提供数字名称/ID 属性。它必须像“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

