Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php mysql循环的多次上载上次插入id_Php_Mysql - Fatal编程技术网

php mysql循环的多次上载上次插入id

php mysql循环的多次上载上次插入id,php,mysql,Php,Mysql,我将输入表单插入到两个不同的表中。第一个表用于信息,另一个表用于信息的图像 当我提交信息时,我希望在单独的表中的第一个表的每一行上载多个图像,并将每个图像的最后一个插入id插入到第二个表中 table1 location ID | address | contact information table2 pictureID | location ID | filepath 在下面的代码中,它只插入一个图像,但将所有图像上载到文件夹中 <?php include_once

我将输入表单插入到两个不同的表中。第一个表用于信息,另一个表用于信息的图像

当我提交信息时,我希望在单独的表中的第一个表的每一行上载多个图像,并将每个图像的最后一个插入id插入到第二个表中

table1
location ID | address | contact information 

table2
pictureID | location ID | filepath 
在下面的代码中,它只插入一个图像,但将所有图像上载到文件夹中

<?php 

    include_once 'dbconnect.php';
    mysql_query("SET NAMES UTF8");
    session_start();
    $tbl_name="location";
    $tbl_image="image";

    if(isset($_POST['btn-upload'])){

        $name2=$_POST['name2'];
        $phone=$_POST['phone'];
        $email=$_POST['email'];
        $type=$_POST['type'];
        $other=$_POST['other'];
        $description=$_POST['description'];
        $address=$_POST['address'];
        $name=$_POST['name'];
        $lat=$_POST['lat'];
        $lng=$_POST['lng'];
        $country=$_POST['country'];
        $administrative_area_level_1=$_POST['administrative_area_level_1'];
        $place_id=$_POST['place_id'];
        $url=$_POST['url'];
        $website=$_POST['website'];




    $sql="INSERT INTO $tbl_name(name2, phone, email, type, other, description, address, name, lat, lng, country, administrative_area_level_1, place_id, url, website) VALUES ('$name2', '$phone', '$email', '$type','$other', '$description', '$address', '$name', '$lat', '$lng', '$country', '$administrative_area_level_1', '$place_id', '$url', '$website')";
    $result=mysql_query($sql);

        for($i=0; $i<count($_FILES['image']['tmp_name']); $i++)
        {
            $file = rand(1000,100000)."-".$_FILES['image']['name'][$i];
            $file_loc = $_FILES['image']['tmp_name'][$i];
            $file_size = $_FILES['image']['size'][$i];
            $file_type = $_FILES['image']['type'][$i];
            $folder="uploads/";

            // new file size in KB
            $new_size = $file_size/1024;  
            // new file size in KB

            // make file name in lower case
            $new_file_name = strtolower($file);
            // make file name in lower case

            $final_file=str_replace(' ','-',$new_file_name);
            $res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
            $userRow=mysql_fetch_array($res);
            $userID=$userRow['userID'];

            if(move_uploaded_file($file_loc,$folder.$final_file))
            {
                $sql="INSERT INTO $tbl_image(user_ID, Location_ID, file) VALUES('$userID', LAST_INSERT_ID(), '$final_file')";
                mysql_query($sql);
                ?>
                <script>
                alert('successfully uploaded');
                window.location.href='index.php?success';
                </script>
                <?php
            }
            else
            {
                ?>
                <script>
                alert('error while uploading file');
                window.location.href='index.php?fail';
                </script>
                <?php
            }

        }

        if($result){
        echo "Successful";
        echo "<BR>";
        echo "<a href='index.php'>Back to main page</a>";
        }

        else {
        echo "ERROR";
        }

        if($result1){
        echo "Upload Successful";
        echo "<BR>";
        echo "<a href='locationform.php'>Back to main page</a>";
        }

        else {
        echo "ERROR";
        }


    }   




?> 

<?php 
// close connection 
mysql_close();
?>

终于明白了。这是最后一个有问题的插入ID。必须在For循环之外声明最后一个insert ID

$locationID=mysql_insert_id($conn);
上面是添加在第一个插入查询的正下方

