Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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中上载的每个图像的表图像提供ads_id_Php - Fatal编程技术网

为PHP中上载的每个图像的表图像提供ads_id

为PHP中上载的每个图像的表图像提供ads_id,php,Php,我有一些简单的表单供用户上传广告,如下所示: 正如你所看到的,有一个图片表单,用户可以在他们的广告中放置多张图片 我有一张表格,上面有图像,如下所示: 正如您可以看到的那样,ads\u id列始终为0,我没有找到一种方法来为每个上传的图像放置ads\u id 广告表格如下所示: 下面是我用来执行操作的uploader.php文件: <?php // Define variables and initialize with empty values $title = $status =

我有一些简单的表单供用户上传广告,如下所示:

正如你所看到的,有一个图片表单,用户可以在他们的广告中放置多张图片

我有一张表格,上面有图像,如下所示:

正如您可以看到的那样,ads\u id列始终为0,我没有找到一种方法来为每个上传的图像放置ads\u id

广告表格如下所示:

下面是我用来执行操作的uploader.php文件:

<?php
// Define variables and initialize with empty values
$title = $status = $category = $location = $description = $mark = $notes = $user_id =  "";
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';

if(isset($_POST['submit'])){

// Include the database configuration file
include 'session.php';

// File upload configuration
$targetDir = "upload/";
$allowTypes = array('jpg','png','jpeg','gif');

//giving a value
$title = $_POST["title"];
$status = $_POST["status"];
$category = $_POST["category"];
$location = $_POST["location"];
$description = $_POST["description"];
$mark = $_POST["mark"];
$notes = $_POST["notes"];
$user_id = $_SESSION["user_id"];

mysqli_query($koneksi, "INSERT INTO ads(user_id,category_id,status,title,location,mark,description,notes,date_posted) VALUES ('$user_id', '$category','$status', '$title','$location', '$mark','$description', '$notes',NOW())");
//images uploader
if(!empty(array_filter($_FILES['images']['name']))){
    foreach($_FILES['images']['name'] as $key=>$val){
        // File upload path
        $fileName = basename($_FILES['images']['name'][$key]);
        $targetFilePath = $targetDir . $fileName;

        // Check whether file type is valid
        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
        if(in_array($fileType, $allowTypes)){
            // Upload file to server
            if(move_uploaded_file($_FILES["images"]["tmp_name"][$key], $targetFilePath)){
                // Image db insert sql
                $insertValuesSQL .= "('".$fileName."', NOW()),";
            }else{
                $errorUpload .= $_FILES['images']['name'][$key].', ';
            }
        }else{
            $errorUploadType .= $_FILES['images']['name'][$key].', ';
        }
    }

    if(!empty($insertValuesSQL)){
        $insertValuesSQL = trim($insertValuesSQL,',');
        // Insert image file name into database
        $insert = $koneksi->query("INSERT INTO images (file_name, uploaded_on) VALUES $insertValuesSQL");
        if($insert){
            $errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
            $errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
            $errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
            $statusMsg = "Files are uploaded successfully.".$errorMsg;
        }else{
            $statusMsg = "Sorry, there was an error uploading your file.";
        }
    }
}else{
    $statusMsg = 'Please select a file to upload.';
}

// Display status message
echo $statusMsg;
header('refresh:1; url=index.php');
}
?>

首先,您应该将更新查询添加到脚本中,最好在
if($insert){
之后进行此操作,以获取您可以使用的最后一个插入ID,如Carl所述,示例如下:

if($insert){
    $last_inserted_id = $insert->insert_id;
    $update_ads_id = $koneksi->query("UPDATE images SET ads_id = 2"); // or what id you want.

注意:您应该学习如何使用

实现更多安全脚本。首先,您应该在脚本中添加更新查询,最好在
if($insert){
之后进行此操作,以获取您可以使用的最后一个插入ID,如Carl所述,示例如下:

if($insert){
    $last_inserted_id = $insert->insert_id;
    $update_ads_id = $koneksi->query("UPDATE images SET ads_id = 2"); // or what id you want.

注意:您应该学习如何使用

实现更多安全脚本。您需要获取最后插入的记录的id,然后使用它。您可以执行以下操作:

if (mysqli_query($koneksi, "INSERT INTO ads(user_id,category_id,status,title,location,mark,description,notes,date_posted) VALUES ('$user_id', '$category','$status', '$title','$location', '$mark','$description', '$notes',NOW())")) {
    $last_id = mysqli_insert_id($koneksi); // now you can insert this with rest of your data to your images table...
}

您需要获取最后插入的记录的id,然后使用它。您可以执行以下操作:

if (mysqli_query($koneksi, "INSERT INTO ads(user_id,category_id,status,title,location,mark,description,notes,date_posted) VALUES ('$user_id', '$category','$status', '$title','$location', '$mark','$description', '$notes',NOW())")) {
    $last_id = mysqli_insert_id($koneksi); // now you can insert this with rest of your data to your images table...
}

感谢Morgan、Saud和Carl,mysqli\u insert\u id()就是答案

最后,我在第一次查询后放置了最后插入的\u id,这样它就得到了最新的广告id

mysqli_query($koneksi, "INSERT INTO ads(user_id,category_id,status,title,location,mark,description,notes,date_posted) VALUES ('$user_id', '$category','$status', '$title','$location', '$mark','$description', '$notes',NOW())");
$last_inserted_id = $insert->insert_id;
之后,我将其插入到图像查询中

$insertValuesSQL .= "('$last_inserted_id','".$fileName."', NOW()),";
$insert = $koneksi->query("INSERT INTO images (ads_id,file_name, uploaded_on) VALUES $insertValuesSQL");

感谢Morgan、Saud和Carl,mysqli\u insert\u id()就是答案

最后,我在第一次查询后放置了最后插入的\u id,这样它就得到了最新的广告id

mysqli_query($koneksi, "INSERT INTO ads(user_id,category_id,status,title,location,mark,description,notes,date_posted) VALUES ('$user_id', '$category','$status', '$title','$location', '$mark','$description', '$notes',NOW())");
$last_inserted_id = $insert->insert_id;
之后,我将其插入到图像查询中

$insertValuesSQL .= "('$last_inserted_id','".$fileName."', NOW()),";
$insert = $koneksi->query("INSERT INTO images (ads_id,file_name, uploaded_on) VALUES $insertValuesSQL");

您需要获取最后一个插入查询的id,就像使用我只是对mysqli_insert_id()进行一些研究,这正是我所需要的。感谢您的帮助!您需要获取最后一个插入查询的id,就像使用我只是对mysqli_insert_id()进行一些研究,这正是我所需要的。谢谢您的帮助!