0

我正在使用以下代码插入一些数据,但没有插入。我已经通过回显$_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    |
4

6 回答 6

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."')";
于 2013-02-11T12:49:02.617 回答
1

bunsiness_email应该business_email在您的插入中,这将彻底破坏它,因为该列bunsiness_email不存在。Nthing 学习准备好的语句,因为在这里你有一个场景,你将照顾许多打开和关闭的单引号和双引号,而准备好的语句使处理更容易 + 更安全地防止 SQL 注入。

于 2013-02-11T12:51:35.820 回答
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."')";
于 2013-02-11T12:47:12.917 回答
0

由于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,请不要使用引号。

于 2013-02-11T12:47:40.460 回答
0

您的 business_id 是自动增量主键,因此当您在查询中将其作为 null 发送时,它会给出错误 remove business_id from your query

$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");
于 2013-02-11T12:48:54.683 回答
0

您的业​​务 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."')";
于 2013-02-11T13:48:08.940 回答