我在下面有一个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
我的问题是如何从图像表中检索ImageId并将其插入Image_Question表?下面是我的代码,它将使用mysql将数据插入到Image_Question表中,只需要能够从Image Table中检索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();