>백엔드 개발 >PHP 튜토리얼 >请大侠们帮我看看这个问题出在哪?

请大侠们帮我看看这个问题出在哪?

WBOY
WBOY원래의
2016-06-23 14:08:30942검색

代码如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><?php 	require_once 'header.php'; 	require_once 'check.php';?><meta content="en-us" http-equiv="Content-Language" /><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>Item Detail</title><style type="text/css">.auto-style2 {	text-align: center;}.auto-style1 {	font-size: 36pt;	text-align: left;	font-family: Default;}.auto-style3 {	font-family: Broadway;}</style></head><body><?php	if(isset($_POST['submit_edit_item_id']))	{		$_SESSION['eidt_item_id'] = $_POST['sub_edit_item_id'];	}		$item_id = $_SESSION['eidt_item_id'];			$result = mysql_query("SELECT * FROM ITEM WHERE ITEM_ID = $item_id");		if($result)	{		$row = mysql_fetch_array($result);				$user_id = $_SESSION['id'];		$name = $row['NAME'];		$dsc = $row['DESCRIPTION'];		$auc = $row['RESERVE_PRICE'];		$bin = $row['BIN_PRICE'];		$btime = $row['BEGIN'];		$etime = $row['END'];		$status = $row['STATUS'];		$seller = $row['SELLER_ID'];		$winner = $row['WINNER_ID'];		$curr_bid = $row['CURR_BID'];		$num_bid = $row['NUM_BID'];		$img_id = $row['IMG_ID'];	}		$result = mysql_query("SELECT USER_NAME FROM USER WHERE USER_ID = $seller");	if($result)	{		$row = mysql_fetch_array($result);		$seller_name = $row['USER_NAME'];	}?>		<table align="center" style="width: 100%">			<tr>				<td>				<div class="auto-style2">					<table align="center" style="width: 100%">						<tr>							<td class="auto-style1">Edit item:</td>							<td>							<a href="http://acadweb1.salisbury.edu/~rwan/home.php">							<img     style="max-width:90%" src="logo.jpg" style="float: right"  style="max-width:90%" / alt="请大侠们帮我看看这个问题出在哪?" ></a></td>						</tr>					</table>				</div>				<hr />				<table style="width: 100%">					<tr>						<td rowspan="2">						<?php						display_image($img_id, 500, 400);						?>												<form action="edit_item.php" method="post" enctype="multipart/form-data">						<table><tr><td><input type="file" name="image" /></td></tr>						<tr><td><input name="sub_change_pic" type="submit" value="Change picture">						<input name="sub_del_pic" type="submit" value="Delete picture"></td></tr>						</table></form>												<?php						if(isset($_POST['sub_change_pic']))						{							if(isset($_FILES['image']))							{								$file = $_FILES['image']['tmp_name'];								if(!empty($file))								{									$image = addslashes(file_get_contents($file));									$image_name = addslashes($_FILES['image']['name']);									$image_size = getimagesize($file);																									if($image_size == false)									{										print'<p class = "error">The file is not an image.</p>';									}																	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']))						{							echo "delete click";							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']);														}						?>											</td>						<td><form action="edit_item.php" method="post">						<textarea cols="50" name="title" rows="4"><?php print $name; ?></textarea><br>						<input name="change_item_title" type="submit" value="Change item title" /></form></td>											</tr>										<tr>						<td><hr />										<?php						if(isset($_POST['change_item_title']))					{						$name = $_POST['title'];						mysql_query("LOCK TABLES ITEM WRITE");						mysql_query("UPDATE ITEM SET NAME = '$name' WHERE ITEM_ID = $item_id");						mysql_query("UNLOCK TABLES");						header("location: " . $_SERVER['REQUEST_URI']);						}																if($status == "SOLD")					{						$result = mysql_query("SELECT * FROM ITEM_SOLD WHERE ITEM_ID = $item_id");						$row = mysql_fetch_array($result);						$buyer = $row['BUYER_ID'];												if($_SESSION['id'] == $buyer)						{							print'<p class = "error">Congratulations! The item is yours.</p>';						}						else						{							print'<p class = "error">Sorry, the item has been sold!</p>';						}					}					else if($status == "NA")					{						print'<p class = "error">Item is not available yet!</p>';					}					else if($status == "EXPIRE")					{						print'<p class = "error">Item is expired.</p>';					}					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"<p><strong>Current datetime: $curr_datetime</strong></p>";												$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'<table><tr><td><form action="edit_item.php" method="post">						  Change End time:</td></tr><tr><td><input type="date" name="end_date" value='.$date.'>						  <input type="time" name="end_time" value='.$time.'></td>						  <td><input type="submit" name= "go" value="Set Time">						</form></td></tr></table>';						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();					}										?>										<hr /><br />															<table><tr><td>Buy it Now Price:</td></tr>						<tr><td><form action ="edit_item.php" method="post">						<input name="price" type="text" value="<?php print $bin; ?>" ></td></tr>						<tr><td><input name="sub_change_price" type="submit" value="Change price" /></form></td></tr></table>												<?php						if(isset($_POST['sub_change_price']))						{							$price = $_POST['price'];							mysql_query("LOCK TABLES ITEM WRITE");							mysql_query("UPDATE ITEM SET BIN_PRICE = $price WHERE ITEM_ID = $item_id");							mysql_query("UNLOCK TABLES");							header("location: " . $_SERVER['REQUEST_URI']);							}					?>											</td>						</tr>						<tr>							<td class="auto-style3" colspan="2">							<form action ="edit_item.php" method="post">							<textarea cols="80" name="description" rows="10"><?php print $dsc; ?></textarea><br>							<input name="change_dsc" type="submit" value="Change item description" /></form></td>						</tr>											<?php					if(isset($_POST['change_dsc']))					{						$dsc = $_POST['description'];						mysql_query("LOCK TABLES ITEM WRITE");						mysql_query("UPDATE ITEM SET DESCRIPTION = '$dsc' WHERE ITEM_ID = $item_id");						mysql_query("UNLOCK TABLES");						header("location: " . $_SERVER['REQUEST_URI']);						}					?>					</table>					</td>				</tr>			</table>			</body><?php require_once 'webmaster.php'; ?></html>



