编辑数据库PHP html

时间:2013-03-14 14:45:59

标签: php html mysql database

我有一个名为Stock的数据库,到目前为止,我可以打开我的view.php并查看该表,并使用我的insert.php文件向该表添加行

我也添加了编辑和删除,但是,我在下面看到的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!';
}
}
?>

1 个答案:

答案 0 :(得分:1)

此行中有错误

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

应该是

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

您在使用其他两个

else声明中也有错误

应该是

 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!';
 }

 ?>