我在下面有一个 mysqli 代码,它上传文件并将数据插入“图像”表:
<?php
session_start();
$username="xxx";
$password="xxx";
$database="mobile_app";
$mysqli = new mysqli("localhost", $username, $password, $database);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$result = 0;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES (?)";
if (!$insert = $mysqli->prepare($imagesql)) {
// Handle errors with prepare operation here
}
//Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s",$img);
//Assign the variable
$img = 'ImageFiles/'.$_FILES['fileImage']['name'];
$insert->execute();
}
}
?>
因此,例如,如果我将 2 个图像“cat.png”和“dog.png”插入“图像”数据库表中,它将像这样插入:
ImageId ImageFile
220 cat.png
221 dog.png
(ImageId 是一个自动增量)
无论如何,我的问题是,当上传文件时,不仅将数据插入到上面的表中,而且我还希望能够检索上面插入的 ImageId 并将其放在下面的 Image_Question 表中,这样就可以了像这样:
ImageId SessionId QuestionId
220 cat.png 1
221 dog.png 4
我的问题是如何从 Image Table 中检索 ImageId 并将其插入 Image_Question 表中?下面是我的代码,它将使用 mysql 将数据插入到 Image_Question 表中,只需要能够从 Image 表中检索 ImageId:
$imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId)
VALUES (?, ?, ?)";
if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) {
// Handle errors with prepare operation here
}
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
$insertimagequestion->bind_param("sss", $sessid, $_POST['numQuestion'][$i]);
$insertimagequestion->execute();
if ($insertimagequestion->errno) {
// Handle query error here
}
$insertimagequestion->close();