我无法用PHP更新MySQL数据库

时间:2014-09-13 21:06:43

标签: php mysql

我正在尝试使用PHP更新mysql数据库,但不知怎的,我无法做同样的事情。 它回应了它已成功更新的消息,但它没有更新它。请帮忙。 请检查代码并告诉我更正:首先我显示表单然后是PHP代码

<?php
$id=$_GET['id'];
$sql="select * from members_data where id='$id'";
$data=mysql_query($sql);
$row=mysql_fetch_array($data);
?>

<form method="post" action="edit1.php" name="form1" enctype="multipart/form-data">
<table  width="400" align="left">
<tr>
<td style="width:300px;">
Name
</td>
<td><input placeholder="What's the latest news?" type="text" name="name" style="height:20px;      width:300px;" value="<?php echo $row['name']?>" /></td>
</tr>
<tr>
<td style="width:300px;">
Family Image
</td>

<td width="69%" height="25"><input type="file" name="file" style="height:20px; width:300px;" /></td>
</tr>
<tr>
<td style="width:300px;">
Membership No.
</td>
<td><input placeholder="What's your membership number?" type="text" name="membership_no" style="height:20px; width:300px;" value="<?php echo $row['membership_no']?>" /></td>
</tr>
<tr>
<td style="width:300px;">
Address
</td>
<td><input placeholder="What's the latest news?" type="text" name="address" style="height:20px; width:300px;" value="<?php echo $row['address']?>" /></td>
</tr>
<tr>
<td style="width:300px;">
Contact Number
</td>
<td><input placeholder="What's the latest news?" type="text" name="contact_no" style="height:20px; width:300px;" value="<?php echo $row['contact_no']?>" /></td>
</tr>

<tr>
<td>
<input type="hidden" name="id" value="<?php echo $res['id']?>" />
</td>
<td style="text-align:right; width:300px;"><input type="submit" name="edit1" value="Update" /></td>
</tr>
</table>
</form>

现在启动php代码:

<?php
include("conn.php");
?>
<?php


if(isset($_POST['edit1']))
{
$allowedExts = array("gif", "jpeg", "jpg", "png", "pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    //echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    //echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    //echo "Type: " . $_FILES["file"]["type"] . "<br />";
    //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("images/family_pics/" . $_FILES["file"]["name"]))
      {
      //echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/family_pics/" . $_FILES["file"]["name"]);
      //echo "Stored in: " . "images/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  //echo "Invalid file";
  }
$id=$_POST['id'];
$name=$_POST['name'];
$image=$_FILES["file"]["name"];
$membership_no=$_POST['membership_no'];
$address=$_POST['address'];
$contact_no=$_POST['contact_no'];

$query=mysql_query("update members_data set name='$name',family_image='$image',membership_no='$membership_no',address='$address',contact_no='$contact_no' WHERE id='$id'")  or die(mysql_error());

// if successfully updated. 
if($query){
echo "Successful";
echo "<BR>";
echo "<a href='admin_detail.php'>View result</a>";
}

else {
echo "ERROR";
}

}

?>
<?php /*?><script type="text/javascript">window.location="edit_records.php"</script><?php */?>

1 个答案:

答案 0 :(得分:1)

可能的问题在于行:

<input type="hidden" name="id" value="<?php echo $res['id']?>" />

我没有在任何地方看到$res

你可能想要:

<input type="hidden" name="id" value="<?php echo $row['id']?>" />

我也建议你研究一下:How can I prevent SQL injection in PHP?