Php 从";中获取行值;id";柱
在下面的代码中,我希望获得列“id”的行结果作为$selectedmovieid的结果。“CommonId”是主键属性。很抱歉,不知道如何正确使用mysql\u fetch\u assocPhp 从";中获取行值;id";柱,php,Php,在下面的代码中,我希望获得列“id”的行结果作为$selectedmovieid的结果。“CommonId”是主键属性。很抱歉,不知道如何正确使用mysql\u fetch\u assoc <?php require ("connect-comment.php"); $deleteid=$_GET['commentid']; $query1=mysql_query("SELECT id FROM comment WHERE commentid='$deleteid'"); $selecte
<?php
require ("connect-comment.php");
$deleteid=$_GET['commentid'];
$query1=mysql_query("SELECT id FROM comment WHERE commentid='$deleteid'");
$selectedmovieid= mysql_fetch_assoc($query1);
$query2=mysql_query("DELETE FROM comment WHERE commentid='$deleteid'");
header("Location: reload.php?id=$selectedmovieid");
?>
尝试更改
$deleteid=$_GET['commentid'];
// ...
header("Location: reload.php?id=$selectedmovieid");
到
此外,正如佩卡正确地建议的那样,试着阅读和
作为旁注,您应该不要为位置:
标题重定向使用相对路径。RFC指定此字段应包含完整URL,虽然许多浏览器将正确解释相对路径,但不应依赖此行为。换句话说,Location:reload.php?id=…
应该是Location:http://mysite.tld/reload.php?id=
编辑尝试以下示例代码的完整版本:
<?php
require ("connect-comment.php");
$deleteid = mysql_real_escape_string($_GET['commentid']);
// Added LIMIT 1 to the query, because you are only using one result
if (!$query1 = mysql_query("SELECT `id` FROM `comment` WHERE `commentid` = '$deleteid' LIMIT 1")) {
// Do NOT show the output of mysql_error() to the user in a production environment!
exit("Something went wrong with query 1: ".mysql_error());
} else if (mysql_num_rows($query1) < 1) {
exit("No results from query 1");
}
$selectedmovieid = mysql_fetch_assoc($query1);
$selectedmovieid = $selectedmovieid['id']; // $selectedmovieid now contains the id you want
if (!$query2 = mysql_query("DELETE FROM `comment` WHERE `commentid` = '$deleteid'")) {
// Do NOT show the output of mysql_error() to the user in a production environment!
exit("Something went wrong with query 2: ".mysql_error());
}
// If we get this far, everything should be fine
// You still need a full URL here though, not a relative path...
header("Location: reload.php?id=$selectedmovieid");
?>
如果您键入http://php.net/
然后是您需要了解的函数名称,您将进入PHP手册中的页面。手册几乎总是提供一个例子。哎哟这么多未初始化的用户输入…您永远不应该将从用户获取的内容(尤其是GET参数)插入到SQL语句中。了解mysql\u fetch\u assoc()。只需在文档中查找即可。要详细说明Till的含义:+1@TillHelgeHelwig,尤其是在DELETE
语句中-如果我请求http://yourdomain.tld/yourpage.php?commentid=1“+或+'0'+=+'0
?
$deleteid = mysql_real_escape_string($_GET['commentid']);
// ...
header("Location: reload.php?id={$selectedmovieid['id']}");
<?php
require ("connect-comment.php");
$deleteid = mysql_real_escape_string($_GET['commentid']);
// Added LIMIT 1 to the query, because you are only using one result
if (!$query1 = mysql_query("SELECT `id` FROM `comment` WHERE `commentid` = '$deleteid' LIMIT 1")) {
// Do NOT show the output of mysql_error() to the user in a production environment!
exit("Something went wrong with query 1: ".mysql_error());
} else if (mysql_num_rows($query1) < 1) {
exit("No results from query 1");
}
$selectedmovieid = mysql_fetch_assoc($query1);
$selectedmovieid = $selectedmovieid['id']; // $selectedmovieid now contains the id you want
if (!$query2 = mysql_query("DELETE FROM `comment` WHERE `commentid` = '$deleteid'")) {
// Do NOT show the output of mysql_error() to the user in a production environment!
exit("Something went wrong with query 2: ".mysql_error());
}
// If we get this far, everything should be fine
// You still need a full URL here though, not a relative path...
header("Location: reload.php?id=$selectedmovieid");
?>