2011-04-27 92 views
1

任何人都可以看看這段代碼,並告訴我爲什麼它不更新數據庫?我知道它砍死在一起,我在PHP新手提前PHP和MySQL不更新數據庫

<?php 
// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 


$sql="SELECT * FROM $tbl_name WHERE depot = 'plainview'"; 
$result=mysql_query($sql); 

// Count table rows 
$count=mysql_num_rows($result); 

//update 
if(isset($_POST['Submit'])){ 
for($i=0;$i<$count;$i++){ 

$sql1 = "UPDATE $tbl_name SET 

    available='{$_POST['available'][$i]}', 
    rent='{$_POST['rent'][$i]}', 
    corp_ready='{$_POST['corp_ready'][$i]}', 
    down='{$_POST['down'][$i]}', 
    gfs='{$_POST['gfs'][$i]}', 
    dateTime = NOW() 

WHERE id='$id[$i]'"; 


$result1 = mysql_query($sql1) or die(mysql_error()); 
} 
} 

//redirect 
if($result1){ 
header("location: plainview.php"); 
} 



mysql_close(); 
?> 

======================

感謝

整個代碼

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 


$sql="SELECT * FROM $tbl_name WHERE depot = 'plainview'"; 
$result=mysql_query($sql); 

// Count table rows 
$count=mysql_num_rows($result); 

//update 
if(isset($_POST['Submit'])){ 
for($i=0;$i<$count;$i++){ 

$sql1 = "UPDATE $tbl_name SET 

available='".mysql_real_escape_string($_POST['available'][$i])."', 
rent='".mysql_real_escape_string($_POST['rent'][$i])."', 
corp_ready='".mysql_real_escape_string($_POST['corp_ready'][$i])."', 
down='".mysql_real_escape_string($_POST['down'][$i])."', 
gfs='".mysql_real_escape_string($_POST['gfs'][$i])."', 
dateTime = NOW() 
WHERE id='".$id[$i]."'"; 



$result1 = mysql_query($sql1) or die(mysql_error()); 
} 
} 

//redirect 
if($result1){ 
header("location: plainview.php"); 
} 




mysql_close(); 
?> 



<!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> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<script language="JavaScript1.1" type="text/javascript"> 
<!-- 
function mm_jumpmenu(targ,selObj,restore){ //v3.0 
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); 
    if (restore) selObj.selectedIndex=0; 
} 
//--> 
</script> 
<title>Untitled Document</title> 
</head> 

<body> 

<div> 
    <p>Plainview, North East Region</p> 
    <p>Select a different region: <select onchange="mm_jumpmenu('parent',this,0)" name="lostlist"> 
       <option value="" selected="selected">Choose Your Depot</option> 
       <option value="plainview.php">Plainview</option> 
       <option value="worcrester.php">Worcrester</option> 

       </select></p> 
</div><Br /> 

<table width="500" border="0" cellspacing="1" cellpadding="0"> 
<form name="form1" method="post" action=""> 
<tr> 
<td> 
<table width="700" border="0" cellspacing="1" cellpadding="0"> 

<tr> 
<td>ID</td> 
<td align="center"><strong>Product Name</strong></td> 
<td align="center"><strong>Available</strong></td> 
<td align="center"><strong>Rent</strong></td> 
<td align="center"><strong>Corp Ready</strong></td> 
<td align="center"><strong>Down</strong></td> 
<td align="center"><strong>GFS</strong></td> 
</tr> 
<?php 
while($rows=mysql_fetch_array($result)){ 
?> 
<tr> 
<td align="left"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td> 

<td align="left"><?php echo $rows['product']; ?></td> 
<td align="center"><input name="available[]" type="text" id="available" value="<?php echo $rows['available']; ?>" size="5"></td> 
<td align="center"><input name="rent[]" type="text" id="rent" value="<?php echo $rows['rent']; ?>" size="5"></td> 
<td align="center"><input name="corp_ready[]" type="text" id="corp_ready" value="<?php echo $rows['corp_ready']; ?>" size="5"></td> 
<td align="center"><input name="down[]" type="text" id="down" value="<?php echo $rows['down']; ?>" size="5" /></td> 
<td align="center"><input name="gfs[]" type="text" id="gfs" value="<?php echo $rows['gfs']; ?>" size="5"></td> 

</tr> 
<?php 
} 
?> 
<tr> 
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> 
</tr> 
</table> 
</td> 
</tr> 
</form> 
</table> 
<?php 

echo "$sql1"; 

?> 


</body> 
</html> 
+1

echo $ sql1返回什麼,你得到什麼錯誤消息?你在哪裏分配$ tbl_name? – Pentium10 2011-04-27 18:19:48

+0

你好鮑比表和錯誤處理不足.. – 2011-04-27 18:20:53

+0

'echo mysql_error();'說。 – Gaurav 2011-04-27 18:20:57

回答

2

總是逃避所有用戶輸入。您更新查詢應該像

$sql1 = "UPDATE $tbl_name SET 

available='".mysql_real_escape_string($_POST['available'][$i])."', 
rent='".mysql_real_escape_string($_POST['rent'][$i])."', 
corp_ready='".mysql_real_escape_string($_POST['corp_ready'][$i])."', 
down='".mysql_real_escape_string($_POST['down'][$i])."', 
gfs='".mysql_real_escape_string($_POST['gfs'][$i])."', 
dateTime = NOW() 
WHERE id='".$id[$i]."'"; 
+0

內設置謝謝我將使用此更新我的代碼 – Peter 2011-04-27 18:27:11

+0

@Peter:另外,檢查id是否等於'$ id [$ i]'的記錄確實存在於您的表中。如果沒有,沒有任何變化,也沒有錯誤發生。 – a1ex07 2011-04-27 18:30:19

2

$tbl_name設定的代碼之外您提供?如果沒有,請設置它,以便您的sql知道要使用哪個表。

+0

是的,它在連接設置 – Peter 2011-04-27 18:21:50

4

嘗試echo "id='$id[$i]'<br />";,看看你得到的ID應該被髮送。

+0

不迴應任何身份證 – Peter 2011-04-27 18:29:35

+0

我的意思是隻有echos id ='' – Peter 2011-04-27 18:30:11

+0

那麼您可能無法更改身份證你不給 – webLacky3rdClass 2011-04-27 18:31:11