我有一箇舊的mysql代碼,它成功地將一個圖像文件的名稱插入到數據庫中。但是,由於舊的mysql被斬首,我試圖在mysqli中做同樣的事情(由於PHP版本的原因,不能使用PDO)。問題是我無法將圖像文件名插入到mysqli中的數據庫中。我究竟做錯了什麼?數據沒有插入mysqli
下面是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;
if(getimagesize($_FILES['fileImage']['tmp_name'])){
if(is_file("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while(is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
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
}
$insert->bind_param("s",'ImageFiles/' . $_FILES['fileImage']['name']);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
}
else
{
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
}
$insert->bind_param("s",'ImageFiles/' . $_FILES['fileImage']['name']);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
}
}
?>
下面是舊版本的MySQL代碼中插入所做的工作:
<?php
session_start();
$username="xxx";
$password="xxx";
$database="mobile_app";
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die("Unable to select database");
$result = 0;
if(getimagesize($_FILES['fileImage']['tmp_name'])){
if(is_file("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while(is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
}
}
mysql_close();
?>
<script language="javascript" type="text/javascript">
window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');
</script>
您是否在某處出現錯誤? – Jocelyn
一個好的開始將是用實際的錯誤處理替換「處理錯誤」類型的註釋。或者至少顯示錯誤。 – smoove
給我5分鐘檢查錯誤 – user1490145