插入SQL查询未成功执行
我插入了一些数据,但没有插入下面的代码。我已经检查了每个东西回声$_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 |
bunsiness_email
应business_email
在您的插入,将彻底打破它,因为列bunsiness_email
不存在。了解准备好的语句,因为在这里你有一个场景,你将会关注许多打开和关闭单双引号标记,而准备好的语句使处理变得更容易,更加安全,以防SQL注入。
Gotcha This one thanx @Octopi可以更详细地解释如何从注入中获得安全 – Bandayar 2013-02-11 13:00:02
Booya,请查看@John Conde在第一条评论中发布的教程。有一个SQL注入问题的解释和准备语句如何解决它。 – 2013-02-11 13:05:26
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自动递增列,你不需要它在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");
您正在试图插入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."')";
您的业务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."')";
[**在新的代码,请不要使用'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
使用'mysql_error()'输出错误。 – Sirko 2013-02-11 12:44:13
此外,您可以[SQL注入](http://stackoverflow.com/q/60174) – 2013-02-11 12:44:23