dqrmkdu25623 2017-03-31 13:27
浏览 77
已采纳

一般错误:1366不正确的整数值

So I am working on an app until I got an error which I can't figure out why this does not work, essentially the code is trying to put something in a database, which it is not doing. I got this error with it.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'id' at row 1' in /Users/DannyTran/Desktop/WebServer/social-network/classes/DB.php:10 Stack trace: #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) #2 {main} thrown in /Users/DannyTran/Desktop/WebServer/social-network/classes/DB.php on line 10

Here is the code that I would have:
Database:

 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;
        }
    }
?>

I would appreciate any help I could get, thank you!

  • 写回答

1条回答 默认 最新

  • dtu72460 2017-03-31 13:30
    关注

    The error message tells you what's wrong; you're trying to insert a row with an id integer value ''.

    That is not valid.

    Either provide a valid integer as id, or fallback on the auto-increment feature by not providing an id at all. The downside there is that you have to name each column for which you do wish to provide values.

    Read the MySQL documentation on INSERT to find out how to use it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题