小弟剛學php不久,寫了如下代碼,各位大俠見笑了。
代碼的大概功能是在當前頁面修改頁面上顯示的圖片和文字。文字資訊和圖片資訊儲存在mySQL相應的表格裡。
Item Detail
| The file is not an image.';}else{mysql_query("LOCK TABLES IMAGES WRITE");mysql_query("LOCK TABLES ITEM WRITE");if($img_id == 1){mysql_query("INSERT INTO IMAGES (IMG_NAME, IMG) VALUES ('$image_name', '$image')");$pic_id = mysql_insert_id();mysql_query("UPDATE ITEM SET IMG_ID = $pic_id WHERE ITEM_ID = $item_id");}else{mysql_query("UPDATE IMAGES SET IMG_NAME = '$image_name', IMG = '$image' WHERE IMG_ID = $img_id");}mysql_query("UNLOCK TABLES");header("location: " . $_SERVER['REQUEST_URI']);}}}}if(isset($_POST['sub_del_pic'])){mysql_query("LOCK TABLES ITEM WRITE");mysql_query("UPDATE ITEM SET IMG_ID = 1WHERE ITEM_ID = $item_id");if($img_id != 1){mysql_query("LOCK TABLES IMAGES WRITE");mysql_query("DELETE FROM IMAGES WHERE IMG_ID = $img_id");}mysql_query("UNLOCK TABLES");header("location: " . $_SERVER['REQUEST_URI']);}?> |
|
| Congratulations! The item is yours.';}else{print' Sorry, the item has been sold! ';}}else if($status == "NA"){print'Item is not available yet! ';}else if($status == "EXPIRE"){print'Item is expired. ';}else{date_default_timezone_set('America/New_York');$curr_date = date("Y-m-d");$curr_time = date("H:i");$curr_datetime = "$curr_date $curr_time:00";print"Current datetime: $curr_datetime ";$result = mysql_query("SELECT * FROM ITEM WHERE ITEM_ID = $item_id");if($result){$row = mysql_fetch_array($result);$btime = strtotime($row['BEGIN']);$etime = strtotime($row['END']);$date = date("Y-m-d", $etime);$time = date("H:i", $etime);}print'
';if($_POST['go'] == "Set Time"){$end_date = $_POST['end_date'];$end_time = $_POST['end_time'];$end_time = $end_time.':00';$end_datetime = strtotime("$end_date $end_time");$now = strtotime($default_datetime);$problem = false;if ($end_datetime <= $now){print'You cannot set End time earlier than current time!';$problem = true;}if(!$problem){mysql_query("LOCK TABLES ITEM WRITE");mysql_query("UPDATE ITEM SET END = '$end_date $end_time' WHERE ITEM_ID = $item_id");mysql_query("UNLOCK TABLES");header("location: " . $_SERVER['REQUEST_URI']);}}refresh();}?>
|
|
|
問題:第
120行的插入命令為什麼不能執行?什麼原因導致?怎麼解決?
回複討論(解決方案)
自己多echo輸出下看看,查看mysql_query()的傳回值是啥?
mysql_query("INSERT INTO IMAGES (IMG_NAME, IMG) VALUES ('$image_name', '$image')");
而
$image = addslashes(file_get_contents($file));
$file 顯然是二進位檔案
那麼僅僅 addslashes 對於 mysql 5 是不行的(mysql 4 可以)
因為大多不可列印的 ASCII 字元都會影響 mysql 的行為
如果一定要將圖片資料存放於資料庫中,那麼要存放圖片資料的 base64 編碼