插入SQL查询未成功执行

时间:2013-02-11 12:43:11

标签: php mysql

我使用下面的代码插入一些数据,但没有插入。我已经通过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    |

6 个答案:

答案 0 :(得分: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."')";

答案 1 :(得分:1)

bunsiness_email在您的插入中应为business_email,因为列bunsiness_email不存在而会彻底破坏它。没有学习准备好的语句,因为在这里您将有一个场景,您将负责处理许多打开和关闭的单一和双重配额标记,并且准备好的语句使得处理更容易,更安全,防止SQL注入。

答案 2 :(得分: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."')";

答案 3 :(得分: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,请不要使用引号。

答案 4 :(得分: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");

答案 5 :(得分: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."')";