2013-10-16 19 views
0

我正在嘗試將post表單數據發佈到mysql。但我對所有角色都感到困惑和厭倦。我想我錯過了一些角色或者添加了太多角色!請幫我把這個php表格轉換成mysql無法將此表單php發佈到mysql

瀏覽器在該行的錯誤反應:

"'" . $_POST['credit_card_expiration'] ."',".); 

下面是代碼:

<?php 
session_start(); // Start session first thing in script 
// Script Error Reporting 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
// Connect to the MySQL database 
include "storescripts/connect_to_mysql.php"; 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <title>Untitled Document</title> 
    </head> 

    <body> 
     <?php 
     //let's create the query 
     $insert_query = "INSERT INTO order (". 
     "name,". 
     "email_address,". 
     "membership_type". 
     "terms_and_conditions,". 
     "name_on_card,". 
     "credit_card_number,". 
     "credit_card_expiration_data,". 
     ") values 
    (". 
     "'" . $_POST['name'] . "', ". 
     "'" . $_POST['email_address'] . "',". 
     "'" . $_POST['membership_type'] . "',". 
     "'" . $_POST['terms_and_conditions'] . "',". 
     "'" . $_POST['name_on_card'] . "',". 
     "'" . $_POST['credit_card_number'] . "',". 
     "'" . $_POST['credit_card_expiration'] ."',".); 

     //let's run the query 
     mysql_query($insert_query); 
     ?> 
    </body> 
</html> 

我的表格:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form method="post" action="form_process.php"> 
Name on card: 
<input type="text" name="name_on_card"><br /> 
Credit card number: 
<input type="text" name="credit_card_number"><br /> 
Credit card Expiration date: 
<input type="text" name="credit_card_expiration_date"><br /> 

<input type="hidden" name="name" 
value="<?php echo $_POST['name']; ?>"> 

<input type="hidden" name="email_address" 
value="<?php echo $_POST['email_address']; ?>"> 

<input type="hidden" name="membership_type" 
value="<?php echo $_POST['membership_type']; ?>"> 

<input type="hidden" name="terms_and_conditions" 
    value="<?php echo $_POST['terms_and_conditions']; ?>"> 

<input type="submit" value="Submit"> 
</form> 
</body> 
</html> 
+0

嘗試的mysql_query($ insert_query)或trigger_error(mysql_error());看看出現了什麼。 –

回答

2
  1. 不要使用mysql_函數( Why shouldn't I use mysql_* functions in PHP?
  2. in $_POST['credit_card_expiration'] ."',".);最後一個「,」。應該不存在
  3. 我們需要查看錶單以給出正確答案
  4. FILTER輸入(或者在此處閱讀有關SQL注入的信息:How can I prevent SQL injection in PHP?)。請。
+0

另請參見:存儲cc詳細信息?用純文本? – Alasjo

+0

那麼,那應該是數字5 ...:P – opalenzuela

+0

這只是一個快速原型:) 好吧,剛剛添加了表單。 – Onizuka

0

變化

"'" . $_POST['credit_card_expiration'] ."',".); 

有了這個

"'" . $_POST['credit_card_expiration'] ."')"; 

,不使用mysql_*功能,擴展已被棄用

編輯:

並把一個逗號後membership_type!我忘了它

0

您的查詢在最後一個值之後有一個值seperator(逗號),它也沒有結尾括號。更改:

"'" . $_POST['credit_card_expiration'] ."',".); 

"'" . $_POST['credit_card_expiration'] ."')"); 
0

爲什麼你有."',".在結束了嗎?

"'" . $_POST['credit_card_expiration'] ."',".); 
              ^create an error 
"'" . $_POST['credit_card_expiration']); 

而且,如果它沒有用,則使用concatenation。使用.只有當您連接變量和字符串axample

$insert_query = "INSERT INTO order (
name, 
email_address, 
membership_type, 
terms_and_conditions, 
name_on_card, 
credit_card_number, 
credit_card_expiration_data) 
values ('" 
.$_POST['name']."','" 
.$_POST['email_address'] . "','" 
.$_POST['membership_type'] . "','" 
.$_POST['terms_and_conditions'] . "','" 
.$_POST['name_on_card'] . "','" 
.$_POST['credit_card_number'] . "','" 
.$_POST['credit_card_expiration']); 
0

