2013-03-14 122 views
-2

我有一個叫做股票表DATABSE,到目前爲止,我可以打開我的view.php,看看錶,並與我insert.php文件添加行到該表編輯數據庫PHP HTML

我也已經添加了編輯和刪除,但是,我可以在下面看到的Edit.php文件不允許我編輯表格中的任何數據。我似乎無法找出問題的一些幫助將不勝感激。

<?php 
function valid($id, $ProductName, $Quantity,$Price, $error) 
{ 
?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 

<head> 
<title>Edit Records</title> 
</head> 

<body> 
<?php 

if ($error != '') 
{ 
    echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 

<form action="" method="post"> 


<table border="1"> 
<tr> 
<td colspan="2"><b><font color='Red'>Edit Records </font></b></td> 
</tr> 

<tr> 
<td width="179"><b><font color='#663300'>Product Name<em>*</em></font></b></td> 
<td><label> 
<input type="text" name="ProductName" value="<?php echo $ProductName; ?>" /> 
</label></td> 
</tr> 

<tr> 
<td width="179"><b><font color='#663300'>Quantity<em>*</em></font></b></td> 
<td><label> 
<input type="text" name="Quantity" value="<?php echo $Quantity; ?>" /> 
</label></td> 
</tr> 

<tr> 
<td width="179"><b><font color='#663300'>City<em>*</em></font></b></td> 
<td><label> 
<input type="text" name="Price" value="<?php echo $Price; ?>" /> 
</label></td> 
</tr> 

<tr align="Right"> 
<td colspan="2"><label> 
<input type="submit" name="submit" value="Edit Records"> 
</label></td> 
</tr> 
</table> 
</form> 
</body> 

</html> 
<?php 
} 

include('config.php'); 

if (isset($_POST['submit'])) 
{ 

$ProductName = mysql_real_escape_string(htmlspecialchars($_POST['ProductName'])); 
$Quantity = mysql_real_escape_string(htmlspecialchars($_POST['Quantity'])); 
$Price = mysql_real_escape_string(htmlspecialchars($_POST['Price'])); 


if ($ProductName == '' || $Quantity == '' || $Price == '') 
{ 

$error = 'ERROR: Please fill in all required fields!'; 


valid($id, $ProductName, $Quantity,$Price, $error); 
} 
else 
{ 

mysql_query("UPDATE Stock SET ProductName='$ProductName', Quantity='$Quantity' ,Price='$Price' ") 
or die(mysql_error()); 

header("Location: view.php"); 
} 
} 
else 
{ 

echo 'Error!'; 
} 


if($row) 
{ 

$ProductName = $row['ProductName']; 
$Quantity = $row['Quantity']; 
$Price = $row['Price']; 

valid($ProductName, $Quantity,$Price,); 
} 
else 
{ 
echo "No results!"; 
} 
} 
else 

{ 
echo 'Error!'; 
} 
} 
?> 
+0

有使用了'mysql_query'任何理由? – tadman 2013-03-14 14:49:00

+0

如果可能,我強烈建議轉換爲mysqli或PDO,因爲mysqli函數很快就會被棄用。請參閱[這裏](http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/)瞭解更多關於這兩者的信息。 – 2013-03-14 14:53:06

+0

「UPDATE」命令中沒有'WHERE'語句? – UnholyRanger 2013-03-14 14:53:09

回答

1

你有一個錯誤在這行

valid($ProductName, $Quantity,$Price,); 

應該

valid($ProductName, $Quantity,$Price,$error); 

你有一個錯誤也else statment使用兩個其他

它應該be

if(..... 

else if (.... 

else { ... 

編輯:

試試這個

<?php 
    function valid($id, $ProductName, $Quantity,$Price, $error) 
    { 
    ?> 

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 

<head> 
<title>Edit Records</title> 
</head> 

<body> 
<?php 

if ($error != '') 
{ 
    echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 

<form action="" method="post"> 


<table border="1"> 
<tr> 
<td colspan="2"><b><font color='Red'>Edit Records </font></b></td> 
</tr> 

<tr> 
<td width="179"><b><font color='#663300'>Product Name<em>*</em></font></b></td> 
<td><label> 
<input type="text" name="ProductName" value="<?php echo $ProductName; ?>" /> 
</label></td> 
</tr> 

<tr> 
<td width="179"><b><font color='#663300'>Quantity<em>*</em></font></b></td> 
<td><label> 
<input type="text" name="Quantity" value="<?php echo $Quantity; ?>" /> 
</label></td> 
</tr> 

<tr> 
<td width="179"><b><font color='#663300'>City<em>*</em></font></b></td> 
<td><label> 
<input type="text" name="Price" value="<?php echo $Price; ?>" /> 
</label></td> 
</tr> 

<tr align="Right"> 
<td colspan="2"><label> 
<input type="submit" name="submit" value="Edit Records"> 
</label></td> 
</tr> 
</table> 
</form> 
</body> 

</html> 
<?php 
} 

include('config.php'); 

if (isset($_POST['submit'])) 
{ 

$ProductName = mysql_real_escape_string(htmlspecialchars($_POST['ProductName'])); 
$Quantity = mysql_real_escape_string(htmlspecialchars($_POST['Quantity'])); 
$Price = mysql_real_escape_string(htmlspecialchars($_POST['Price'])); 


if ($ProductName == '' || $Quantity == '' || $Price == '') 
{ 

$error = 'ERROR: Please fill in all required fields!'; 


valid($id, $ProductName, $Quantity,$Price, $error); 
} 
else 
{ 

mysql_query("UPDATE Stock SET ProductName='$ProductName', Quantity='$Quantity' ,Price='$Price' ") 
or die(mysql_error()); 

header("Location: view.php"); 
} 
} 
else 
{ 

echo 'Error!'; 
} 

$query = mysql_query(" select * from Stock"); 
$row = mysql_fetch_array($query) ; 


if (mysql_num_rows($query) == 0) 
{ 
echo "No results!"; 
} 

else if (mysql_num_rows($query) == 1) 
    { 

$ProductName = $row['ProductName']; 
    $Quantity = $row['Quantity']; 
    $Price = $row['Price']; 
valid($ProductName, $Quantity,$Price,$error); 
    echo "echo what you like here". 
} 
else 
{ 
echo 'Error!'; 
} 

?> 
+0

解析錯誤:語法錯誤,意外的T_ECHO在/home/k1012904/www/EditDatabase/edit.php 108行....這是我查看時看到的。這段代碼看起來像這樣:if($ row) { $ ProductName = $ row ['ProductName']; $ Quantity = $ row ['Quantity']; $ Price = $ row ['Price']; 有效($ ProductName,$ Quantity,$ Price,$ error); } else if { echo「No results!」; } }} 其他 { 回聲 '錯誤!'; } } ?> – Jay240692 2013-03-14 15:14:20

+0

試試我編輯的答案。 – 2013-03-14 15:33:37