dongqi8030 2013-05-18 13:24
浏览 21
已采纳

为什么不能使用php更新数据库表中的数据? [关闭]

Here is my php code.In this code if the user edits his/her information and clicks the update button, the posted values must be updated in database side.My table name for user is users.But when I want to post some values just the name is updated not the others.And also the date is changed to 0000-00-00.Why does this happen?I couldn't understand.Any idea and reply will be helpful?

<?php
if(isset($_POST['update']))
{

 include("db.php");

$name = $_POST['name'];

$password1 = md5($_POST['password1']);
$password2 = md5($_POST['password2']);
$date_of_birth = $_POST['date_of_birth'];
$place_of_birth = $_POST['place_of_birth'];
$info = $_POST['info'];
$nationality = $_POST['nationality'];

   echo $_POST['name'];
   echo $name;
   echo $date_of_birth;
   echo $info;
   echo $place_of_birth;
   echo $nationality;

if ($password1 != $password2) {
    include "src/header.php";
    include "src/mainmenu.php";
    echo '<p>Error: password does not match. Try again</a>';
    echo '<p><a href="EditProfile.php">Try again</p>';
    include "src/footer.php";
    exit;
}
//If the name and the other fields are empty
if($name=='' || $email=='' || $password1=='' || $password2=='' || $date_of_birth==       
    ''|| $place_of_birth==''|| $info=='' || $nationality=='' ){
    include "src/header.php";
    include "src/mainmenu.php";
    echo '<p>Error:You didn\'t fill the fields.Try again</a>';
    echo '<p><a href="EditProfile.php">Try again</p>';
    include "src/footer.php";
    exit;
}


   $email=$_SESSION['email'];

   $sql = "UPDATE users SET name='".mysql_real_escape_string($name)."',
                     info=     
   '".mysql_real_escape_string($info)."',

   password=".mysql_real_escape_string($password2)."'

   place_of_birth='".mysql_real_escape_string($place_of_birth)."',

   date_of_birth='".mysql_real_escape_string($date_of_birth)."', 

   nationality='".mysql_real_escape_string($nationality)."'
                     WHERE email ='$email'";


  $retval = mysql_query($sql,$link);



  if (!$retval|| $retval==false) {
    include "src/header.php";
    include "src/mainmenu.php";
    die('Could not update data: ' . mysql_error());
    echo '<p><a href="EditProfile.php">Try again</a></p>';
    include "src/footer.php";
    mysql_close($link);
    exit;
}
else {
    echo "Updated data successfully
";

    //header('Location: private.php');
}

  mysql_close($link);
  }
 else
 {
 include("db.php"); 

 $email=$_SESSION['email'];
 $run = mysql_query("select * from users where email='$email'") or die("Error!");
 $read = mysql_fetch_assoc($run);

 ?>
 <form method="post" action="<?php $_PHP_SELF ?>">
<fieldset>
<legend>Update Profile</legend>
<p>
    <label for="name">Full name:</label> <input type="text" name="name"     
 id="name" value="<?PHP echo $read['name']; ?>"/> 
<br>
    <label for="email">Email:</label> <input type="email" name="email"   
 id="email" value="<?PHP echo $read['email']; ?>"/> 
<br>
    <label for="password1">Password:</label> <input type="password"    
 name="password1" id="password1" />
<br>
    <label for="password2">Confirm password:</label> <input type="password"  
 name="password2" id="password2" />
<br>
    <label for="date_of_birth">Date of birth (yyyy-mm-dd):</label> <input  
type="date" name="date_of_birth" id="date_of_birth"  value="<?PHP echo 
$read['date_of_birth']; ?>"/>
<br>
    <label for="place_of_birth">Place of birth:</label> <input type="text"  
 name="place_of_birth" id="place_of_birth" value="<?PHP echo $read['place_of_birth']; ?
 >"/>
<br>
    <label for="info">Information:</label> <textarea name="info" id="info" 
rows="5" cols="50" ></textarea>
<br>
    <label for="nationality">Nationality:</label> <input type="text"    ,
name="nationality" id="nationality" value="<?PHP echo $read['nationality']; ?>"/>
</p>

<p class="center"><input value="Update" type="submit" name="update" id="update"/>  
 </p>
</fieldset>
</form>
<?php

}
?>
  • 写回答

2条回答 默认 最新

  • dqthn68688 2013-05-18 13:31
    关注

    your code wrong in here;

    password=".mysql_real_escape_string($password2)."'
    

    change with this;

    password='".mysql_real_escape_string($password2)."',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))