未将PHP结果行设置为对象
我有一段PHP代码,其中我试图编辑数据库中的一行未将PHP结果行设置为对象,php,mysql,mysqli,Php,Mysql,Mysqli,我有一段PHP代码,其中我试图编辑数据库中的一行 $sql="SELECT * FROM `event` where `EId`='".$_GET['EId']."'"; $res=$conn->query($sql); $numrows=mysqli_num_rows($res); if ($numrows>0) { $obj = mysqli_fetch_object($res); } if ($_REQUEST["mode"]=="save") { if ($_FI
$sql="SELECT * FROM `event` where `EId`='".$_GET['EId']."'";
$res=$conn->query($sql);
$numrows=mysqli_num_rows($res);
if ($numrows>0)
{
$obj = mysqli_fetch_object($res);
}
if ($_REQUEST["mode"]=="save")
{
if ($_FILES['image']['name']!="")
{
del_img("event/",$obj->Picture);
$Picture=post_img($_FILES['image']['name'], $_FILES['image']['tmp_name'],"event");
}
else
$Picture = $obj->Picture;
$sqlu="update event set Picture='".$Picture."' where EId='".$_POST['EId']."'";
$conn->query($sqlu);
header("refresh:1; url=event_view.php");
die();
}
function post_img($fileName,$tempFile,$targetFolder)
{
if ($fileName!="")
{
if(!(is_dir($targetFolder)))
mkdir($targetFolder);
$counter=0;
$NewFileName=$fileName;
if(file_exists($targetFolder."/".$NewFileName))
{
do
{
$counter=$counter+1;
$NewFileName=$counter."".$fileName;
}
while(file_exists($targetFolder."/".$NewFileName));
}
$NewFileName=str_replace(",","-",$NewFileName);
$NewFileName=str_replace(" ","_",$NewFileName);
copy($tempFile, $targetFolder."/".$NewFileName);
return $NewFileName;
}
}
function del_img($targetfolder,$filname)
{
if (file_exists($targetfolder.$filname))
{
unlink($targetfolder.$filname);
}
}
如果执行此操作而不上载新图像,则会删除当前图像并保存不带任何图像的行。上载新图像时,不会删除当前图像
我用isset检查过,它告诉我变量$obj->Picture没有设置。我在旧版本的PHP中使用了这段代码,它仍然可以工作,但在当前版本中,我似乎无法让它工作。
我很确定问题出在$obj上,但我似乎不知道它是什么
HTML只是一个带有文件上传输入的表单,我已经建立了到数据库的连接,$conn是一个新的mysqli。我选择整行的原因是因为我也在编辑其他东西
感觉我犯了一个根本性的错误?我遗漏了什么?我敢打赌num\u rows\u函数存在一些问题 尝试以不同的方式构造代码,或者至少确保在到达所需对象的代码部分时定义并初始化了obj 例如,执行以下操作:
if ($_REQUEST["mode"]=="save" && isset($obj))
{
if (($_FILES['image']['name']!=""))
{
del_img("event/",$obj->Picture);
$Picture=post_img($_FILES['image']['name'], $_FILES['image']['tmp_name'],"event");
}
else
$Picture = $obj->Picture;
$sqlu="update event set Picture='".$Picture."' where EId='".$_POST['EId']."'";
(...)
好吧,这是我该怎么解决的。你的整个逻辑都搞砸了;现在我们只需要两个条件:是否发送了有效的EId,是否附加了文件 数据库API被更新到更现代的版本,查询被准备好并参数化以确保安全性,我们在使用用户输入命名文件之前,正在正确地清理用户输入
<?php
$conn = new PDO("mysql:host=localhost;dbname=database", "user", "password");
$stmt = $conn->prepare("SELECT Picture FROM event WHERE EId = ?");
$result = $stmt->execute([$_POST["EId"]]);
if ($obj = $stmt->fetch(\PDO::FETCH_OBJ)) {
if (!empty($_FILES["image"])) {
del_img("event/", $obj->Picture);
$Picture = post_img($_FILES['image'], "event");
$stmt = $conn->prepare("UPDATE event SET Picture = ? WHERE EId = ?");
$result = $stmt->execute([$Picture, $_POST["EId"]]);
}
header("Location: event_view.php");
die();
}
function post_img($file, $targetFolder)
{
if (!(is_dir($targetFolder))) {
mkdir($targetFolder);
}
$fileName = $file["name"];
$tempFile = $file["tmp_name"];
$NewFileName = str_replace([",", " "], ["-", "_"], basename($fileName));
$counter = 0;
while(file_exists($targetFolder . "/" . $NewFileName)) {
$counter += 1;
$NewFileName = $counter . $fileName;
}
move_uploaded_file($tempFile, $targetFolder . "/" . $NewFileName);
return $NewFileName;
}
function del_img($targetfolder,$filname)
{
if (file_exists($targetfolder . $filname)) {
unlink($targetfolder.$filname);
}
}
if($\u REQUEST[“mode”]==“save”)
块中的所有代码都应该在if($numrows>0)
块中,因为它取决于设置的$obj
,而$obj
仅在该if块中设置。但这会破坏功能。它是如何到达($_请求[“模式”]=“保存”)部分的?我在旧版本的PHP中使用了完全相同的代码,它运行良好。然后,您需要重新构造代码,使其不依赖于设置的$obj
,除非您正在更新现有行。但它适用于旧版本。为什么更新版本会变得更加困难?这是非常危险的代码。您不仅将用户提供的数据直接放入数据库查询中,还将其放入文件系统中。在开发过程中,我建议查看numrows的输出—我想这将有助于说明它有时不起作用的原因:)问题是,我知道它没有设置。所以这永远不会运行。我想知道为什么它在旧版本中可以工作,但在这个版本中无法工作。我尝试的另一件事是删除if($numrows>0)条件,并将$obj=mysqli\u fetch\u对象($res)保留为不带if语句。现在var已经设置好了,但是如果您打印r($numrows);-给定的输出是什么?是否执行$conn->query();提供正确的mysqli查询结果?不知道脚本有多旧,但在将旧mysql函数重构为mysqli函数的过程中,有时会出现错误,因为某些mysqli函数的参数集与以前的mysql函数不同,甚至它们的输出有时也不同。这是一个相当不典型错误的来源。事实上,我今天对php开发还不是很深入,但这将是过去发生的典型情况;是一个,我非常确定$conn->query();提供一个正确的查询,因为我在其他地方使用过它