2013-11-09 91 views
3

HTML和PHP輸入驗證碼: 「postresume.php」數據不是在數據庫中插入PHP驗證後

<?php 
$firstName = $lastName = $emailId = $phoneNo = $qualification = $dob = $totex = $address=""; 
$firstNameErr = $lastNameErr = $emailIdErr = $phoneNoErr = $qualificationErr = $dobErr = $totexErr = $addressErr = ""; 

if ($_SERVER['REQUEST_METHOD']== "POST") { 
    $valid = true; 

    if (empty($_POST["firstName"])) { 
     $firstNameErr = "*First name is required"; 
     $valid = false; //false 
    } 
    else { 
     $firstName = test_input($_POST["firstName"]); 
    } 

    //LastName Error 
    if (empty($_POST["lastName"])) { 
     $lastNameErr = "*Last name is required"; 
     $valid = false; 
    } 
    else { 
     $lastName = test_input($_POST["lastName"]); 
    } 

    // validation for,email,phoneno,qualification,dob,totex,address will be same as the above 

    if($valid){ 
     echo '<META HTTP-EQUIV="Refresh" Content="0; URL=datasubmitted.php">';  
     exit; 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 

這是我的表在 「postresume.php」:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post"> 
    <div class="label">*First Name: 
     <div class="txtbox"> 
      <input name="firstName" type="text" id="txt" placeholder="Enter Your First Name." value="<?php echo $firstName; ?>"/> 
      <span class="error"><p></p><?php echo $firstNameErr; ?></span> 
     </div> 
    </div> 
    <div class="label">Last Name: 
     <div class="txtbox"> 
      <input name="lastName" type="text" id="txt" placeholder="Enter Your Last Name." value="<?php echo $lastName; ?>"/> 
      <span class="error"><p></p><?php echo $lastNameErr; ?> 
     </div> 
    </div> 
    // email,phoneno,qualification,dob,totex,address will be same as the above 

PHP代碼用於將數據插入到數據庫mysql中:「datasubmitted.php」

<?php 
$con=mysqli_connect("localhost","root","[email protected]","test"); 
// Check connection 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$sql="INSERT INTO register (fname, lname, emailid, phoneno, qualification, dob, totalex, address) 
    VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[phoneno]','$_POST[qualifi]','$_POST[dob]','$_POST[totex]','$_POST[address]')"; 

if (!mysqli_query($con,$sql)) 
{ 
    die('Error: ' . mysqli_error($con)); 
} 
echo "1 record added"; 

mysqli_close($con); 
?> 

請幫我,告訴我我的代碼有什麼問題。

+0

有沒有錯誤?無論是在PHP或MYSQL? –

+0

在php中插入數據庫中的空白數據 –

+1

你的語法是錯誤的......當你像這樣使用它們時,你需要圍繞變量括號。 'values('{$ _POST ['variable']}',..' –

回答

3

你可以改變這個下面的代碼

if($valid){ 
     echo '<META HTTP-EQUIV="Refresh" Content="0; URL=datasubmitted.php">'; 
     exit; 
    } 

if($valid){ 
     include 'datasubmitted.php'; 
     echo '<META HTTP-EQUIV="Refresh" Content="0; URL=successpage.php">'; 
     exit;  
    } 
+0

可以通過'meta refresh'傳遞post的值嗎? –

+0

謝謝你的朋友,它的作品 –

+0

@Chinnu R非常感謝你這個代碼作品 –