数据库中插入了不正确的值

时间:2014-02-01 13:58:16

标签: php mysql

我有这段代码:

<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

1 个答案:

答案 0 :(得分:4)

将它绑定到查询时,您将它转换为数字(实际上是一个双精度数)。您需要使用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