SELECT 和 UPDATE 不起作用——MySQL

发布于 2024-09-10 04:14:30 字数 727 浏览 2 评论 0原文

将照片上传到服务器后,我想将其保存在 MySQL 中的用户数据库中,但由于某种原因,它不起作用。下面是 uploader.php: 的代码:

session_start();
if(!$_SESSION['userid']) {
  header("Location: index.php"); 
  exit;
}


$con = mysql_connect("host","db","pw");
if (!$con)
  {
  die('Could not connect: ' .mysql_error());
  }

mysql_select_db("db", $con);

$sess_userid = mysql_real_escape_string($_SESSION['userid']);
$query  = "SELECT * FROM Members  WHERE fldID='$sess_userid' UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]'");  
$result = mysql_query($query) or trigger_error(mysql_error().$query);
$row = mysql_fetch_assoc($result);

我确定我的查询有问题,但我不知道它是什么。照片肯定保存到文件夹中。但我只是想更新它在用户数据库中的路径以供以后使用。谢谢你!

After I upload a photo to a server, I want to save it in the user's database in MySQL, but for some reason, it is not working. Below is the code for uploader.php:

session_start();
if(!$_SESSION['userid']) {
  header("Location: index.php"); 
  exit;
}


$con = mysql_connect("host","db","pw");
if (!$con)
  {
  die('Could not connect: ' .mysql_error());
  }

mysql_select_db("db", $con);

$sess_userid = mysql_real_escape_string($_SESSION['userid']);
$query  = "SELECT * FROM Members  WHERE fldID='$sess_userid' UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]'");  
$result = mysql_query($query) or trigger_error(mysql_error().$query);
$row = mysql_fetch_assoc($result);

I'm sure there is something very wrong with my query, but I can't figure out what it is. The photo is definitely being saved into the folder. But I simply want to update its path in the user database for later use. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱&纷飞 2024-09-17 04:14:30

正如已经提到的,您不能同时使用这两个查询。
但也有奇怪的语法:你试图在 mysql 查询中使用 PHP 的串联运算符。
而且你没有转义字符串参数 - 非常糟糕!
所以,看起来你需要类似的东西

$sess_userid = mysql_real_escape_string($_SESSION['userid']);
$PortraitPath = mysql_real_escape_string('profileportraits/' . $_FILES['file']['name']);
$query  = "UPDATE Members SET PortraitPath = '$PortraitPath' WHERE fldID='$sess_userid'";  

as it was mentioned already, you cannot use these two queries at once.
but there is also weird syntax: you're trying to use PHP's concatenation operator inside of mysql query.
And you did not escape a string parameter - very bad!
So, looks like you need something like

$sess_userid = mysql_real_escape_string($_SESSION['userid']);
$PortraitPath = mysql_real_escape_string('profileportraits/' . $_FILES['file']['name']);
$query  = "UPDATE Members SET PortraitPath = '$PortraitPath' WHERE fldID='$sess_userid'";  
姜生凉生 2024-09-17 04:14:30

您可以执行两个单独的查询:

UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]'
 WHERE fldID='$sess_userid';

并且:

SELECT * FROM Members  WHERE fldID='$sess_userid'

You can do two separate queries:

UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]'
 WHERE fldID='$sess_userid';

And:

SELECT * FROM Members  WHERE fldID='$sess_userid'
清旖 2024-09-17 04:14:30

看来您试图将两个查询(SELECT 和 UPDATE)放入一个查询中,这将导致无效查询错误。

只是想知道为什么你需要两个查询,因为你已经知道用户 ID 并且你想要的只是更新。您所需要做的就是更新文件路径

UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]' WHERE fldID='$sess_userid';

It seems you tried to put two queries(SELECT and UPDATE) into one query which will result in invalid query error.

Just wondering why you need two queries since you already know the userid and all you want is to update. All you need is to update the file path

UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]' WHERE fldID='$sess_userid';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文