无法将两个数组值插入数据库
Trouble inserting two array values into database
这是我用于为数据库中的每个条目上传多张图片的代码; ID、图像、名称、日期。通过使用此代码,图像名称可以正确插入,但名称字段有问题。我想使用 $product_name= $_POST['pro_name'];
插入名称,但它只会在名称字段中插入 'array'。
<?php
include('../config.php');
if (isset($_POST['submit'])) {
$product_name = $_POST['pro_name'];
$j = 0; // Variable for indexing uploaded image.
$target_path = "uploads/"; // Declaring Path for uploaded images.
for ($i = 0; $i < count($_FILES['file']['name']) && $product_name < count($product_name); $i++) {
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); // Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$i])); // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; // Set the target path with a new name of image.
$j = $j + 1; // Increment the number of uploaded images according to the files in array.
if (($_FILES["file"]["size"][$i] < 100000000) // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
$finel_name = explode('/', $target_path);
$image_name_final = $finel_name[1];
$jajsj = "insert into spec_product set image='$image_name_final', name='$product_name'";
$janson = mysql_query($jajsj) or die(mysql_error());
// If file moved to uploads folder.
echo $j . ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
} else { // If File Was Not Moved.
echo $j . ').<span id="error">please try again!.</span><br/><br/>';
}
} else { // If File Size And File Type Was Incorrect.
echo $j . ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
}
?>
值作为数组插入,因为变量 $product_name 不是字符串而是数组。每当在需要字符串的地方使用数组时,例如:字符串的连接或在您的情况下查询语句:insert into spec_product set image='$image_name_final', name='$product_name'"; PHP 会自动将数组转换为其默认字符串值 "Array".
确保 $product_name 不是一个数组,而是一个字符串,其中包含您要插入到 table 中的产品名称。
此致,
尼廷塔库尔
替换成你的代码
for ($i = 0; $i < count($_FILES['file']['name']); $i++)
{
并在你的查询执行之前添加这个
$product_name_final= $product_name[$i];
这是我用于为数据库中的每个条目上传多张图片的代码; ID、图像、名称、日期。通过使用此代码,图像名称可以正确插入,但名称字段有问题。我想使用 $product_name= $_POST['pro_name'];
插入名称,但它只会在名称字段中插入 'array'。
<?php
include('../config.php');
if (isset($_POST['submit'])) {
$product_name = $_POST['pro_name'];
$j = 0; // Variable for indexing uploaded image.
$target_path = "uploads/"; // Declaring Path for uploaded images.
for ($i = 0; $i < count($_FILES['file']['name']) && $product_name < count($product_name); $i++) {
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); // Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$i])); // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; // Set the target path with a new name of image.
$j = $j + 1; // Increment the number of uploaded images according to the files in array.
if (($_FILES["file"]["size"][$i] < 100000000) // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
$finel_name = explode('/', $target_path);
$image_name_final = $finel_name[1];
$jajsj = "insert into spec_product set image='$image_name_final', name='$product_name'";
$janson = mysql_query($jajsj) or die(mysql_error());
// If file moved to uploads folder.
echo $j . ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
} else { // If File Was Not Moved.
echo $j . ').<span id="error">please try again!.</span><br/><br/>';
}
} else { // If File Size And File Type Was Incorrect.
echo $j . ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
}
?>
值作为数组插入,因为变量 $product_name 不是字符串而是数组。每当在需要字符串的地方使用数组时,例如:字符串的连接或在您的情况下查询语句:insert into spec_product set image='$image_name_final', name='$product_name'"; PHP 会自动将数组转换为其默认字符串值 "Array".
确保 $product_name 不是一个数组,而是一个字符串,其中包含您要插入到 table 中的产品名称。
此致, 尼廷塔库尔
替换成你的代码
for ($i = 0; $i < count($_FILES['file']['name']); $i++)
{
并在你的查询执行之前添加这个
$product_name_final= $product_name[$i];