2013-10-15 44 views
0

我創建了表單以顯示產品列表以及每個產品的兩個編輯和刪除鏈接。我收到與mysqli有關的錯誤

請參閱的PHP代碼list_recorder.php頁...

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="xxx"; // Mysql password 
$db_name="shopone"; // Database name 
$tbl_name="product"; // Table name 

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

$sql="SELECT * FROM product"; 
$result=mysql_query($sql); 
?> 

<table width="400" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<td> 
<table width="400" border="1" cellspacing="0" cellpadding="3"> 
<tr> 
<td colspan="4"><strong>List data from mysql </strong> </td> 
</tr> 

<tr> 
<td align="center"><strong>product name</strong></td> 
<td align="center"><strong>category</strong></td> 
<td align="center"><strong>retial price</strong></td> 
<td align="center"><strong>price</strong></td> 
<td align="center"><strong>detail</strong></td> 
</tr> 

<?php 
while($rows=mysql_fetch_array($result)){ 
?> 

<tr> 
<td><?php echo $rows['product_name']; ?></td> 
<td><?php echo $rows['product_category']; ?></td> 
<td><?php echo $rows['product_retail_price']; ?></td> 
<td><?php echo $rows['product_price']; ?></td> 
<td><?php echo $rows['product_detail']; ?></td> 


<td align="center"><a href="update.php?product_id=<? echo $rows['product_id']; ?>">update</a></td> 
</tr> 

<?php 
} 
?> 

</table> 
</td> 
</tr> 
</table> 

<?php 
mysql_close(); 
?> 

此列表中的偉大工程,現在我的下一個步驟是對編輯工作,讓我改變產品信息..

請參閱編輯代碼頁被稱爲update.php頁

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="xxx"; // Mysql password 
$db_name="shopone"; // Database name 
$tbl_name="product"; // Table name 

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

$sql="SELECT * FROM product"; 
$result=mysql_query($sql); 
?> 

<table width="400" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<td> 
<table width="400" border="1" cellspacing="0" cellpadding="3"> 
<tr> 
<td colspan="4"><strong>List data from mysql </strong> </td> 
</tr> 

<tr> 
<td align="center"><strong>product name</strong></td> 
<td align="center"><strong>category</strong></td> 
<td align="center"><strong>retial price</strong></td> 
<td align="center"><strong>price</strong></td> 
<td align="center"><strong>detail</strong></td> 
</tr> 

<?php 
while($rows=mysql_fetch_array($result)){ 
?> 

<tr> 
<td><?php echo $rows['product_name']; ?></td> 
<td><?php echo $rows['product_category']; ?></td> 
<td><?php echo $rows['product_retail_price']; ?></td> 
<td><?php echo $rows['product_price']; ?></td> 
<td><?php echo $rows['product_detail']; ?></td> 


<td align="center"><a href="update.php?product_id=<? echo $rows['product_id']; ?>">update</a></td> 
</tr> 

<?php 
} 
?> 

</table> 
</td> 
</tr> 
</table> 

<?php 
mysql_close(); 
?> 

當我運行list_recorder.php頁面,我按編輯的一個環節,從列表中,我得到的錯誤它說

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\VertrigoServ\www\shopone\admin\cd\update.php on line 19 

這與$ rows = mysql_fetch_array($ result)有關;我不確定這個錯誤意味着什麼,我該如何解決這個問題!

請幫忙謝謝。

AM

回答

0

請嘗試以下操作,每一個你傳遞一個$結果:

$result=mysql_query($sql) or die($sql."<br/><br/>".mysql_error()); 

這將提供一個錯誤,這將導致你到一個可能的解決方案。

再看看這行:

<td align="center"><a href="update.php?product_id=<? echo $rows['product_id']; ?>">update</a></td> 

變化開始標記<?php,並確保product_id是在數據庫中的有效列字段。

+0

感謝這麼多,我改變了$結果行給我提供更多的錯誤信息,而且我得到了更多的錯誤信息,但不知道這是什麼意思是說:「 SELECT * FROM產品WHERE的product_id =‘’ 你在你的SQL語法中有錯誤;查看與您的MySQL服務器版本相對應的手冊,以找到在'product_id'附近使用的正確語法; '>'在第1行....... 任何想法! – andy

+0

我加入到答案。 –

+0

我已經解決了,並沒有再顯示錯誤,但我顯示每個文本字段與<?echo $行['product_price'];?>在文本字段內部,我甚至添加了php,如<?php echo $ rows ['product_price'];?>但它有更多的錯誤。意思是如何解決這個問題!而康拉德·洛茲非常感謝幫助解決了第一個錯誤。 – andy

0

嘗試調試您的查詢,手動檢查... becouse可能是您的查詢失敗 ,我建議你在你的腳本變化不大

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("shopone", $con)or die("cannot select DB"); 

嘗試,如果一切都很好,那麼它將確保工作