【发布时间】:2018-09-13 21:11:03
【问题描述】:
当我尝试将图像文件上传到我的数据库以进行发布时收到错误消息。图像出现在我的文件夹中,但不会将其添加到 mysql 数据库中。 我还是一个初学者,不要评判......我找不到错误...... 错误: 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的“WHERE username = 'myusername'' 附近使用正确的语法
代码如下:
$Destination = 'userfiles/posts';
if(!isset($_FILES['attachment_image']) || !is_uploaded_file($_FILES['attachment_image']['tmp_name'])){
$attachnewimg= 'default-background.jpg';
move_uploaded_file($_FILES['attachment_image']['tmp_name'], "$Destination/$attachnewimg");
}
else{
$RandomNum = rand(0, mt_getrandmax());
$ImageName = str_replace(' ','-',strtolower($_FILES['attachment_image']['name']));
$ImageType = $_FILES['attachment_image']['type'];
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$attachnewimg = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
move_uploaded_file($_FILES['attachment_image']['tmp_name'], "$Destination/$attachnewimg");
}
$sql1="UPDATE users SET attachment_image='$attachnewimg' WHERE username = '$temp'";
$sql2="INSERT INTO users (attachment_image) VALUES ('$attachnewimg') WHERE username = '$temp'";
$result = mysqli_query($mysqli,"SELECT * FROM users WHERE username = '$temp'");
if( mysqli_num_rows($result) > 0) {
if(!empty($_FILES['attachment_image']['name'])){
mysqli_query($mysqli,$sql1)or die(mysqli_error($mysqli));
header("location:/");
}
}
else {
mysqli_query($mysqli,$sql2)or die(mysqli_error($mysqli));
header("location:/");
}
【问题讨论】:
-
INSERT 语句没有 WHERE 子句(除非 INSERT SELECT)。
标签: php mysqli upload localhost mariadb