簡體   English   中英

插入SQL查詢未成功執行

[英]Insert SQL Query is not executing successfully

我插入一些數據與下面的代碼,但沒有插入。 我已經通過echo $_REQUEST數據檢查了所有內容,輸出中的一切都很好,但是沒有插入數據

有了這段代碼。 我從表單中抓取數據

$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    |

您正在嘗試將NULL值插入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."')";

bunsiness_email應該是你的插入中的business_email ,因為列bunsiness_email不存在而會徹底破壞它。 沒有學習准備好的語句,因為在這里你有一個場景,你將負責許多打開和關閉單一和雙重配額標記,並且准備好的語句使得處理更容易,更安全,防止SQL注入。

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."')";

由於business_id是INT AUTO INCREMENT列,因此在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,請不要使用引號。

您的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");

您的業​​務ID是主鍵。它是自動增量值。因此插入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."')";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM