douliao5467 2015-10-23 13:25
浏览 17
已采纳

PDI插入循环

I have a form, containing skill name and skill level fields. This can be dynamically expanded by js so basically they are skill_name[] and skill_level[]. The point is, that I would like to insert these into the database, into separate records.

I have a piece of code, which supposed to do this but not working:

$skill_name         = $_POST['skill_name'];
$skill_level        = $_POST['skill_level'];    

for ($is = 0; $is < count($skill_name); $is++) {
        if(!empty($skill_name[$is])) {
            $inserting  = $db->prepare("insert into position_skills 
                (`type`,`pid`,`name`,`general`)
                values 
                ('skill', :pid, :name, :general");
                $inserting  ->execute(
                array(
                ':pid'          => $last_id,
                ':name'         => $skill_name[$is],
                ':general'      => $skill_level[$is]
                )); 
        }
    } 

The error message is the following:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4' in /home1/xx/public_html/xx/post.php:47 Stack trace: #0 /home1/xx/public_html/xx/post.php(47): PDOStatement->execute(Array) #1 {main} thrown in /home1/xx/public_html/xx/post.php on line 47

The line 47 is the end of the arrays.

I am totally new to PDO, but other insert is working like a charm. What can be the problem? Thanks in advance

  • 写回答

1条回答 默认 最新

  • duangai9678 2015-10-23 13:30
    关注

    Typo... unclosed parentheses.

    /***/
    $inserting  = $db->prepare("insert into position_skills 
                   (`type`,`pid`,`name`,`general`)
                   values 
                   ('skill', :pid, :name, :general)
                  ");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器