2013-02-11 166 views
0

我插入了一些數據,但沒有插入下面的代碼。我已經檢查了每個東西回聲$_REQUEST數據一切都很好輸出,但不插入數據插入SQL查詢未成功執行

用此代碼。我從形式

$bname   =$_REQUEST['bname']; 
$btype   =$_REQUEST['btype']; 
$bemail  =$_REQUEST['bemail']; 
$bwebsite  =$_REQUEST['bwebsite']; 
$bphone  =$_REQUEST['bphone']; 
$bpostal  =$_REQUEST['bpostal']; 
$bcountry  =$_REQUEST['bcountry']; 
$bannertype =$_REQUEST['bannertype']; 
$bgcolor  =$_REQUEST['bgcolor']; 
$bheadcolor =$_REQUEST['bheadcolor']; 
$bsubheadcolor =$_REQUEST['bsubheadcolor']; 
$btextcolor =$_REQUEST['btextcolor']; 

它獲取與此

echo "$bname, $btype, $bemail, $bwebsite, $bphone, $bpostal, $bcountry, $bannertype, 
$bgcolor, $bheadcolor,$bsubheadcolor,$btextcolor"; 

容易呼應抓取數據,但是當它涉及到插入不工作給予錯誤

include 'dbconnect.php'; 

$sql="insert into company (business_id, business_name, business_type, bunsiness_email, 

business_website, work_phone, postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('NULL','".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')"; 

mysql_query($sql) or die("An Error Occured while updating"); 
include 'dbclose.php';*/ 

,這是我的表說明

+----------------------------+---------------+------+-----+---------+----------- 
-----+ 
| Field      | Type   | Null | Key | Default | Extra 
| 
+----------------------------+---------------+------+-----+---------+----------- 
-----+ 
| business_id    | int(10)  | NO | PRI | NULL | auto_increment | 
| business_name    | varchar(50) | NO |  | NULL || 
| business_type    | varchar(50) | NO |  | NULL |  | 
| business_email    | varchar(50) | NO |  | NULL | 
| business_website   | varchar(50) | NO |  | NULL | 
| work_phone     | varchar(20) | NO |  | NULL | 
| postal_code    | varchar(20) | NO |  | NULL | 
| business_country   | varchar(20) | NO |  | NULL | 
| banner_type    | varchar(50) | NO |  | NULL | 
| select_bgcolor    | varchar(50) | NO |  | NULL | 
| select_heading1   | varchar(50) | NO |  | NULL | 
| select_heading2   | varchar(50) | NO |  | NULL | 
| select_text    | varchar(50) | NO |  | NULL | 
+1

[**在新的代碼,請不要使用'mysql_ *'功能**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – 2013-02-11 12:44:05

+0

使用'mysql_error()'輸出錯誤。 – Sirko 2013-02-11 12:44:13

+2

此外,您可以[SQL注入](http://stackoverflow.com/q/60174) – 2013-02-11 12:44:23

回答

1

bunsiness_emailbusiness_email在您的插入,將徹底打破它,因爲列bunsiness_email不存在。瞭解準備好的語句,因爲在這裏你有一個場景,你將會關注許多打開和關閉單雙引號標記,而準備好的語句使處理變得更容易,更加安全,以防SQL注入。

+0

Gotcha This one thanx @Octopi可以更詳細地解釋如何從注入中獲得安全 – Bandayar 2013-02-11 13:00:02

+0

Booya,請查看@John Conde在第一條評論中發佈的教程。有一個SQL注入問題的解釋和準備語句如何解決它。 – 2013-02-11 13:05:26

0

business_id將永遠不會爲NULL。它是自動遞增的字段。

$sql="insert into company (business_name, business_type, bunsiness_email, 

business_website, work_phone, postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')"; 
0

由於business_id是INT自動遞增列,你不需要它在INSERT查詢。

$sql = "insert into company (
    business_name, 
    business_type, 
    business_email, 
    business_website, 
    work_phone, 
    postal_code, 
    business_country, 
    banner_type, 
    select_bgcolor, 
    select_heading1, 
    select_heading2, 
    select_text 
    ) values (
     '" . $bname . "', 
     '" . $btype . "', 
     '" . $bemail . "', 
     '" . $bwebsite . "', 
     '" . $bphone . "', 
     '" . $bpostal . "', 
     '" . $bcountry . "', 
     '" . $bannertype . "', 
     '" . $bgcolor . "', 
     '" . $bheadcolor . "', 
     '" . $bsubheadcolor . "', 
      '" . $btextcolor . "' 
    )"; 

如果您想要在查詢中傳遞NULL,請不要引號。

0

你business_id是自動增量的主鍵,以便您在查詢發送它爲null,則給錯誤從查詢中刪除business_id

$sql="insert into company (business_name, business_type, bunsiness_email, 

business_website, work_phone, postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')"; 

mysql_query($sql) or die("An Error Occured while updating"); 
1

您正在試圖插入NULL價值爲business_id列。你不能這樣做,因爲這列不能爲空(因爲它的主鍵)

請儘量使用:(我已刪除了插入到business_id列)

$sql="insert into company (business_name, business_type, bunsiness_email, 

business_website, work_phone, postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')"; 
0

您的業務ID是主鍵,它是自動增加value.so,同時插入business_id不能插入爲NULL。 所以查詢將是:

$sql="insert into company (business_name, business_type, bunsiness_email, 

business_website, work_phone, postal_code, business_country,banner_type, 

select_bgcolor, select_heading1, select_heading2, select_text) values 


('".$bname."','".$btype."','".$bemail."','".$bwebsite."', '".$bphone."', 

'".$bpostal."', '".$bcountry."','".$bannertype."', '".$bgcolor."', '".$bheadcolor."', 

'".$bsubheadcolor."', '".$btextcolor."')";