所以我正在开发一个应用程序,直到我收到一个错误,我无法弄清楚为什么这不起作用,本质上代码试图将某些东西放在数据库中,但它没有做到。我用它得到了这个错误。
致命错误:带有消息'SQLSTATE [HY000]的未捕获异常'PDOException':常规错误:1366错误的整数值:'/ Users / DannyTran / Desktop / WebServer / social-中第1行'列'id' network / classes / DB.php:10堆栈跟踪:#0 /Users/DannyTran/Desktop/WebServer/social-network/classes/DB.php(10):PDOStatement-> execute(Array)#1 / users / DannyTran /Desktop/WebServer/social-network/register.php(24):DB :: query('INSERT INTO use ...',Array)/ users / DannyTran / Desktop / WebServer / social-中引发#2 {main}第10行的network / classes / DB.php
以下是我的代码:
数据库:
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL DEFAULT '',
`first_name` varchar(255) NOT NULL DEFAULT '',
`last_name` varchar(255) NOT NULL DEFAULT '',
`email` varchar(255) NOT NULL DEFAULT '',
`password` varchar(255) NOT NULL DEFAULT '',
`gender` varchar(11) NOT NULL DEFAULT '',
`country` varchar(255) NOT NULL DEFAULT '',
`verified` int(11) NOT NULL,
`role` varchar(255) NOT NULL DEFAULT '',
`bio` text,
`website` text,
`status` text,
`register` date NOT NULL,
`last` date NOT NULL,
`activated` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Register.php:
<?php
include('./classes/DB.php');
$pdo = new PDO('mysql:host=127.0.0.1;DBname=socialnetwork;charset=utf8', 'root', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['register'])) {
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['uname']);
$em = strip_tags(@$_POST['email']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$country = strip_tags(@$_POST['country']);
$gender = strip_tags(@$_POST['gender']);
$date = date("m-d-Y"); // Year - Month - Day
$no = 0;
$role = 'Member';
$status = 'Hello world!';
$website = '';
$bio = '';
DB::query('INSERT INTO users VALUES (\'\' , :username, :first_name, :last_name, :email, :password, :gender, :country, :verified, :role, :bio, :website, :status, :register, :last, :activated)', array(':username'=>$un,':first_name'=>$fn, ':last_name'=>$ln, ':email'=>$em, ':password'=>$pswd, ':gender'=>$gender, ':country'=>$country, ':verified'=>0, ':role'=>$role, ':bio'=>$bio, ':website'=>$website, ':status'=>$status, ':register'=>$date, ':last'=>$date, ':activated'=>0));
echo "Welcome!";
};
?>
<h1>Register Now</h1>
<form action="#" method="post">
<input type="text" placeholder="First Name" name="fname" required/><br /><br />
<input type="text" placeholder="Last Name" name="lname" required/><br /><br />
<input type="text" placeholder="Username" name="uname" required/><br /><br />
<input type="email" placeholder="E-Mail Address" name="email" required/><br /><br />
<input type="password" placeholder="Password" name="pswd" required/><br /><br />
<input type="password" placeholder="Confirm Password" name="pswd2" required/><br /><br />
<select name="gender">
<option value="" disabled selected>Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br /><br />
<?php require_once './options/countries.php'; ?><br /><br />
<input name="register" type="submit" value="Login" /><br /><br />
</form>
<a href="login.php">Have an account? Login!</a>
db.php中:
<?php
class DB {
private static function connect() {
$pdo = new PDO('mysql:host=127.0.0.1;dbname=socialnetwork;charset=utf8', 'root', 'password');
$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;
}
}
?>
我很感激我能得到任何帮助,谢谢!
答案 0 :(得分:1)
错误消息告诉您哪里出错了;您正在尝试插入id
整数值id
的行。
这是无效的。
要么提供有效的整数$task = new Task();
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Create Task'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$task = $form->getData();
// ... perform some action, such as saving the task to the database
// for example, if Task is a Doctrine entity, save it!
// $em = $this->getDoctrine()->getManager();
// $em->persist($task);
// $em->flush();
return $this->redirectToRoute('task_success');
}
return $this->render('default/new.html.twig', array(
'form' => $form->createView(),
));
,要么通过不提供.flipud()
来回退自动增量功能。缺点是你必须为做希望提供值的每一列命名。
阅读the MySQL documentation on INSERT
以了解如何使用它。