dongzhan8620
2014-07-07 16:49
浏览 64
已采纳

使用PDO将数据插入数据库

I'm trying to use PDO to insert data into my database but I'm getting the error "0 results". I already used PDO to select data from my database but this is the first time I use it to insert data so any help with the below code would be appreciated!

My form:

<html>
<body>

<form name="blog post" action="insert.php" method="post">

<label for "id">Id: </label>
<input type="text" name="id">
<br>    
<label for "title">Title: </label>
<input type="text" name="title">
<br>    
<label for "year">Year: </label>
<input type="text" name="year">
<br>    
<label for "text">Text: </label>
<textarea rows="10" cols="50" name="text"></textarea>
<br>    
<button type="submit">Submit</button>
</form>

</body>
</html>

My insert.php code:

<?php

$pdo = new PDO('mysql:host=localhost;dbname=dbexample', 'userexample', 'paswexample', array(\PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES utf8;SET time_zone = 'Europe/London'"));

$sql = "INSERT INTO `tableexample` (id, title, year, text)
                                        VALUES (:id, :title, :year, :text)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":id", $id);
$stmt->bindParam(":title", $title);
$stmt->bindParam(":year", $year);
$stmt->bindParam(":text", $text);

$form = $_POST;
$id = $form[ 'id' ];                    
$title= $form[ 'title' ];                   
$year= $form[ 'year' ];                 
$text= $form[ 'text' ];                 


$stmt->execute();

$result = $stmt->execute(array(':id'=>$id, ':title'=>$title, ':year'=>$year, ':text'=>$text));

if($result) {
    echo "Your text has been posted";

    }// end if
else {
    echo '0 results';
    }// end else

?>

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用PDO将数据插入我的数据库,但我收到的错误是“0结果” 。 我已经使用PDO从我的数据库中选择数据,但这是我第一次使用它来插入数据,所以对以下代码的任何帮助都将不胜感激! </ p>

我的表单:</ strong> </ p>

 &lt; html&gt; 
&lt; body&gt; 
 
&lt;表单 name =“blog post”action =“insert.php”method =“post”&gt; 
 
&lt; label for“id”&gt; Id:&lt; / label&gt; 
&lt; input type =“text”name =  “ID” &GT; 
&LT峰; br&GT;  
&lt; label for“title”&gt;标题:&lt; / label&gt; 
&lt; input type =“text”name =“title”&gt; 
&lt; br&gt;  
&lt;标签为“年”&gt;年:&lt; / label&gt; 
&lt; input type =“text”name =“year”&gt; 
&lt; br&gt;  
&lt;标签为“text”&gt;文字:&lt; / label&gt; 
&lt; textarea rows =“10”cols =“50”name =“text”&gt;&lt; / textarea&gt; 
&lt; br&gt;  
&lt; button type =“submit”&gt;提交&lt; / button&gt; 
&lt; / form&gt; 
 
&lt; / body&gt; 
&lt; / html&gt; 
 </ code> </ pre> 
 
  

我的insert.php代码:</ strong> </ p>

 &lt;?php 
 
 $ pdo = new PDO('mysql:host = localhost  ; dbname = dbexample','userexample','paswexample',array(\ PDO :: MYSQL_ATTR_INIT_COMMAND =&gt;“SET NAMES utf8; SET time_zone ='Europe / London'”)); 
 
 $ sql =“INSERT  INTO`tableexample`(id,title,year,text)
 VALUES(:id,:title,:year,:text)“; 
 $ stmt = $ pdo-&gt; prepare($ sql); 
 $  stmt-&gt; bindParam(“:id”,$ id); 
 $ stmt-&gt; bindParam(“:title”,$ title); 
 $ stmt-&gt; bindParam(“:year”,$ year)  ; 
 $ stmt-&gt; bindParam(“:text”,$ text); 
 
 $ form = $ _POST; 
 $ id = $ form ['id'];  
 $ title = $ form ['title'];  
 $ year = $ form ['year'];  
 $ text = $ form ['text'];  
 
 
 $ stmt-&gt; execute(); 
 
 $ result = $ stmt-&gt; execute(array(':id'=&gt; $ id,':title'=&gt; $ title  ,':year'=&gt; $ year,':text'=&gt; $ text)); 
 
if($ result){
 echo“您的文字已发布”; 
 
} // 结束如果
else {
 echo'0 results'; 
} // end else 
 
?&gt; 
 </ code> </ pre> 
 </ div>

2条回答 默认 最新

相关推荐 更多相似问题