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

标签 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。请帮帮我...

最佳答案

请使用逗号(,)代替

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

关于php - 更新查询不起作用并将字段更改为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30233150/

相关文章:

javascript - 如何使用 javascript 通过验证 cookie 存储值和 ajax 响应来尝试 3 次登录表单

mysql - 如何删除非空约束?

php - 为什么尽管已成功将文件移动到 NFS 挂载磁盘,但 rename() 仍返回 false?

php - 使用像 filter_var_array() 这样的 PHP 过滤器函数有没有办法检查输入字符串的长度是否小于某个值

mysql - SELECT with Limit in SQL 搞乱了行的顺序

mysql - 在 MySQL 查询中包含两个不同的表

php - 标记未显示在 map 上

mysql - 使用 DetachedCriteria Hibernate 多个子查询

php - 仅当 Laravel eloquent 中有值时才查询 "Where"

php - 我需要用什么来从字符串中取出 2 个单词?正则表达式还是 explode ?