麻烦帮小弟解决一下这个问题
时间:2021-07-01 10:21:17
帮助过:2人阅读
小弟刚学php不久,写了如下代码,各位大侠见笑了。
代码的大概功能是在当前页面修改页面上显示的图片和文字。文字信息和图片信息存储在mySQL相应的表格里。
Item Detail
Edit item: | | 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 = 1 WHERE 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 编码