我正在尝试将图像上传功能添加到我的表单中,该功能使用自己的表格。问题是,它需要作为外键的“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");
}
慕标琳琳