需要在TWO表PHP / MYSQLI之间建立连接

时间:2014-11-01 23:18:21

标签: php mysql database mysqli insert

这是我的代码:

    <?php
    $target_dir = "videoCover/";
    $target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]);
    $uploadOk=1;

    // Check if file already exists
    if (file_exists($target_dir . $_FILES["uploadFile"]["name"])) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }

    // Check file size
    if ($uploadFile_size > 128000000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }


    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk==0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else { 
        if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir)) {
            echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }



    include '../connect/con.php';

    $id = mysqli_real_escape_string($con, $_POST['id']);
    $vidTitle = mysqli_real_escape_string($con, $_POST['vidTitle']);
    $imgCover = $_FILES['uploadFile']['name'];
    $size = $_FILES['uploadFile']['size'];
    $type = $_FILES['uploadFile']['type'];
    $url = '/upload/videoCover/'.$_FILES["uploadFile"]["name"];
    $vidSD = mysqli_real_escape_string($con, $_POST['vidSD']);
    $sql="INSERT INTO newsvid (id, vidTitle, imgCover, size, type, url, vidSD) VALUES ('$id', '$vidTitle', '$imgCover', '$size', '$type', '$url', '$vidSD')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }echo "Video links are added";

    mysqli_close($con);
    ?>


    <?php

    include '../connect/con.php';


    if(isset($_FILES['file_array'])){

        $name_array = $_FILES['file_array']['name'];
        $tmp_name_array = $_FILES['file_array']['tmp_name'];
        $type_array = $_FILES['file_array']['type'];
        $size_array = $_FILES['file_array']['size'];
        $error_array = $_FILES['file_array']['error'];
        for($i = 0; $i < count($tmp_name_array); $i++){
            if(move_uploaded_file($tmp_name_array[$i], "videoScreenShots/".$name_array[$i])){
                echo $name_array[$i]." upload is complete<br>";
                $imgShot[$i]='videoScreenShots/'.$name_array[$i];
            } else {
                echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
            }   
        }
}



    $idvi = mysqli_insert_id($con);
    $vidLD = mysqli_real_escape_string($con, $_POST['vidLD']);
    $vidYear = mysqli_real_escape_string($con, $_POST['vidYear']);
    $vidCity = mysqli_real_escape_string($con, $_POST['vidCity']);
    $vidZanr = mysqli_real_escape_string($con, $_POST['vidZanr']);
    $vidZanr2 = mysqli_real_escape_string($con, $_POST['vidZanr2']);
    $vidZanr3 = mysqli_real_escape_string($con, $_POST['vidZanr3']);
    $vidQuality = mysqli_real_escape_string($con, $_POST['vidQuality']);
    $vidTranslated = mysqli_real_escape_string($con, $_POST['vidTranslated']);
    $vidTime = mysqli_real_escape_string($con, $_POST['vidTime']);
    $vidMaker = mysqli_real_escape_string($con, $_POST['vidMaker']);
    $vidRoles = mysqli_real_escape_string($con, $_POST['vidRoles']);

    $sql="INSERT INTO videoinformation (id, vidLD, vidYear, vidCity, vidZanr, vidZanr2, vidZanr3, vidQuality, vidTranslated, vidTime, vidMaker, vidRoles, imgShot1, imgShot2, imgShot3) VALUES ('$idvi', '$vidLD', '$vidYear', '$vidCity', '$vidZanr', '$vidZanr2', '$vidZanr3', '$vidQuality', '$vidTranslated', '$vidTime', '$vidMaker', '$vidRoles', '".$imgShot[0]."','".$imgShot[1]."','".$imgShot[2]."')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }echo "Video Description are added";

    mysqli_close($con);
    ?>

问题是我使用此$idvi = mysql_insert_id($con);从第一个上传信息到第一个表(newsvid)获取ID。

我所需要的只是:我的表格有很多不同的信息,我尝试将所有这些信息上传到两个不同的表格中。在首先上传到table1并使用table1中的id上传信息到table2的方式。最后,我陷入了困境,不知道怎么做(

P.S。目前使用此代码我只有空/空白页。

P.P.S 出错:Error: Cannot add or update a child row: a foreign key constraint fails (denzw681_u.videoinformation, CONSTRAINT videoinformation_ibfk_1 FOREIGN KEY (id) REFERENCES newsvid (id))

1 个答案:

答案 0 :(得分:0)

需要删除此位:

mysqli_close($con);
?>


<?php

include '../connect/con.php';

添加东西之间的分离。现在它正在发挥作用。

和其他人一样,错误而不是 mysqli 我使用 mysql

并且缺少“}”现在代码正在运行))