的代碼應該是

"'" . $_POST['credit_card_expiration'] ."')";

,而不是

"'" . $_POST['credit_card_expiration'] ."',".); 

試試這個,

$insert_query = "INSERT INTO order (
         name, 
         email_address, 
         membership_type, 
         terms_and_conditions, 
         name_on_card, 
         credit_card_number, 
         credit_card_expiration_data) 
         values (". 
         "'" . $_POST['name'] . "', ". 
         "'" . $_POST['email_address'] . "',". 
         "'" . $_POST['membership_type'] . "',". 
         "'" . $_POST['terms_and_conditions'] . "',". 
         "'" . $_POST['name_on_card'] . "',". 
         "'" . $_POST['credit_card_number'] . "',". 
         "'" . $_POST['credit_card_expiration'] ."')"; 
1

我完全同意@opalenzuela。到1。我必須編輯:請參閱http://php.net/manual/de/book.pdo.php

和2.您還錯過了「,」在membership_type之後,並且您的最後一個「)」不是字符串的一部分,因爲它應該是。

所以正確的代碼是:

<?php 
session_start(); // Start session first thing in script 
// Script Error Reporting 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
// Connect to the MySQL database 
include "storescripts/connect_to_mysql.php"; 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<?php 

//let's create the query 
$insert_query = "INSERT INTO order (". 
"name,". 
"email_address,". 
"membership_type,". 
"terms_and_conditions,". 
"name_on_card,". 
"credit_card_number,". 
"credit_card_expiration_data,". 
") values 
(". 
"'" . $_POST['name'] . "', ". 
"'" . $_POST['email_address'] . "',". 
"'" . $_POST['membership_type'] . "',". 
"'" . $_POST['terms_and_conditions'] . "',". 
"'" . $_POST['name_on_card'] . "',". 
"'" . $_POST['credit_card_number'] . "',". 
"'" . $_POST['credit_card_expiration'] ."')"; 

//let's run the query 
mysql_query($insert_query); 

?> 
</body> 
</html> 

下一步的建議是,把你的$ _ POST值的變量,它更清晰地閱讀。 另外,如果您進行換行,則不必附加字符串,只需繼續寫。 在這之後你的代碼應該是這樣的:

<?php 
session_start(); // Start session first thing in script 
// Script Error Reporting 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
// Connect to the MySQL database 
include "storescripts/connect_to_mysql.php"; 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<?php 

$name     = $_POST['name']; 
$email_address   = $_POST['email_address']; 
$membership_type  = $_POST['membership_type']; 
$terms_and_conditions = $_POST['terms_and_conditions']; 
$name_on_card   = $_POST['name_on_card']; 
$credit_card_number  = $_POST['credit_card_number']; 
$credit_card_expiration = $_POST['credit_card_expiration']; 

//let's create the query 
$insert_query = "INSERT INTO order (
    name 
    email_address, 
    membership_type, 
    terms_and_conditions, 
    name_on_card, 
    credit_card_number, 
    credit_card_expiration_data 
) values (
    '$name', 
    '$email_address', 
    '$membership_type', 
    '$terms_and_conditions', 
    '$name_on_card', 
    '$credit_card_number', 
    '$credit_card_expiration')"; 

//let's run the query 
mysql_query($insert_query); 

?> 
</body> 
</html> 
0

我已經修改您的查詢一點試試這個,

    $insert_query = "INSERT INTO order (
    'name', 
    'email_address', 
    'membership_type', 
    'terms_and_conditions' 
    'name_on_card' 
    'credit_card_number', 
    'credit_card_expiration_data')values ('".$_POST["name"]."','".$_POST["email_address"]."','".$_POST["membership_type"]."','".$_POST["terms_and_conditions"]."','".$_POST["name_on_card"]."','".$_POST["credit_card_number"]."','".$_POST["credit_card_expiration"]."')";