dongzhi6905 2017-12-20 20:02
浏览 22
已采纳

PDO插入一列

Really idk whats wrong here every time I try to insert into 1 signal column?

Database connected very well, table name is stad, and it contains two columns id AI PRIMARY and name unique

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

connection file

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

?>

Database:

enter image description here

  • 写回答

3条回答 默认 最新

  • dsg435665475 2017-12-20 20:04
    关注

    You're missing brackets around your column list and your value list (even if it's just one column, those brackets are required):

    $sql = 'INSERT INTO stad (name) VALUES (:name)';
    $stmt = $pdo->prepare($sql);
    $stmt->execute(['name' =>  $stadName]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