dongmu5815 2014-09-13 21:06
浏览 23

我无法用PHP更新MySQL数据库

I am trying to update the mysql database using PHP but somehow I am not able to do the same. It echos the message that it is successfully updated but it doesn't update it. Please help. Please check the code and tell me the correction: First I am showing the form then the PHP code

<?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>

Now starts the php code:

<?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条回答 默认 最新

  • dongmou5628 2014-09-13 22:32
    关注

    The likely problem is with line:

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

    I didn't see $res defined anywhere.

    You probably want:

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

    Also I recommend studying this: How can I prevent SQL injection in PHP?

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题