2011-08-04 131 views
0

我有一個PHP引擎頁面,它處理輸入到具有變量行的表單上的數據。 PHP生成一個電子郵件,並且還應該將數據輸入到已經創建的MySQL表中。電子郵件工作正常,但由於某種原因沒有數據輸入到我的MySQL數據庫中。我確信數據庫和表的所有設置都是正確的。我也沒有看到任何錯誤消息。我的代碼由以下部分組成:爲什麼我的數據沒有提交到MySQL數據庫?

HTML表單:

<form method="post" name="booking" action="bookingengine.php"> 
    <fieldset> 
     <h2>Waged/Organisation Rate</h2> 
     <p> 
      <input type="text" name="name[]"> 
      <input type="text" name="email[]"> 
      <input type="text" name="organisation[]"> 
      <input type="text" name="position[]"> 
     </p> 
     <p><span class="add">Add person</span></p> 
    </fieldset> 

    <fieldset> 
     <h2>Unwaged Rate</h2> 
     <p> 
      <input type="text" name="name2[]"> 
      <input type="text" name="email2[]"> 
     </p> 
     <p><span class="add">Add person</span></p> 
    </fieldset> 

    <p><input type="submit" name="submit" id="submit" value="Submit and proceed to payment page" class="submit-button" /></p> 

</form> 

connection.php:

<?php 

$hostname = "localhost"; 
$database = "****"; 
$username = "****"; 
$password = "****"; 
$connection = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); 

?> 

bookingengine.php:

<? include 'connection.php'; ?> 

<?php 

$emailFrom = "****"; 
$emailTo = "****"; 
$subject = "Booking for Soteria Conference"; 

$body = "The following people have booked for the Soteria Conference in Derby:" . "\n\n" . "Waged/Organisation Rate:" . "\n\n"; 
$row_count = count($_POST['name']); 
$row_count2 = count($_POST['name2']); 

$values = array(); 

for($i = 0; $i < $row_count; $i++) { 
    // variable sanitation... 
    $name = trim(stripslashes($_POST['name'][$i])); 
    $email = trim(stripslashes($_POST['email'][$i])); 
    $organisation = trim(stripslashes($_POST['organisation'][$i])); 
    $position = trim(stripslashes($_POST['position'][$i])); 

    // this assumes name, email, and telephone are required & present in each element 
    // otherwise you will have spurious line breaks. 
    $body .= "Name: " . $name . " Email: " . $email . " Organisation: " . $organisation . " Position: " . $position . "\n\n"; 

    //prepare the values for MySQL 
    $values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')'; 
} 

mysql_select_db($database, $connection); 

$query = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES " . implode(',', $values); 

$values = array(); 

$body .= "Unwaged Rate:" . "\n\n"; 

for($i = 0; $i < $row_count; $i++) { 
    // variable sanitation... 
    $name = trim(stripslashes($_POST['name'][$i])); 
    $email = trim(stripslashes($_POST['email'][$i])); 

    // this assumes name, email, and telephone are required & present in each element 
    // otherwise you will have spurious line breaks. 
    $body .= "Name: " . $name . " Email: " . $email . "\n\n"; 

    //prepare the values for MySQL 
    $values[] = '(' . $name . ',' . $email . ')'; 
} 
$query = "INSERT INTO conference (Name, Email) VALUES " . implode(',', $values); 

// send email 
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>"); 

// redirect to success page 
if ($success){ 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=/conference/payment.html\">"; 
} 
else{ 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; 
} 
?> 

誰能找出爲什麼數據不會被髮送到MySQL數據庫?

感謝,

尼克

目前代碼:

for($i = 0; $i < $row_count; $i++) { 
    // variable sanitation... 
    $name = trim(stripslashes($_POST['name'][$i])); 
    $email = trim(stripslashes($_POST['email'][$i])); 
    $organisation = trim(stripslashes($_POST['organisation'][$i])); 
    $position = trim(stripslashes($_POST['position'][$i])); 

    // this assumes name, email, and telephone are required & present in each element 
    // otherwise you will have spurious line breaks. 
    $body .= "Name: " . $name . " Email: " . $email . " Organisation: " . $organisation . " Position: " . $position . "\n\n"; 

    //prepare the values for MySQL 
    $values = "'" . $name . "','" . $email . "','" . $organisation . "','" . $position . "'"; 
} 

mysql_select_db($database, $connection); 

$query1 = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES " . $values; 

$result1 = mysql_query($query1); 
if (!$result1) { 
    die('Invalid query: ' . mysql_error()); 
} 
+0

您是否正在獲取$ _POST ['name'] [$ i]的值 – nidhin

+3

您在哪裏運行查詢?我沒有看到它。 – JJJ

+1

另外,您不使用'stripslashes()'來清理* input *。您從數據庫中提取數據時使用它。改爲使用'mysq_real_escape_string()'。 – JJJ

回答

2

mysql_query($query);或其他函數執行查詢丟失。您正在用$query = ...構建您的查詢,但是您不使用此變量。

您可以添加這樣的:

$result = mysql_query($query); 
if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 

欲瞭解更多詳細信息,請參閱:http://www.php.net/manual/en/function.mysql-query.php

此外,該查詢缺少一些省略號的價值觀:

代碼:

$values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')'; 

應該是這樣的:

$values[] = '(\'' . $name . '\',\'' . $email . '\',\'' . $organisation . '\',\'' . $position . '\')'; 

$values[] = "('" . $name . "','" . $email . "','" . $organisation . "','" . $position . "')"; 

否則價值會被解釋爲字段名。

+0

謝謝。我已經添加了這個,現在得到錯誤:「無效的查詢:'字段列表'中的未知列'asdasdas',其中'asdasdas'是我輸入到名稱字段中的。 – Nick

+0

非常感謝馬丁。這解決了它。享受這一天。 Nick – Nick

+0

歡迎你。另外我想說的是,這段代碼可能需要一些進一步的改進。特別是僅使用一個值的數組+ implode的用法不是必需的。一個簡單的字符串就可以做到。 –

3

我猜你設置插入語句後沒有執行查詢。

2

就拿第一個查詢值...

$query = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES (" . $values . ")"; 

這是不實際執行查詢,但簡單地定義它。你必須將其通過mysql_query()像這樣採取執行它的額外步驟...

mysql_query($query); 

不用說,你應該考慮將其插入到數據庫之前滅菌數據。祝你好運!

編輯:更改此行...

$values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')'; 

了這一點。正如你所看到的,你需要引用你的個人價值觀,在這種情況下不需要數組。只需使用一個字符串。您也可以從您的查詢字符串中刪除implode(),並引用該變量。

$values = "('" . $name . "','" . $email . "','" . $organisation . "','" . $position . "')"; 
+0

謝謝。我已經添加了這個,現在得到錯誤:「無效的查詢:'字段列表'中的未知列'asdasdas',其中'asdasdas'是我輸入到名稱字段中的。 – Nick

+0

請參閱我上面的編輯。 – 65Fbef05

+0

謝謝。我試圖改變爲一個字符串,現在我得到了你也說我應該刪除的內爆函數的錯誤。我將如何'只參考變量'?對不起,有點虛擬! – Nick

相關問題