[英]mysql && php issue with id
i am getting the following error in terminal 我在终端中收到以下错误
127.0.0.1:36368 [500]: /create_account.php - Uncaught PDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 in /home/hue/social_network/classes/DB.php:10
Stack trace:
#0 /home/hue/social_network/classes/DB.php(10): PDOStatement->execute(Array)
#1 /home/hue/social_network/create_account.php(7): DB::query('INSERT INTO use...', Array)
#2 {main}
thrown in /home/hue/social_network/classes/DB.php on line 10
Here is my php code: 这是我的PHP代码:
<?php
include('classes/DB.php');
if (isset($_POST['createaccount'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
DB::query('INSERT INTO users VALUES (:username, :password, :email)',
array(':username'=>$username, ':password'=>$password, ':email'=>$email));
echo "Success!";
}
?>
<h1>Register</h1>
<form action="create_account.php" method="post">
<input type="text" name="username" value="" placeholder="Username ..."><p />
<input type="password" name="password" value="" placeholder="Password ..."><p />
<input type="email" name="email" value="" placeholder="someone@somesite.com"><p />
<input type="submit" name="createaccount" value="Create Account">
</form>
db class: db类:
<?php
class DB {
private static function connect() {
$pdo = new PDO('mysql:host=127.0.0.1;dbname=social_network;charset=utf8', 'root', 'helloworld');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
public static function query($query, $params = array()) {
$statement = self::connect()->prepare($query);
$statement->execute($params);
// $data = $statement->fetchAll();
// return $data;
}
}
i tried to INSERT values for id : ":id" and "\\'\\'" , but nothing change. 我尝试插入id的值:“:id”和“ \\'\\'”,但没有任何变化。 my id field is auto_increment
我的id字段是auto_increment
Unless you're inserting all columns you need to specify which ones you're using: 除非要插入所有列,否则需要指定要使用的列:
INSERT INTO users (username, password, email) VALUES (:username, :password, :email)
You're omitting the column specification, so the default likely includes id
and a bunch of other things. 您将省略列说明,因此默认值可能包括
id
和其他内容。
Also before you go and write your own ORM accidentally keep in mind there are several out there that are feature-complete, battle-tested, and documented: Doctrine , Propel and Eloquent are just a few examples. 同样,在不经意地编写自己的ORM之前,请记住,其中有一些功能完整,经过了战斗测试并已记录在案: Doctrine , Propel和Eloquent只是几个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.