我不想误导大家, 但我自己的分析,认为问题出在 94-149行之间的这段php代码上。
IMAGES的表里存储的是BLOB图片,IMG_ID我设置的是自动增长的,IMG_ID为1的图片是默认的错误信息图。

每当我插入一张新的图片,名为IMAGES里面会有新的图片插入,并自动生成IMG_ID,ITEM里面的IMG_ID无法自动更新(详情见121行), 135-147行代码也是相同的问题,如果成功删除图片,无法将IMG_ID改回1。
我已经做了实验,print $item_id的值,发现$item_id的值是没有问题的。另外table的拼写也无错误。。。

难道是什么地方不小心把table lock了,而且没有unlock?可是即使lock了,我自己应该还是有权限修改的不是吗?小弟百思不得其解

麻烦各位大神帮小弟分析一下原因,小弟感激不尽


回复讨论(解决方案)

自增长id应该是不会回退的吧?

自增长id应该是不会回退的吧?

谢谢你。可以解释的详细点吗? 问题该怎么解决呢?

$pic_id = mysql_insert_id();
mysql_query("UPDATE ITEM SET IMG_ID = $pic_id WHERE ITEM_ID = $item_id");

既然 IMG_ID 是自动增长的, 那么 IMG_ID 就是主键了
主键是不可重复的!
SET IMG_ID = $pic_id 就有重复的嫌疑

至少在你的代码中没有看到对 ITEM_ID 赋值的代码
而有
$item_id = $_SESSION['eidt_item_id'];
但是也没有看到 session_start();

虽说 ITEM_ID 可能有初值,但 $item_id 为空的话将造成 SQL 指令为
UPDATE ITEM SET IMG_ID = nnnn WHERE ITEM_ID = 
显然是错误的

估计是列名弄混了

斑竹 火眼金睛~

$pic_id = mysql_insert_id();
mysql_query("UPDATE ITEM SET IMG_ID = $pic_id WHERE ITEM_ID = $item_id");

既然 IMG_ID 是自动增长的, 那么 IMG_ID 就是主键了
主键是不可重复的!
SET IMG_ID = $pic_id 就有重复的嫌疑

至少在你的代码中没有看到对 ITEM_ID 赋值的代码
而有
$item_id = $_SESSION['eidt_item_id'];
但是也没有看到 session_start();

