我仍在学习 PHP 和 MySQLI,并想知道是否可以在单个 php 页面上托管多个 MySQLI 删除查询,并让 url 对相应的查询进行操作而不会造成混淆。我在这里有这个代码,我已经在网上搜索了 3 天的答案,但我发现的一切似乎都不是我想要的。任何基于我的代码的帮助或正确描述的示例将不胜感激。
我的代码是:
<?php
session_start();
include ('dbconnect.php');
$userr = $_SESSION['usr_id'];
$uid=$_GET['pid'];
$pid=$_GET['phid'];
$user_id=$_GET['user'];
$fileID=$_GET['file'];
if($userr==$uid && $pid==$fileID){
echo "ERROR No. 9B2AP";
}else{
mysqli_query($con,"delete from images where userID='$uid' AND PhotoID='$pid'") or die (mysqli_error());
header ("location: /viewImage.php?pid=$uid");
}
if($userr==$user_id && $fileID==$pid){
echo "ERROR No. 39V41";
}else{
mysqli_query($con,"delete from music where userID='$user_id' AND Record_ID='$fileID'") or die (mysqli_error());
header ("location: /users/music-gallery/$user_id");
}
?>
无论我重写此代码多少次,当我使用此页面上的代码删除图像或歌曲时,它只会将我重定向到/users/music-gallery/
而不是正确的关联页面。我怎样才能解决这个问题?就像我说的,我是 PHP 和 MySQLI 的新手,我认为应该详细描述任何建议,以便我能够理解和理解我是如何犯错误的,并且我可以修复并防止它在以后的代码中再次发生。谢谢,麻烦您了。-小姐
出于安全原因,不要这样做:
// Consider escaping the incoming data for better security
$uid=$_GET['pid'];
$pid=$_GET['phid'];
// ...
由于您正在使用,MySQLi
您可以使用它来转义您的数据:
$uid = mysqli_real_escape_string($con, $_GET['pid']);
您可以使用FILTERS
检查输入数据类型:
// If you are expecting an `INT` from $_GET['pid']
if (filter_var($_GET['pid'], FILTER_VALIDATE_INT))
{
echo 'pid is an int';
}
else
{
echo 'pid is not an int';
}
更多关于这里的过滤器。
最好的是,使用prepared mysqli statements
有stmt
:
// prepare the statement
$stmt = $con->prepare("delete from images where userID=? AND PhotoID=?");
// bind variables to the '?' and set types --> `i` = integer
$stmt->bind_param("ii", $_GET['pid'], $_GET['phid']);
// execute query
$stmt->execute();
// Do the same for the next query
更多关于准备好的声明在这里。
要解决您的问题:
对于exit
标题后的程序,您需要exit();
在每个标题之后使用,header
如下所示:
header ("location: /viewImage.php?pid=$uid");
exit();
例如:
header ("location: /viewImage.php?pid=$uid");
// this line of code gets exucuted
// this too
// ...
header ("location: /viewImage.php?pid=$uid");
exit();
// Nothing gets executed as program terminates and redirects
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句