PDO插入一列

时间:2017-12-20 20:02:19

标签: php mysql pdo

每次我尝试插入1个信号列时,真的很不对劲吗?

数据库连接得很好,表名为stad,它包含两列id AI PRIMARY和name唯一

<?php
    @$submit = $_POST['submit'] ;
    @$stadName = $_POST['stadName'];
    if(isset($submit)){
       if(!$stadName){
         echo "Error" ;
       }else{
         $sql = 'INSERT INTO stad name VALUES :name';
         $stmt = $pdo->prepare($sql);
         $stmt->execute(['name' =>  $stadName]);
         echo "DONE" ;
       }
    }
    ?>

连接文件

<?php
$serv = '127.0.0.1';
$user = 'root';
$pass = 'root';
$dbname = 'akfoot';

// db base connection
$pdo = new PDO('mysql:dbname='.$dbname.';host='.$serv.'; charset=utf8',$user,$pass);

?>

数据库:

enter image description here

3 个答案:

答案 0 :(得分:2)

您的列列表和值列表中缺少括号(即使它只是一列,也需要这些括号):

$sql = 'INSERT INTO stad (name) VALUES (:name)';
$stmt = $pdo->prepare($sql);
$stmt->execute(['name' =>  $stadName]);

答案 1 :(得分:-1)

<?php
include "connect.php" ;

@$submit = $_POST['submit'] ;
@$stadName = $_POST['stadName'];
   if(isset($submit)){
if(!$stadName){
   echo "error" ;
}else{
   $sql = 'INSERT INTO stad (name) VALUES (:name)';
$stmt = $pdo->prepare($sql);
$stmt->execute(['name' =>  $stadName]);
echo "done" ;
}
}
?>

data base 这是连接文件

<?php
$serv = '127.0.0.1';
$user = 'root';
$pass = 'root';
$dbname = 'akfoot';

// db base connection
$pdo = new PDO('mysql:dbname='.$dbname.';host='.$serv.'; charset=utf8',$user,$pass);

?>

答案 2 :(得分:-1)

问题是数据类型是阿拉伯语和我使用其他编码类型,我必须使用UTF8 bin而不是拉丁语

感谢你一定知道我犯了什么错误