更新查询不起作用并将字段更改为0

时间:2015-05-14 08:56:12

标签: php mysql

我有这个表单代码。

<form action="save_profile.php" method="post">
          <table>
            <tr><td>First Name:</td><td><input type="text" name="fname" value="<?php echo $fname;?>"></td></tr>
            <tr><td>Last Name:</td><td><input type="text" name="lname" value="<?php echo $lname;?>"></td></tr>
            <tr><td>Mail ID:</td><td><input type="text" name="mail" value="<?php echo $mail;?>"></td></tr>
            <tr><td>Contact NO:</td><td><input type="text" name="contact" value="<?php echo $contact;?>"></td></tr>
            <tr><td></td><td><input type="submit" name="submit" value="Save Profile"></td></tr>
          </table>
          </form>

我的save_profile.php文件包含此代码。

<?php
session_start();
require('config.php');
function is_valid_fname($fname,$lname,$contact)
{
 if (empty($fname)) {
     ?>
     <center><strong><font color="red">First Name is required.</font></strong></center>
     <?php
     return false;
 }
 if ( !preg_match ("/^[a-zA-Z\s]+$/",$fname)) {
     ?>
     <center><strong><font color="red">First Name Only Contain Letters.</font></strong></center>
     <?php
     return false;
 } 
 if (strlen($fname)>20) {
     ?>
     <center><strong><font color="red">First Name must be less than 20 Letters.</font></strong></center>
     <?php
     return false;
 }
 if (empty($lname)) {
     ?>
     <center><strong><font color="red">Last Name is required.</font></strong></center>
     <?php
     return false;
 }
 if ( !preg_match ("/^[a-zA-Z\s]+$/",$lname)) {
     ?>
     <center><strong><font color="red">Last Name Only Contain Letters.</font></strong></center>
     <?php
     return false;
 } 
 if (strlen($lname)>20) {
     ?>
     <center><strong><font color="red">Last Name must be less than 20 Letters.</font></strong></center>
     <?php
     return false;
 }
 if (empty($contact)) {
     ?>
     <center><strong><font color="red">Contact is required.</font></strong></center>
     <?php
     return false;
 }
 if ( !preg_match ("/^[0-9\s]+$/",$contact)) {
     ?>
     <center><strong><font color="red">Contact Only Contain Numbers.</font></strong></center>
     <?php
     return false;
 }
 if (strlen($contact)>15) {
     ?>
     <center><strong><font color="red">Contact must be less than 20 Digits.</font></strong></center>
     <?php
     return false;
 } 
 else{
  return true;
 }
}
function is_valid_email($mail)
{
 if (empty($mail)) {
     ?>
     <center><strong><font color="red">Email is required.</font></strong></center>
     <?php
     return false;
 } else {
     $email = test_input($mail);
     // check if e-mail address is well-formed
     if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
       ?>
     <center><strong><font color="red">Invalid Email Format.</font></strong></center>
     <?php 
       return false;
 } 
 // now check if the mail is already registered
 $slquery = "SELECT 1 FROM user_tbl WHERE user_mail = '".$mail."'";
 $selectresult = mysql_query($slquery);
 if(mysql_num_rows($selectresult)>1) {
   ?>
     <center><strong><font color="red">This Email Is Already Exits.</font></strong></center>
     <?php
   return false;
 }
 // now returns the true- means you can proceed with this mail
 return true;
}
}
if (isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['mail'];
$contact = $_POST['contact'];
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (is_valid_email($mail) && is_valid_fname($fname,$lname,$contact))
{
    $query="UPDATE user_tbl SET user_fname='".$fname."' AND user_lname='".$lname."' AND user_mail='".$mail."' AND user_contact='".$contact."' WHERE user_id='".$_SESSION['uid']."'";
     $result = mysql_query($query);
     if ($result) {
         header('location:profile.php');
     }
}
else{
      ?>
     <center><strong><font color="red">Error Updating User.</font></strong></center>
     <?php
    }
}
?>

我的问题是在我更改表单数据后以表格形式显示数据时,当我提交表单时,它将始终更新我的user_fname = 0 ...当我更改其他字段时,我的其他字段保持相同并设置user_fname = 0.please帮助我...

2 个答案:

答案 0 :(得分:2)

请使用逗号(,)代替

$query="UPDATE user_tbl SET user_fname='".$fname."', user_lname='".$lname."', user_mail='".$mail."', user_contact='".$contact."' WHERE user_id='".$_SESSION['uid']."'";

答案 1 :(得分:0)

  "SELECT `1` FROM `user_tbl` WHERE `user_mail` = '".$mail."'"; 
  "UPDATE `user_tbl` SET `user_fname`='".$fname."' AND `user_lname`='".$lname."'AND
  `user_mail`='".$mail."' AND `user_contact`='".$contact."' WHERE 
  `user_id`='".$_SESSION['uid']."'";

你忘记了严肃的口音。现在检查是否仍然有效。