虽说 ITEM_ID 可能有初值,但 $item_id 为空的话将造成 SQL 指令为
UPDATE ITEM SET IMG_ID = nnnn WHERE ITEM_ID = 
显然是错误的

估计是列名弄混了

谢谢版主大神

我解释一下我的构思

因为我想换了图片以后,立即显示出来,所以加了以下语句

header("location: " . $_SERVER['REQUEST_URI']);

但加入之后会有个问题。一旦点击换图按钮,页面就会自动刷新,刷新会导致我会丢失item_id

if(isset($_POST['submit_edit_item_id']))	{		$_SESSION['edit_item_id'] = $_POST['sub_edit_item_id'];	}		$item_id = $_SESSION['edit_item_id'];


所以把item_id存在了SESSION['edit_item_id']里,如果item_id不是之前网页传递过来的,就直接读取存在session里的item_id,方便刷新页面以后继续使用。 至于session_start()我放在了header.php里,抱歉我没有做相关的说明。

小弟刚学网页不久,也不知道这种思路是不是正确。话说item_id,我去检查一下,是不是为空。。。

$pic_id = mysql_insert_id();
mysql_query("UPDATE ITEM SET IMG_ID = $pic_id WHERE ITEM_ID = $item_id");

既然 IMG_ID 是自动增长的, 那么 IMG_ID 就是主键了
主键是不可重复的!
SET IMG_ID = $pic_id 就有重复的嫌疑

至少在你的代码中没有看到对 ITEM_ID 赋值的代码
而有
$item_id = $_SESSION['eidt_item_id'];
但是也没有看到 session_start();

虽说 ITEM_ID 可能有初值,但 $item_id 为空的话将造成 SQL 指令为
UPDATE ITEM SET IMG_ID = nnnn WHERE ITEM_ID = 
显然是错误的

估计是列名弄混了

img_id在IMAGES里确实是主键,但在ITEM表里也有一个,不是主键。
我做了下测试,如果我删除图片,ITEM表里的img_id就会变成1,即为默认图片。如果我再换图片,没有新的图片插入进去,相对的img_id会变成0。。。可能是应为没有插进去图片,所以mysql_insert_id()返回的值为0吧?
为了以防万一,我在mysql里测试了一下insert语句,发现没有语法问题啊。。。 为什么会这样啊。。。 要抓狂了。。。

