猿问

使用外键上传图片

我正在尝试将图像上传功能添加到我的表单中,该功能使用自己的表格。问题是,它需要作为外键的“cardid”,因为这就是我想要存储哪个图像属于哪个卡的方式。问题是,我无法获取 cardid,因为我想将图像上传到用户创建卡片的同一页面上。所以,它还没有ID。我的“卡片”表如下所示:


id - name - phone - more fields - userid

和'cardimages'表:


id - cardid - image - name

这是我的代码:


<?php

session_start();

header('Content-type: text/html; charset=utf-8');

require_once("db_connect.php");


// Prepare an insert statement

$sql = "INSERT INTO cards (name, phone, phone2, email, zipcode, address, company, job, description, userid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";


if($stmt = mysqli_prepare($conn, $sql)){

    // Bind variables to the prepared statement as parameters

    mysqli_stmt_bind_param($stmt, "ssssissssi", $name, $phone, $phone2, $email, $zipcode, $address, $company, $job, $description, $userid);



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

        $name = $_POST['name'];

    }

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

        $phone = $_POST['phone'];

    }

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

        $phone2 = $_POST['phone2'];

    }

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

        $email = $_POST['email'];

    }

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

        $zipcode = $_POST['zipcode'];

    }

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

        $address = $_POST['address'];

    }

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

        $company = $_POST['company'];

    }

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

        $job = $_POST['job'];

    }

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

        $description = $_POST['description'];

    }


    if(isset($_SESSION['id'])){

        $userid = $_SESSION['id'];

    }



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

    $name = $_FILES['file']['name'];

    $target_dir = "upload/";

    $target_file = $target_dir . basename($_FILES["file"]["name"]);


    // Select file type

    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));


    // Valid file extensions

    $extensions_arr = array("jpg","jpeg","png","gif");


        }



不负相思意
浏览 96回答 1
1回答

慕标琳琳

我更改了两个插入,添加 select for catch last insert id 卡然后我上传图像并插入 DB<?phpsession_start();header('Content-type: text/html; charset=utf-8');require_once "db_connect.php";// Prepare an insert statement$sql = "INSERT INTO cards (name, phone, phone2, email, zipcode, address, company, job, description, userid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";$stmt = mysqli_prepare($conn, $sql);// Bind variables to the prepared statement as parametersmysqli_stmt_bind_param($stmt, "ssssissssi", $name, $phone, $phone2, $email, $zipcode, $address, $company, $job, $description, $userid);if (mysqli_stmt_execute($stmt)) {&nbsp; &nbsp; if (isset($_POST['name'])) {&nbsp; &nbsp; &nbsp; &nbsp; $name = $_POST['name'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['phone'])) {&nbsp; &nbsp; &nbsp; &nbsp; $phone = $_POST['phone'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['phone2'])) {&nbsp; &nbsp; &nbsp; &nbsp; $phone2 = $_POST['phone2'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['email'])) {&nbsp; &nbsp; &nbsp; &nbsp; $email = $_POST['email'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['zipcode'])) {&nbsp; &nbsp; &nbsp; &nbsp; $zipcode = $_POST['zipcode'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['address'])) {&nbsp; &nbsp; &nbsp; &nbsp; $address = $_POST['address'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['company'])) {&nbsp; &nbsp; &nbsp; &nbsp; $company = $_POST['company'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['job'])) {&nbsp; &nbsp; &nbsp; &nbsp; $job = $_POST['job'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['description'])) {&nbsp; &nbsp; &nbsp; &nbsp; $description = $_POST['description'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_SESSION['id'])) {&nbsp; &nbsp; &nbsp; &nbsp; $userid = $_SESSION['id'];&nbsp; &nbsp; }&nbsp; &nbsp; if (isset($_POST['upload'])) {&nbsp; &nbsp; &nbsp; &nbsp; $name = $_FILES['file']['name'];&nbsp; &nbsp; &nbsp; &nbsp; $target_dir = "upload/";&nbsp; &nbsp; &nbsp; &nbsp; $target_file = $target_dir . basename($_FILES["file"]["name"]);&nbsp; &nbsp; &nbsp; &nbsp; // Select file type&nbsp; &nbsp; &nbsp; &nbsp; $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));&nbsp; &nbsp; &nbsp; &nbsp; // Valid file extensions&nbsp; &nbsp; &nbsp; &nbsp; $extensions_arr = array("jpg", "jpeg", "png", "gif");&nbsp; &nbsp; &nbsp; &nbsp; // Check extension&nbsp; &nbsp; &nbsp; &nbsp; if (in_array($imageFileType, $extensions_arr)) {&nbsp; &nbsp; &nbsp; &nbsp; // Convert to base64&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image_base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = 'data:image/'.$imageFileType.';base64,'.$image_base64;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $selectid = $conn->prepare("SELECT id FROM cards DESC limit 1");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $selectid->execute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $resultid = $selectid->get_result();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res = $resultid->fetch_array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cardid = $res['id'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Insert record&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imagequery = $conn->prepare("INSERT INTO cardimages(cardid, name, image) VALUES (?,?,?)");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imagequery->bind_param('iss', $cardid, $name, $image);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $imagequery->execute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Upload file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move_uploaded_file($_FILES['file']['tmp_name'], 'upload/'.$name);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; header("Location: addbusiness.php?message=1");} else {&nbsp; &nbsp; echo "ERROR: Could not execute query: $sql. " . mysqli_error($conn);}// Close statementmysqli_stmt_close($stmt);// Close connectionmysqli_close($conn);另一个变化是所有查询都准备好了。记住总是这样做。参考
随时随地看视频慕课网APP
我要回答