2013-11-24 48 views
0

我试图在将值插入数据库时​​使用预准备语句。准备好的语句不能正常工作

 $this->db = new Database(); 

    if(!empty($_POST['first_name'])){ 
     $this->first_name = $_POST['first_name']; 
    } 
    if(!empty($_POST['second_name'])){ 
     $this->second_name = $_POST['second_name']; 
    } 
    if(!empty($_POST['last_name'])){ 
     $this->last_name = $_POST['last_name']; 
    } 
    if(!empty($_POST['course'])){ 
     $this->course = $_POST['course']; 
    } 
    if(!empty($_POST['math'])){ 
     $this->math = $_POST['math']; 
    } 
    if(!empty($_POST['programming'])){ 
     $this->programming = $_POST['programming']; 
    } 
    if(!empty($_POST['english'])){ 
     $this->english = $_POST['english']; 
    } 
    if(!empty($_POST['history'])){ 
     $this->history = $_POST['history']; 
    } 
    try { 
    $this->stmt = $this->db->dbh->prepare("INSERT INTO students (first_name,second_name,last_name,course) VALUES (':first_name',':second_name',':last_name',':course')"); 
    $this->stmt->bindValue(':first_name', $first_name, PDO::PARAM_INT); 
    $this->stmt->bindValue(':second_name', $second_name, PDO::PARAM_STR); 
    $this->stmt->bindValue(':last_name', $last_name, PDO::PARAM_STR); 
    $this->stmt->bindValue(':course', $course, PDO::PARAM_STR); 
    $this->stmt->execute(); 
    //$this->db->insertQuery("INSERT INTO objects (student_id,math,programming,english,history) VALUES ('','".$this->math."','".$this->programming."','".$this->english."','".$this->history."')"); 
    //$data = $this->db->stmt->fetchAll($q); 
    //$this->view->render('index','template',$data); 
    } catch(PDOException $e) { 
     echo 'ERROR: ' . $e->getMessage(); 
     } 

它不工作,表中的值是:first_name,:second_name,:third_name。这里有什么问题?

感谢

+0

http://php.net/manual/en/pdo.prepared-statements.php - 下面的例子中,你会解决您的代码 – Artur

+1

您不必在准备好的语句中引用占位符的引号。 –

+0

是的,谢谢特雷斯科。它工作 – user3026704

回答

0

预处理语句做工精细,它是你的代码没有。

变化

$this->db->insertQuery("INSERT INTO students (id,first_name,second_name,last_name,course) VALUES ('',':first_name',':second_name',':last_name',':course')",array(':first_name'=>$this->first_name,':second_name'=>$this->second_name,':last_name'=>$this->last_name,':course'=>$this->course)); 

$this->db->insertQuery("INSERT INTO students (id,first_name,second_name,last_name,course) VALUES ('',':first_name',':second_name',':last_name',':course')",array('first_name'=>$this->first_name,'second_name'=>$this->second_name,'last_name'=>$this->last_name,'course'=>$this->course)); 

通知的:的缺失值的阵列中通过。

+0

嗯..我照你说的,但仍然插入:first_name,:second_name ... – user3026704

+0

你[绑定参数](http://php.net/manual/en/pdo.prepared-statements .php)在任何地方? – vascowhite

+0

不,我不。有一个例子$ stmt-> bindParam(':name',$ name);.我应该从哪里得到$姓名? – user3026704

0

从数组键中删除“:”。从查询

0

你可以试试这个,我已删除ID列

$this->db->insertQuery("INSERT INTO students (first_name,second_name,last_name, course)  
VALUES (':first_name',':second_name',':last_name',':course')", 
array('first_name'=>$this->first_name,'second_name'=>$this->second_name, 
'last_name'=>$this->last_name,'course'=>$this->course)); 
相关问题