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

标签 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

最佳答案

当您将它绑定(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/

相关文章:

php - 通过cPanel使用PHP创建mysql DB并导入*.sql文件

mysql - 如何在 MySQL 中使用 DATEADD?

javascript - 使用 AJAX 使用从 mysql 数据库中提取的数据填充表单

php - Codeigniter - 具有多个参数的搜索表单

php - memcached 是关于时间的吗?

php - 更改 Zend_Framework 项目中的公用文件夹位置?

php - 从 Woocommerce 中的我的帐户订单表中删除项目计数

php - 从联合表中选择 php/mysql

MySQL : Select all the rows except those having the specified values in columns

MYSQL JOIN 两个表按日期限制第二个表的结果