在使用Php将数据插入mysql数据库时。I';我听到一个错误,说整数值不正确:'';对于列';费率';在第一排
1.Php代码如下,我没有自动递增字段 完整错误描述 错误:无法执行INSERT INTO employee(emp_名称、费率、, ifsc_代码、acc_编号、acc_持有人(名称)值(“”、“”、“”、“”、“”)。 第1行“rate”列的整数值“”不正确在使用Php将数据插入mysql数据库时。I';我听到一个错误,说整数值不正确:'';对于列';费率';在第一排,php,mysql,Php,Mysql,1.Php代码如下,我没有自动递增字段 完整错误描述 错误:无法执行INSERT INTO employee(emp_名称、费率、, ifsc_代码、acc_编号、acc_持有人(名称)值(“”、“”、“”、“”、“”)。 第1行“rate”列的整数值“”不正确 根据我的评论,您应该将$rate转换为整数或删除”这样的单引号 $sql = "INSERT INTO employee( emp_name, rate,
根据我的评论,您应该将$rate转换为整数或删除”
这样的单引号
$sql = "INSERT INTO employee( emp_name,
rate,
ifsc_code,
acc_num,
acc_holder_name)
VALUES ( '$emp_name',
$rate,
'$ifsc_code',
'$acc_num',
'$acc_holder_name')";
或
您可以像下面这样转换为整数$rate=(int)$rate代码>
还可以使用带绑定参数功能的pdo
来防止sql
注入如上注释所述,原始代码可能容易受到sql注入的影响,因此建议使用准备好的语句
来帮助降低风险
您发布的错误消息与我有关-似乎所有值都是空的。。。是这样吗?在尝试sql操作之前,应该检查这些变量是否存在
<?php
include_once('connectdb.php');
try{
$sql='insert into `employee` ( `emp_name`, `rate`, `ifsc_code`, `acc_num`, `acc_holder_name` ) values (?,?,?,?,?);';
/* field names expected in REQUEST array and associated data type for filtering */
$args=array(
'emp_name' => FILTER_SANITIZE_STRING,
'rate' => FILTER_SANITIZE_NUMBER_INT, /* assumed that rate is an integer */
'ifsc_code' => FILTER_SANITIZE_STRING,
'acc_num' => FILTER_SANITIZE_NUMBER_INT, /* assumed that acc_num is an integer ?? */
'acc_holder_name' => FILTER_SANITIZE_STRING
);
/* filter REQUEST array using above arguments */
filter_input_array( INPUT_REQUEST, $args );
/* extract variables */
extract( $_REQUEST );
/* If all the variables were extracted correctly after filtering - proceed */
if( $emp_name && $rate && $ifsc_code && $acc_num && $acc_holder_name ){
/* if the filter failed this will probably never be called but... */
if( !is_integer( $rate ) ) throw new Exception('rate is not an integer');
/* create a prepared statement */
$stmt=$link->prepare( $sql );
/* If the query failed for some reason - abandon ship */
if( !$stmt )throw new Exception( sprintf( 'error preparing sql query: %s', $stmt->error ) );
/* assumed that rate and acc_num is an integer ?? */
$stmt->bind_param( 'sisis', $emp_name, $rate, $ifsc_code, $acc_num, $acc_holder_name );
/* execute the query */
$result = $stmt->execute();
if( $result ){
echo "Success";
} else {
throw new Exception( sprintf( "Bogus! %s", $stmt->error ) );
}
} else {
throw new Exception( 'an error occurred extracting one or more variables - check "$args" array!' );
}
} catch( Exception $e ){
exit( $e->getMessage() );
}
?>
'
不是整数值。改为使用null
(如果表中的列当然接受null),这是一个简单的错误。将$rate
更改为整数或删除'
上的单引号$rate
什么是rate
的整列类型?如果您有数字数据类型,则必须删除相关var1周围的单引号。删除$rate
周围的引号。您需要添加null
或0
作为表3中该列的默认值。对于SQL注入
而言,代码是完全开放的,因此使用prepared statements
一旦我删除$rate左右的引号,我就会收到一个错误,说您无法执行INSERT INTO INTO employee(emp_名称、费率、ifsc_代码、acc_num、acc持有人_姓名)值(“”,=(int),“”,“”,“”)。您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,了解在第8行“=(int)”、“,”附近使用的正确语法。首先,您应该只删除“
上的”
,然后执行查询
<?php
include_once('connectdb.php');
try{
$sql='insert into `employee` ( `emp_name`, `rate`, `ifsc_code`, `acc_num`, `acc_holder_name` ) values (?,?,?,?,?);';
/* field names expected in REQUEST array and associated data type for filtering */
$args=array(
'emp_name' => FILTER_SANITIZE_STRING,
'rate' => FILTER_SANITIZE_NUMBER_INT, /* assumed that rate is an integer */
'ifsc_code' => FILTER_SANITIZE_STRING,
'acc_num' => FILTER_SANITIZE_NUMBER_INT, /* assumed that acc_num is an integer ?? */
'acc_holder_name' => FILTER_SANITIZE_STRING
);
/* filter REQUEST array using above arguments */
filter_input_array( INPUT_REQUEST, $args );
/* extract variables */
extract( $_REQUEST );
/* If all the variables were extracted correctly after filtering - proceed */
if( $emp_name && $rate && $ifsc_code && $acc_num && $acc_holder_name ){
/* if the filter failed this will probably never be called but... */
if( !is_integer( $rate ) ) throw new Exception('rate is not an integer');
/* create a prepared statement */
$stmt=$link->prepare( $sql );
/* If the query failed for some reason - abandon ship */
if( !$stmt )throw new Exception( sprintf( 'error preparing sql query: %s', $stmt->error ) );
/* assumed that rate and acc_num is an integer ?? */
$stmt->bind_param( 'sisis', $emp_name, $rate, $ifsc_code, $acc_num, $acc_holder_name );
/* execute the query */
$result = $stmt->execute();
if( $result ){
echo "Success";
} else {
throw new Exception( sprintf( "Bogus! %s", $stmt->error ) );
}
} else {
throw new Exception( 'an error occurred extracting one or more variables - check "$args" array!' );
}
} catch( Exception $e ){
exit( $e->getMessage() );
}
?>