Php 插入SQL查询未成功执行
我用下面的代码插入了一些数据,但没有被插入。我已经通过echoPhp 插入SQL查询未成功执行,php,mysql,Php,Mysql,我用下面的代码插入了一些数据,但没有被插入。我已经通过echo$\u REQUESTdata检查了所有内容输出中的所有内容,但没有插入数据 用这个代码。我正在从表单中获取数据 $bname =$_REQUEST['bname']; $btype =$_REQUEST['btype']; $bemail =$_REQUEST['bemail']; $bwebsite =$_REQUEST['bwebsite']; $bphone
$\u REQUEST
data检查了所有内容输出中的所有内容,但没有插入数据
用这个代码。我正在从表单中获取数据
$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 |
业务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."')";
由于
business\u 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,请不要使用引号。您的业务id是自动递增主键,因此当您在查询中以NULL形式发送它时,它会给出错误消息,从查询中删除业务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\u id
列。您不能这样做,因为此列不能为null(因为它是主键)
请尝试使用:(我已删除“插入业务id”列)
bunsiness\u email
应该是插入的business\u email
,这将彻底破坏它,因为bunsiness\u email
列不存在。n学习准备好的语句,因为这里有一个场景,您将关注许多打开和关闭的单引号和双引号,准备好的语句使得处理SQL注入变得更加容易和安全。您的业务id是主键。它是自动增量值。因此,在插入业务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."')";
. 它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,.使用
mysql\u error()
输出错误。此外,您对问题中显示的错误非常开放。首先,死亡并同时响应mysql\u error()
将是一个明智的主意。下一步,将您的输入数据化!如果有人输入一个“作为输入,你会得到bobby tables.明白了,这一个thanx@Octopi你能解释一下如何从injectionBooya获得安全性吗?请查看@John Conde在第一条评论中发布的教程。这里有一个SQL注入问题的解释,以及预处理语句是如何解决这个问题的。
$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."')";