2015-05-14 50 views
1

我有此表單代碼。更新查詢不起作用,並將字段更改爲0

<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.請幫助我...

+2

我不知道原因,只注意到查詢應該是這樣的:UPDATE user_tbl SET user_fname =值,user_lname =價值,...用逗號(,)分隔列名不是AND。 – Darian

回答

3

請在代替用逗號(,)和

$query="UPDATE user_tbl SET user_fname='".$fname."', user_lname='".$lname."', user_mail='".$mail."', user_contact='".$contact."' WHERE user_id='".$_SESSION['uid']."'"; 
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']."'"; 

你忘了重音符。現在檢查是否仍在工作或沒有。