简体   繁体   English

使用PDO-mysql将值插入数据库

[英]Inserting values into database using PDO-mysql

When I execute my, mysql query to database using PDO-mysql it will not insert the values. 当我使用PDO-mysql对数据库执行mysql查询时,它不会插入值。 I am following the documentation but still don't seem to have made any progress. 我正在关注文档,但似乎仍未取得任何进展。

So my .sql file is this 所以我的.sql文件是这个

CREATE DATABASE IF NOT EXISTS `qr_db`; 

CREATE TABLE IF NOT EXISTS `qr_db`.`clients` (
    `id` int(11) NOT NULL auto_increment,
    `first_name` varchar(100) NOT NULL,
    `last_name` varchar(100) NOT NULL,
    `key` char(128) NOT NULL,
    PRIMARY KEY(`id`),
    UNIQUE KEY `key` (`key`)
) ENGINE=MyISAM;

grant select, insert, update, delete 
on qr_db.* 
to qr_db@localhost identified by 'password';

and this is the code I'm having trouble with 这是我遇到麻烦的代码

if(!isset($_GET['key'])) {

$mysql = db_connect();  // <--- this is ok i already checked this

$query = 'INSERT INTO `clients` (`first_name`, `last_name`, `hash`) VALUES (?, ?, ?)';

$stmt = $mysql->prepare($query);

$stmt->bindParam(1, $f_name, PDO::PARAM_STR);
$stmt->bindParam(2, $l_name, PDO::PARAM_STR);
$stmt->bindParam(3, $dubhash, PDO::PARAM_STR);
$stmt->execute();

} 

First check your connection if you connected with the database then use following the code to insert the record 首先检查您的连接(如果您已连接到数据库),然后使用以下代码插入记录

Change hash in column name to key. 将列名中的哈希更改为键。 this is creating problem because table don't have any column with name hash 这造成了问题,因为表没有任何带有名称哈希的列

 $sql = "INSERT INTO clients (first_name, last_name, key) VALUES (:fname, :lname, :hashval)";

$q = $mysql->prepare($sql);
$q->execute(array(':fname'=>$f_name,
                  ':lname'=>$lname,
                  ':hashval'=>$hash ));

Best of Luck... 祝你好运...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM