2011-04-27 56 views
0

我試圖解決此代碼,我得到這個錯誤,我不知道如何解決它:未定義的變量:RESULT1在plainview.php在線44php error未定義的變量:result1 help!

這裏是代碼:

// 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); 

//error reporting 
error_reporting(E_ALL); ini_set('display_errors', '1'); 


//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> 



</body> 
</html> 

請幫忙!我是一個業餘的PHP和任何幫助,將不勝感激....

回答

2

$result1只是設置一個for循環,在if語句內。如果if失敗,它從未定義,所以if($result1)給出該錯誤。在if語句之前設置$ result1 = false。更好的做法是,移動if語句以環繞整個代碼塊,包括數據庫連接和斷開連接。

+0

感謝您的幫助! – Peter 2011-04-27 19:54:54

0

在第44行$結果是不確定的,如果

if (isset($_POST['Submit'])) { 
... 
$result1 = mysql_query($sql1) or die(mysql_error()); 
} 

偏轉的塊,因爲你沒有張貼的請求或$ _POST [「提交」]沒有定義。

使用默認值FALSE預設$ result1。

0

如果通過POST操作調用腳本,則只定義$ result1。因此,

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

將在您不處於POST狀態時發出該警告。

另外還有一點需要注意的是,在您獲取行來生成HTML之前,您正在關閉mysql連接。這會殺死你的頁面輸出,因爲在關閉連接時結果還沒有被「提取」。

+0

我已經更新了頁面,現在它只是失敗,我認爲這是因爲我沒有設置任何地方的$ id – Peter 2011-04-27 19:59:25