首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

请大侠们帮小弟我看看这个有关问题出在哪

2013-07-09 
请大侠们帮我看看这个问题出在哪?代码如下!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//E

请大侠们帮我看看这个问题出在哪?
代码如下


<!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 height="89" src="logo.jpg" style="float: right" width="184" /></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 = 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']);

}
?>

</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了,我自己应该还是有权限修改的不是吗?小弟百思不得其解

麻烦各位大神帮小弟分析一下原因,小弟感激不尽
MySQL php
[解决办法]
$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 = 
显然是错误的

估计是列名弄混了

热点排行