$sql="INSERT INTO $tbl_location(name2, phone, email, type, other, description, address, name, lat, lng, country, administrative_area_level_1, place_id, url, website) 
    VALUES ('$name2', '$phone', '$email', '$type','$other', '$description', '$address', '$name', '$lat', '$lng', '$country', '$administrative_area_level_1', '$place_id', '$url', '$website')";
    $result=mysql_query($sql);

    $locationID=mysql_insert_id($conn);

    for($i=0; $i<count($_FILES['image']['name']); $i++)
    {
        $file = rand(1000,100000)."-".$_FILES['image']['name'][$i];
        $file_loc = $_FILES['image']['tmp_name'][$i];
        $file_size = $_FILES['image']['size'][$i];
        $file_type = $_FILES['image']['type'][$i];
        $folder="uploads/";

        // new file size in KB
        $new_size = $file_size/1024;  
        // new file size in KB

        // make file name in lower case
        $new_file_name = strtolower($file);
        // make file name in lower case

        $final_file=str_replace(' ','-',$new_file_name);
        $res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
        $userRow=mysql_fetch_array($res);
        $userID=$userRow['userID'];


        if(move_uploaded_file($file_loc,$folder.$final_file))
        {
            $sql2="INSERT INTO $tbl_image(user_ID, Location_ID, file) VALUES('$userID', '$locationID', '$final_file')";
            $result = mysql_query($sql2);
            ?>
            <script>
            alert('successfully uploaded');
            window.location.href='index.php?success';
            </script>
            <?php
        }
        else
        {
            ?>
            <script>
            alert('error while uploading file');
            window.location.href='index.php?fail';
            </script>
            <?php
        }

    }
$sql=“插入$tbl\u位置(姓名2、电话、电子邮件、类型、其他、描述、地址、姓名、lat、lng、国家、行政区域、级别1、地点id、url、网站)
值(“$name2”、“$phone”、“$email”、“$type”、“$other”、“$description”、“$address”、“$name”、“$lat”、“$lng”、“$country”、“$administration\u area\u level\u 1”、“$place\u id”、“$url”、“$website”);
$result=mysql\u查询($sql);
$locationID=mysql\u insert\u id($conn);
对于($i=0;$i)
警报(“已成功上载”);
window.location.href='index.php?成功';
警报(“上传文件时出错”);
window.location.href='index.php?失败';

//如何在数据库中上载多个图像,但将图像保存在文件夹中,并使用带有循环的最后一个插入id的上载表//
表1-表1
ID |字段1 |字段2
表2-上传
图片ID | ID |上传图片

您的代码是正确的,但在第一次成功的图像表插入之后,您将重定向代码。因此,在图像表中只得到一个插入项。由于MySQL L*在PHP 5.5中被弃用(请参阅)。您应该真正考虑使用。这将有帮助。永远不要相信用户的数据!请使用<代码> Error报告(EyALL);InIySeT(“DePaseSaleError”,1)。
在页面顶部,告诉我们是否有错误。此外,您不应假设sql调用成功。在每个sql查询之后添加错误处理并打印任何错误消息。如何不重定向代码?您需要解释您的答案
$sql="INSERT INTO $tbl_location(name2, phone, email, type, other, description, address, name, lat, lng, country, administrative_area_level_1, place_id, url, website) 
    VALUES ('$name2', '$phone', '$email', '$type','$other', '$description', '$address', '$name', '$lat', '$lng', '$country', '$administrative_area_level_1', '$place_id', '$url', '$website')";
    $result=mysql_query($sql);

    $locationID=mysql_insert_id($conn);

    for($i=0; $i<count($_FILES['image']['name']); $i++)
    {
        $file = rand(1000,100000)."-".$_FILES['image']['name'][$i];
        $file_loc = $_FILES['image']['tmp_name'][$i];
        $file_size = $_FILES['image']['size'][$i];
        $file_type = $_FILES['image']['type'][$i];
        $folder="uploads/";

        // new file size in KB
        $new_size = $file_size/1024;  
        // new file size in KB

        // make file name in lower case
        $new_file_name = strtolower($file);
        // make file name in lower case

        $final_file=str_replace(' ','-',$new_file_name);
        $res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
        $userRow=mysql_fetch_array($res);
        $userID=$userRow['userID'];


        if(move_uploaded_file($file_loc,$folder.$final_file))
        {
            $sql2="INSERT INTO $tbl_image(user_ID, Location_ID, file) VALUES('$userID', '$locationID', '$final_file')";
            $result = mysql_query($sql2);
            ?>
            <script>
            alert('successfully uploaded');
            window.location.href='index.php?success';
            </script>
            <?php
        }
        else
        {
            ?>
            <script>
            alert('error while uploading file');
            window.location.href='index.php?fail';
            </script>
            <?php
        }

    }