2013-03-14 91 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