2016-10-31 118 views
0

基本上我可以使用它創建一條記錄,但它不會自動更新我的ID列以允許新記錄。我需要添加一個表達式來更新計數器嗎?還是有一個按鈕或開關使用phpmyadmin自動創建提交新ID?無法在MySQL中添加新記錄

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "message"; 

try { 
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
    // set the PDO error mode to exception 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $sql = "INSERT INTO adminmessages (UserName, Message, Score) 
    VALUES ('SammyWest', 'Test1', '100')"; 
    // use exec() because no results are returned 
    $conn->exec($sql); 
    echo "New record created successfully"; 
    } 
catch(PDOException $e) 
    { 
    echo $sql . "<br>" . $e->getMessage(); 
    } 

$conn = null; 
?> 

表模式

1 IDPrimaryIndex int(255)   No None  Change Change Drop Drop 
Primary Primary 
Unique Unique 
Index Index 
Spatial Spatial 
Fulltext Fulltext 
More 
    2 UserName varchar(255)   No None  Change Change Drop Drop 
Primary Primary 
Unique Unique 
Index Index 
Spatial Spatial 
Fulltext Fulltext 
More 
    3 Message varchar(255)   No None  Change Change Drop Drop 
Primary Primary 
Unique Unique 
Index Index 
Spatial Spatial 
Fulltext Fulltext 
More 
    4 Score int(255)   No None  Change Change Drop Drop 
Primary Primary 
Unique Unique 
Index Index 
Spatial Spatial 
Fulltext Fulltext 
More 
+0

請發表您的表模式。 –

+0

你可以檢查這個答案http://stackoverflow.com/a/18942684/1548932 – Imanez

+0

啊,是的! A_I是我唯一缺少的東西,現在就進行測試! :D –

回答

0

你必須定義你的id列是使用自動增量主鍵字段:

ALTER TABLE adminmessages CHANGE id id INT AUTO_INCREMENT PRIMARY KEY;