如何验证用户更新配置文件中的输入字段

时间:2014-03-11 06:38:58

标签: php

以下代码是个人资料登录系统更新用户pade:

在下面的代码中,我已经在文本框中有数据(来自数据库的反向数据)。我想验证我是否删除数据并提交:

如果输入字段为空,请帮助我想要验证输入字段的朋友:

<?php
session_start(); // Must start session first thing

// Here we run a login check
if (!isset($_SESSION['id'])) { 
   echo 'Please <a href="login.php">log in</a> to access your account';
   exit(); 
}
//Connect to the database through our include 
include_once "database.php";

// Place Session variable 'id' into local variable
$id = $_SESSION['id'];
// Process the form if it is submitted
if ($_POST['qualific']) {
    $fullname = $_POST['fullname'];
                    $contact = $_POST['contact'];
                    $qualific = $_POST['qualific'];

$sql = mysql_query("UPDATE jobseeker_members SET fullname='$fullname', contact='$contact', qualification='$qualific'  WHERE id='$id'");

 echo 'Your account info has been updated, visitors to your profile will now see the new info.<br /><br />
To return to your profile edit area, <a href="member_account.php">click here</a>';

exit();
} // close if post
?>

<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM jobseeker_members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$fullname = $row["fullname"];
$contact = $row["contact"];
$qualific = $row["qualification"];
}
?>

<form action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data" name="form" id="form" onsubmit="return validate_form ( );">
          <label>FullName:</label>
          <input class="txtbox" name="fullname" type="text" id="fullname"
          value="<?php echo "$fullname"?>"/>

 <label>Contact No:</label>
            <input class="txtbox"name="contact" type="text" id="contact" value="<?php echo "$contact"; ?>" /><br>
 <label>Qualification :</label>
            <select class="txtbox" name = "qualific">
              <option value="<?php echo "$qualific"; ?>"><?php echo "$qualific"; ?></option>
              <option value="Below SSC(10 Std)">Below SSC(10 Std)</option>
              <option value="SSC(10 Std) passed">SSC(10 Std) passed</option>
              <option value="HSC(12 Std) passed">HSC(12 Std) passed</option>
              <option value="Graduate">Graduate</option>
              <option value="Post Graduate">Post Graduate</option>
            </select>
<input  class="submitbtn" name="Submit" type="submit" value="Save" />
</form>

1 个答案:

答案 0 :(得分:0)

只使用多条件IF

if($_POST['fullname'] == '')
{ 
    //value 
}
elseif($_POST['qualific'] == '')
{ 
    //value 
}
and so on ...

如果您想检查数据是否正在发生变化,您可以使用会话来保存数据库中的数据,并使用表单中的数据进行预占。

while($row = mysql_fetch_array($sql)){
    $fullname = $row["fullname"];
    // Place Session variable 'fullname'
    $fullname = $_SESSION[$fullname];

    $contact = $row["contact"];
    // Place Session variable 'contact'
    $contact = $_SESSION[$contact];

    $qualific = $row["qualification"];
    // Place Session variable 'qualification'
    $qualific = $_SESSION[$qualific];
}

比较它:

if($_SESSION['fullname'] !== $_POST['fullname'])
{
    // do something
} elseif($_SESSION['fullname'] !== $_POST['fullname']) {
    // do something
}
and so on...
顺便说一句,对不起我的英文