无法将两个数组值插入数据库

时间:2015-01-06 08:57:47

标签: php arrays

这是我用于数据库中每个条目的多个图像上传的代码; 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/>';
        }
    }
}
?>

2 个答案:

答案 0 :(得分:1)

该值是作为数组插入的,因为变量$ product_name不是字符串而是数组。只要在需要字符串的地方使用数组,例如:字符串连接或在您的情况下查询语句:insert into spec_product set image ='$ image_name_final',name ='$ product_name'“; PHP将自动将数组转换为其默认字符串值“Array”。

确保$ product_name不是数组,而是包含要在表中插入的产品名称的字符串。

此致 Nitin Thakur

答案 1 :(得分:0)

替换你的代码

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

并在查询执行

之前添加此项
$product_name_final= $product_name[$i];