我遇到了PHP UPDATE
多图像和唯一的数据库ID的问题。在数据库中,我具有图像表和发布表,当我上传多张图像时,该图像将使用当前发布ID更新图像表中的当前图像。
错误详细信息
错误描述:您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在'('36512422_1678146545636436_8502224333249183744_n.jpg')WHERE post_id ='37''第1行附近使用
问题是UPDATE
语法是错误的,因为$insertValuesSQL
来自foreach
,使用.=
并置分配来更新图像。
有人可以帮助我纠正语法并能够使用帖子ID更新图片吗?
错误行:
$insert = $conn->query("UPDATE image SET file_name = $insertValuesSQL WHERE post_id = '".$post_id."'");
PHP代码:
<?php
require_once "conn.php";
session_start();
//$leavedate = date("Y-m-d", strtotime($_POST['leavedate']));
$startingPoint= $_POST["m_StartingPoint"];
$destination = $_POST["m_Destination"];
$date = $_POST["m_Date"];
$time = $_POST["m_Time"];
$type = $_POST["m_Type"];
$vehicle = $_POST["m_Car"];
$license = $_POST["m_license"];
$insurance = $_POST["m_Insurance"];
$userId = $_SESSION['userLogin_Id'];
$post_id = $_POST['post_id'];
$convertDate = date("Y-m-d", strtotime($date));
$convertTime = date('H:i:s', strtotime($time));
$registrationDetails = array();
//found here https://www.codexworld.com/upload-multiple-images-store-in database-php-mysql/
// File upload configuration
$sql = "UPDATE post SET starting_point = '".$startingPoint."', destination = '".$destination."',date ='".$convertDate."', time = '".$convertTime."', type ='".$type."', type_vehicle = '".$vehicle."',
type_license = '".$license."',insurance = '".$insurance."', user_id ='".$userId."'
WHERE post.id = '".$post_id."'";
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['submit-image']['name']))){
if($post_id!= null ){
$stmt = $conn->prepare($sql);
$stmt->execute();
$update_successful = true;
foreach($_FILES['submit-image']['name'] as $key=>$val){
// File upload path
$fileName = basename($_FILES['submit-image']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["submit-image"]["tmp_name"][$key], $targetFilePath)){
// Image db insert sql
$insertValuesSQL .= "('".$fileName."'),";
}else{
$errorUpload .= $_FILES['submit-image']['name'][$key].', ';
}
}else{
$errorUploadType .= $_FILES['submit-image']['name'][$key].', ';
}
}
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL,',');
// Insert image file name into database
$insert = $conn->query("UPDATE image SET file_name = $insertValuesSQL WHERE post_id = '".$post_id."'");
if($insert){
$errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
$statusMsg = "Files are uploaded successfully.".$errorMsg;
$registrationDetails['successful-addPost'] = true;
header('Content-Type: application/json');
echo json_encode($registrationDetails);
}else{
$statusMsg = "Error description: " . mysqli_error($conn) . $insertValuesSQL;
// $statusMsg = "Sorry, there was an error uploading your file.";
$registrationDetails['error-addPost'] = true;
header('Content-Type: application/json');
echo json_encode($registrationDetails);
}
}
}
else{
$update_successful = false;
}
}else{
// $statusMsg = 'Please select a file to upload.';
mysqli_query($conn, $sql);
mysqli_close($conn);
$registrationDetails['successful-addPost'] = true;
header('Content-Type: application/json');
echo json_encode($registrationDetails);
}
// Display status message
echo $statusMsg;
//if($sql = "INSERT INTO post (starting_point, destination ,date, time)VALUES ('$startingPoint', '$destination','$convertDate', '$convertTime')") {
// mysqli_query($conn, $sql);
// mysqli_close($conn);
//}
下面的图片是我的图片表:
我有两个图像,其ID为 37
引用来源:https://www.codexworld.com/upload-multiple-images-store-in-database-php-mysql/
上面的参考是关于INSERT
的,目前我需要的是UPDATE
。谢谢。
答案 0 :(得分:1)
如果要在image
表中每行获取一个图像文件名,则需要有多个查询才能插入值。由于您可能已经具有任何给定post_id
值的行,因此在将新文件名添加到表之前,必须删除这些行。因此,首先我们必须创建一个文件名列表。添加
$insertValuesSQL = array();
在您的foreach
循环之前,然后进行更改
$insertValuesSQL .= "('".$fileName."'),";
到
$insertValuesSQL[] = $fileName;
然后更改这些行
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL,',');
// Insert image file name into database
$insert = $conn->query("UPDATE image SET file_name = $insertValuesSQL WHERE post_id = '".$post_id."'");
if($insert){
收件人:
if (count($insertValuesSQL)){
// Delete existing image file names in database
$delete = $conn->query("DELETE FROM image WHERE post_id = '".$post_id."'");
// now insert the new ones
$insert = $conn->query("INSERT INTO image (file_name, post_id) VALUES ('" .
implode("', '$post_id'), ('", $insertValuesSQL) . "', '$post_id')");
if ($delete && $insert) {
答案 1 :(得分:0)
最好先转储查询以查看问题。
$ insertValuesSQL必须在单引号内,因此请修复此行
ServiceRegistry
进入
$insert = $conn->query("UPDATE image SET file_name = $insertValuesSQL WHERE post_id = '".$post_id."'");
还需要更改此行
$insert = $conn->query("UPDATE image SET file_name = '".$insertValuesSQL."' WHERE post_id = '".$post_id."'");
进入
$insertValuesSQL .= "('".$fileName."'),";