我有这个代码:
<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代码如下:
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
最佳答案
当您将它绑定(bind)到查询时,您正在将它转换为一个数字(实际上是一个 double )。您需要使用 s
而不是 d
:
$stmt->bind_param('d', $e);
应该是
$stmt->bind_param('s', $e);
参见 the manual关于类型:
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
关于php - 数据库中插入了不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21498919/