2012-11-22 102 views
1

我無法將表單數據插入到數據庫中。我可以連接到數據庫,因爲它不會引發錯誤,但不會插入任何信息。以下是我的代碼,任何幫助將不勝感激。未將表單數據插入數據庫的PHP腳本

<?php 
// string checking 
function isValid($str) { 
    if(!preg_match('/[^A-Za-z0-9.-]/', $str)) { 
    return true; 
    } else { 
    return false; 
    } 
} 


// Check for form submission 
if(isset($_POST['submit'])){ 

// Get the POST data 
$agree = $_POST['agree']; 
$firstname = $_POST['firstname']; 
$surname = $_POST['surname']; 
$business = $_POST['business']; 
$state = $_POST['state']; 
$email = $_POST['email']; 

// If the T&C box has been ticked 
if($agree){ 

    // Validate the POST data 
    $validationError = ''; 

    // Name Validation 
    if($firstname == ''){ 
     $validationError .= "Please enter your first name.\n"; 
    } else { 
     if(is_numeric($firstname)){ 
     $validationError .= "The first name you have supplied cannot contain numbers.\n"; 
     } 
     if(strlen($firstname) > 50) { 
     $validationError .= "The first name you have supplied must be less than 50 characters.\n"; 
     } 
     if(strlen($firstname) < 3) { 
     $validationError .= "The first name you have supplied is too short.\n"; 
     } 
     if(isValid($firstname) == false){ 
     $validationError .= "The first name cannot contain special characters.\n"; 
    } 
    } 

if($surname == ""){ 
     $validationError .= "Please enter your Surname.\n"; 
    } else { 
     if(is_numeric($surname)){ 
     $validationError .= "The Surname you have supplied cannot contain numbers.\n"; 
     } 
     if(strlen($surname) > 50) { 
     $validationError .= "The Surname you have supplied must be less than 50 characters.\n"; 
     } 
     if(strlen($surname) < 3) { 
     $validationError .= "The Surname you have supplied is too short.\n"; 
     } 
     if(isValid($surname) == false){ 
     $validationError .= "The Surname cannot contain special characters.\n"; 
    } 
    } 


if($state == ''){ 
     $validationError .= "Please select your state.\n"; 
} 

    // Email validation 

    // Function to validate email addresses, taken from here: http://www.linuxjournal.com/article/9585 
    function check_email_address($email) { 

     // Set up regular expression strings to evaluate the value of email variable against 
     $regex1 = '/^[_a-z0-9-][^()<>@,;:\\"[] ]*@([a-z0-9-]+.)+[a-z]{2,4}$/i'; 

     // Run the preg_match function on regex 1 
     if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { 
      return false; 
     } else { 
      return true;  
     } 
    } 

    if($email != ''){ 
     if(!check_email_address($email)) { 
     $validationError .= "The email address does not appear to be correct, please try again.\n"; 
     } 
    } else { 
     $validationError .= "Please enter your email address.\n"; 
    } 

    //Start the mySQL connection 

    if($validationError == ''){ 
    $link = mysql_connect('localhost', '--removed--', '--removed--'); 

    if (!$link) { 
     $validationError .= "There was an error connecting to the database. Please contact us so that we can fix the problem.\n"; 
    } else { 

     // Select the db 
     mysql_select_db("keyinv_seminar", $link); 

     // Check for an existing entry under that email address 
     $checkQuery = 'SELECT * FROM seminar WHERE email="' . mysql_real_escape_string($email) . '"'; 
     $result = mysql_query($checkQuery, $link); 

     if (mysql_num_rows($result) != 0) { 
     $validationError .= "There is already an entry in the competition, using that email address.\n"; 
     } else { 
     // There is no existing entry, update the db 
     $insertQuery = "INSERT INTO seminar (firstname, surname, business, state, email) VALUES ('" . mysql_real_escape_string($firstname) . "', '" . mysql_real_escape_string($surname) . "', '" . mysql_real_escape_string($business) . "', " . mysql_real_escape_string($state) . ", '" . mysql_real_escape_string($email) . "')"; 

     $result = mysql_query($insertQuery, $link); 

     } 

     // Close the connection 
     if($link){ 
     mysql_close($link); 

     if($validationError == ''){ 
      header('Location: thankyou.php'); 
     } 
     } 
    } 
    } 
} else { 
    $validationError = "You must accept the Terms and Conditions \nin order to enter this contest."; 
} 
} 
?> 
+0

您應該嘗試捕獲mysql錯誤而不是驗證錯誤。請參閱mysql_error()php函數。 – rayfranco

+1

正如人們經常說的,你不應該再使用mysql_query了,但是我相信你的mysql_real_escape_string($ state)(在插入時)的輸入需要在它周圍標記。 - 檢查F4r-20的帖子以獲得更好的總結。 – Lucas

回答

5

查看您所在INSERT查詢..

$insertQuery = "INSERT INTO seminar (firstname, surname, business, state, email) VALUES ('" 
    . mysql_real_escape_string($firstname) . "', '" 
    . mysql_real_escape_string($surname) . "', '" 
    . mysql_real_escape_string($business) . "', " 
    . mysql_real_escape_string($state) . ", '" 
    . mysql_real_escape_string($email) . "')"; 

你已經錯過了周圍的$state值的一些單引號。更改爲:

$insertQuery = "INSERT INTO seminar (firstname, surname, business, state, email) VALUES ('" 
    . mysql_real_escape_string($firstname) . "', '" 
    . mysql_real_escape_string($surname) . "', '" 
    . mysql_real_escape_string($business) . "', '" 
    . mysql_real_escape_string($state) . "', '" 
    . mysql_real_escape_string($email) . "')"; 
+0

另外,他不會在任何地方回顯驗證錯誤。 –

+0

嗯,我們不知道這個劇本之後有很好的迴應。 – George

+0

+1格式化無盡的線條也有助於趕上這種錯別字 –