在数据库中插入了不正确的值 [英] Incorrect value is inserted in database
本文介绍了在数据库中插入了不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此代码:
<form action="#" method="post">
<input type="text" name="famname"/>
<input type="submit" name="submit"/>
</form>
<?php
if(isset($_POST['submit'])){
if(!isset($_POST['famname'])){
echo "Please set Family Name.";
die();
}
$mysqli = new mysqli('localhost', 'root', 'marais19', 'famlink');
if($mysqli->connect_errno) {
echo "Connection Failed (".$mysqli->connect_errno.") : ".$mysqli->connect_error;
die();
}
$e = 'yes';
$stmt = $mysqli->prepare("INSERT INTO families(famname) VALUES (?)");
$stmt->bind_param('d', $e);
$stmt->execute();
}
?>
但是,如果我在输入框中键入Smith
,则只会将0
放入数据库中. SQL代码如下:
But if I type Smith
into the input box it only puts 0
into the database. The SQL code is as followed:
CREATE TABLE IF NOT EXISTS `families` (
`famname` varchar(30) CHARACTER SET utf8 NOT NULL DEFAULT 'NOFAM',
KEY `famname` (`famname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
并且:
INSERT INTO `families` (`famname`) VALUES
('0');
如何将Smith
放入数据库而不是0
How can I make it to put Smith
into the database and not 0
推荐答案
将其绑定到查询时,会将其强制转换为数字(实际上是双精度).您需要使用s
而不是d
:
You are casting it to a number (actually a double) when you bind it to the query. You need to use s
instead of d
:
$stmt->bind_param('d', $e);
应该是
$stmt->bind_param('s', $e);
有关类型:请参见手册
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
这篇关于在数据库中插入了不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文