PHP在SQL中删除按钮 [英] php sql delete button
问题描述
我试图从SQL进行删除按钮。我开始了一个名为$德尔功能,但我不知道如何完成它,在echoe'd出当前echo语句旁边的删除按钮的形式。
$ CON = mysql_connect(localhost的用户,合格);
如果(!$ CON){
死亡(无法连接:'mysql_error());
}mysql_select_db(数据库,$ CON);
$ SQL =INSERT INTO营(市,地图,公园,天,细节)
($ _ POST [城市]','$ _ POST [图]','$ _ POST [乐园]','$ _ POST [日],$ _ POST [详情]')价值观;如果(!的mysql_query($的SQL,$ CON)){
死('错误:mysql_error());
}$编号= 0;
$德尔=的mysql_query(DELETE FROM营地,USER_ID ='$号);$结果= mysql_query(SELECT * FROM营);而($行= mysql_fetch_array($结果)){
回声< A HREF = \\'。$行['地图'。\\>中。 $行[城市。 &所述; / A>中;
回声< BR />中;
}
您将需要使你的链接传递参数给一个脚本,将删除该记录。
您的链接会看起来像这样
回声< A HREF = \\'delete.php ID =$ user_ID的。\\?>中。 $行[城市。 &所述; / A>中;
那么你删除可以只抓住从$ _GET gloabal的参数,可以和它们传递到SQL像这样
$德尔=的mysql_query(DELETE FROM营地,USER_ID =$ _GET ['USER_ID']);
这当前的查询将删除该用户的所有营地(调整PARAMS / sql作为needbe)。
但是,你不应该传递用户瓦尔到您的SQL字符串。你必须让自己开放 SQL注入攻击。我会建议使用 PDO 来逃避你的SQL。我也建议使用POST方法对于任何破坏性的DB操作,这样你就不会意外地改变一些东西。
I'm trying to make a delete button from sql. I started a function called $del but I don't know how to complete it, in the form of a delete button echoe'd out beside the current echo statements.
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$sql = "INSERT INTO camps (city, map, park, day, details)
VALUES ('$_POST[city]','$_POST[map]','$_POST[park]','$_POST[day]','$_POST[details]')";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
}
$number = 0;
$del = mysql_query("DELETE FROM camps WHERE user_id= '$number'");
$result = mysql_query("SELECT * FROM camps");
while ($row = mysql_fetch_array($result)) {
echo "<a href=\"'" . $row['map'] . "\">" . $row['city'] . "</a>";
echo "<br />";
}
You will need to make your links pass a param to a script that will delete that record. Your links would looks something like this
echo "<a href=\"'delete.php?id=" . $user_id"\">" . $row['city'] . "</a>";
Then your delete can just grab the params from the $_GET gloabal, and pass them into your sql like so
$del = mysql_query("DELETE FROM camps WHERE user_id=" . $_GET['user_id']);
This current query will delete all camps for that user (adjust params / sql as needbe).
However, you should NEVER pass user vars into your sql strings. You leave yourself open for sql injection attacks. I would recommend using PDO to escape your sql. I would also recommend using the post method for any destructive db operation so that you don't accidentally alter something.
这篇关于PHP在SQL中删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!