对代码稍微做了点修改。。。之前还能插入图片。。。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><?php 	require_once 'header.php'; 	require_once 'check.php';?><meta content="en-us" http-equiv="Content-Language" /><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>Item Detail</title><style type="text/css">.auto-style2 {	text-align: center;}.auto-style1 {	font-size: 36pt;	text-align: left;	font-family: Default;}.auto-style3 {	font-family: Broadway;}</style></head><body><?php	if(isset($_POST['submit_edit_item_id']))	{		$_SESSION['edit_item_id'] = $_POST['sub_edit_item_id'];	}		$item_id = $_SESSION['edit_item_id'];			$result = mysql_query("SELECT * FROM ITEM WHERE ITEM_ID = $item_id");		if($result)	{		$row = mysql_fetch_array($result);				$user_id = $_SESSION['id'];		$name = $row['NAME'];		$dsc = $row['DESCRIPTION'];		$auc = $row['RESERVE_PRICE'];		$bin = $row['BIN_PRICE'];		$btime = $row['BEGIN'];		$etime = $row['END'];		$status = $row['STATUS'];		$seller = $row['SELLER_ID'];		$winner = $row['WINNER_ID'];		$curr_bid = $row['CURR_BID'];		$num_bid = $row['NUM_BID'];		$img_id = $row['IMG_ID'];				echo "item_id is $item_id";		echo "img_id is $img_id";	}		$result = mysql_query("SELECT USER_NAME FROM USER WHERE USER_ID = $seller");	if($result)	{		$row = mysql_fetch_array($result);		$seller_name = $row['USER_NAME'];	}?>		<table align="center" style="width: 100%">			<tr>				<td>				<div class="auto-style2">					<table align="center" style="width: 100%">						<tr>							<td class="auto-style1">Edit item:</td>							<td>							<a href="http://acadweb1.salisbury.edu/~rwan/home.php">							<img     style="max-width:90%" src="logo.jpg" style="float: right"  style="max-width:90%" / alt="请大侠们帮我看看这个问题出在哪?" ></a></td>						</tr>					</table>				</div>				<hr />				<table style="width: 100%">					<tr>						<td rowspan="2">						<?php						display_image($img_id, 500, 400);						?>												<form action="edit_item.php" method="post" enctype="multipart/form-data">						<table><tr><td><input type="file" name="image" /></td></tr>						<tr><td><input name="sub_change_pic" type="submit" value="Change picture">						<input name="sub_del_pic" type="submit" value="Delete picture"></td></tr>						</table></form>												<?php						if(isset($_POST['sub_change_pic']))						{							if(isset($_FILES['image']))							{								$file = $_FILES['image']['tmp_name'];								if(!empty($file))								{									$image = addslashes(file_get_contents($file));									$image_name = addslashes($_FILES['image']['name']);									$image_size = getimagesize($file);																									if($image_size == false)									{										print'<p class = "error">The file is not an image.</p>';									}																	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']);														}						?>											</td>						<td><form action="edit_item.php" method="post">						<textarea cols="50" name="title" rows="4"><?php print $name; ?></textarea><br>						<input name="change_item_title" type="submit" value="Change item title" /></form></td>											</tr>										<tr>						<td><hr />										<?php						if(isset($_POST['change_item_title']))					{						$name = $_POST['title'];						mysql_query("LOCK TABLES ITEM WRITE");						mysql_query("UPDATE ITEM SET NAME = '$name' WHERE ITEM_ID = $item_id");						mysql_query("UNLOCK TABLES");						header("location: " . $_SERVER['REQUEST_URI']);						}																if($status == "SOLD")					{						$result = mysql_query("SELECT * FROM ITEM_SOLD WHERE ITEM_ID = $item_id");						$row = mysql_fetch_array($result);						$buyer = $row['BUYER_ID'];												if($_SESSION['id'] == $buyer)						{							print'<p class = "error">Congratulations! The item is yours.</p>';						}						else						{							print'<p class = "error">Sorry, the item has been sold!</p>';						}					}					else if($status == "NA")					{						print'<p class = "error">Item is not available yet!</p>';					}					else if($status == "EXPIRE")					{						print'<p class = "error">Item is expired.</p>';					}					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"<p><strong>Current datetime: $curr_datetime</strong></p>";												$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'<table><tr><td><form action="edit_item.php" method="post">						  Change End time:</td></tr><tr><td><input type="date" name="end_date" value='.$date.'>						  <input type="time" name="end_time" value='.$time.'></td>						  <td><input type="submit" name= "go" value="Set Time">						</form></td></tr></table>';						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();					}										?>										<hr /><br />															<table><tr><td>Buy it Now Price:</td></tr>						<tr><td><form action ="edit_item.php" method="post">						<input name="price" type="text" value="<?php print $bin; ?>" ></td></tr>						<tr><td><input name="sub_change_price" type="submit" value="Change price" /></form></td></tr></table>												<?php						if(isset($_POST['sub_change_price']))						{							$price = $_POST['price'];							mysql_query("LOCK TABLES ITEM WRITE");							mysql_query("UPDATE ITEM SET BIN_PRICE = $price WHERE ITEM_ID = $item_id");							mysql_query("UNLOCK TABLES");							header("location: " . $_SERVER['REQUEST_URI']);							}					?>											</td>						</tr>						<tr>							<td class="auto-style3" colspan="2">							<form action ="edit_item.php" method="post">							<textarea cols="80" name="description" rows="10"><?php print $dsc; ?></textarea><br>							<input name="change_dsc" type="submit" value="Change item description" /></form></td>						</tr>											<?php					if(isset($_POST['change_dsc']))					{						$dsc = $_POST['description'];						mysql_query("LOCK TABLES ITEM WRITE");						mysql_query("UPDATE ITEM SET DESCRIPTION = '$dsc' WHERE ITEM_ID = $item_id");						mysql_query("UNLOCK TABLES");						header("location: " . $_SERVER['REQUEST_URI']);						}					?>					</table>					</td>				</tr>			</table>			</body><?php require_once 'webmaster.php'; ?></html>



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.