2017-03-31 51 views
-3

因此,我正在應用程序,直到我得到一個錯誤,我不明白爲什麼這不起作用,本質上代碼試圖把東西放在數據庫中,這是不做。我得到了這個錯誤。一般錯誤:1366錯誤的整數值

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

這裏是我將不得不代碼:
數據庫:

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

我WO我很感激我能得到的任何幫助,謝謝!

+0

儘量不要有五行寬的代碼行。 –

+0

這個問題與PHP無關。這是你的MySQL查詢的問題。請將您的問題縮小到未來的特定單一技術。 –

+1

通常情況下,你試圖插入一個錯誤的類型值,關於列規範,例如'string'到'int'。我建議你首先縮進你的sql - 這樣做更具可讀性 - 然後逐個將值類型與列類型進行比較。 – butterFlyNick

回答

1

錯誤消息告訴你什麼是錯的;你試圖插入一個整數值爲id''的行。

這是無效的。

要麼提供一個有效的整數作爲id,要麼後備自動遞增功能,根本不提供id。不利之處在於,您必須列出您希望爲其提供價值的做的

閱讀the MySQL documentation on INSERT瞭解如何使用它。

相關問題