gpt4 book ai didi

PHP 分解并更新数据到 mysql 数据库中的每一列

转载 作者:行者123 更新时间:2023-11-30 22:17:09 26 4
gpt4 key购买 nike

我有一个表单,我希望用户上传一张或多张图片。我为此只使用了一个文件字段。问题是当我提交表单时,只有“itemImageOne”字段会更新。但是 echo 部分 ( echo $pieces[0], echo $pieces[1] ) 显示了正确的结果。波纹管是我的代码的一部分。

<?php
error_reporting(E_ALL & ~E_NOTICE);
@ini_set('post_max_size', '64M');
@ini_set('upload_max_filesize', '64M');

/* * *********************************************** */
// database constants
define('DB_DRIVER', 'mysql');
define('DB_SERVER', 'localhost');
define('DB_SERVER_USERNAME', 'root');
define('DB_SERVER_PASSWORD', '');
define('DB_DATABASE', 'ibs');

$dboptions = array(
PDO::ATTR_PERSISTENT => FALSE,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

try {
$DB = new PDO(DB_DRIVER . ':host=' . DB_SERVER . ';dbname=' . DB_DATABASE, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, $dboptions);
} catch (Exception $ex) {
echo $ex->getMessage();
die;
}

if (isset($_POST["add"])) {
// include resized library
//require_once('./php-image-magician/php_image_magician.php');
$msg = "";
$pieces = "";
$valid_image_check = array("image/gif", "image/jpeg", "image/jpg", "image/png", "image/bmp");
if (count($_FILES["user_files"]) > 0) {
$folderName = "uploads/";

//$sql = "UPDATE module_course SET itemImage='$filename' WHERE id=1";
//$sql = "UPDATE SET module_course(itemImage) VALUES (:img)";
//$stmt = $DB->prepare($sql);

for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) {

if ($_FILES["user_files"]["name"][$i] <> "") {

$image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));
// if valid image type then upload
if (in_array($image_mime, $valid_image_check)) {

$ext = explode("/", strtolower($image_mime));
$ext = strtolower(end($ext));
$filename = rand(10000, 990000) . '_' . time() . '.' . $ext;
$filepath = $folderName . $filename;




$pieces = explode(" ", $filename);
$imageOne = $pieces[0];
$imageTwo = $pieces[1];
echo $pieces[0]."<br/>"; // piece1
echo $pieces[1]."<br/>"; // piece2

$sql = "UPDATE module_course SET itemImageOne = $imageOne, itemImageTwo = $date WHERE id='1'";
$stmt = $DB->prepare($sql);



if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath)) {
$emsg .= "Failed to upload <strong>" . $_FILES["user_files"]["name"][$i] . "</strong>. <br>";
$counter++;
} else {
$smsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> uploaded successfully. <br>";


/* * ****** insert into database starts ******** */
try {
$stmt->bindValue(":img", $filename);
$stmt->execute();
$result = $stmt->rowCount();
if ($result > 0) {
// file uplaoded successfully.
} else {
// failed to insert into database.
}
} catch (Exception $ex) {
$emsg .= "<strong>" . $ex->getMessage() . "</strong>. <br>";
}
/* * ****** insert into database ends ******** */
}
} else {
$emsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> not a valid image. <br>";
}
}
}


$msg .= (strlen($smsg) > 0) ? successMessage($smsg) : "";
$msg .= (strlen($emsg) > 0) ? errorMessage($emsg) : "";
} else {
$msg = errorMessage("You must upload atleast one file");
}
}
?>

最佳答案

尝试

UPDATE module_course 
SET itemImageOne = $imageOne, itemImageTwo = $imageTwo
WHERE id = '1'; # Changed

已更改 - 编辑 01

$stmt = $DB->prepare("UPDATE module_course 
SET itemImageOne = ?, itemImageTwo = ?
WHERE id = 1");


$stmt->bindValue(1, $imageOne, PDO::PARAM_STR);
$stmt->bindValue(2, $imageTwo, PDO::PARAM_STR);
$stmt->execute();

Error Bcz of you are binding one parameter $stmt->bindValue(":img", $filename);

关于PHP 分解并更新数据到 mysql 数据库中的每一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37904552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com