dongtan7998 2015-01-06 08:57
浏览 44
已采纳

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

Here is the code I am using for multiple image upload for every entry in the database; id, image, name, date. By using this code, the image name is inserting correctly but I have a problem with the name field. I want to insert the name using $product_name= $_POST['pro_name']; but it only inserts 'array' in name field.

<?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条回答 默认 最新

  • dpglo66848 2015-01-06 09:35
    关注

    replace in your code

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

    and add this before your query execute

    $product_name_final= $product_name[$i];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题