2015-12-08 174 views
-1

我的.php頁面已成功連接到mySql數據庫。它可以看到表格並從表格中拉出來,但不會將我的php表單中的文本框中的數據保存到數據庫中。我無法將我的PHP數據保存到MySQL數據庫

的config.php

 <?php 


    try{ 
     $db = new PDO("mysql:host=localhost;dbname=nolarec;port=3307","root",""); 
     $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 
     $db->exec("SET NAMES 'utf8'"); 
} catch(Exception $e) { 
     echo $e->getMessage(); 
     exit; 
} 

?> 

fball_event.php

 <form method="post" action="fball_create.php"> 
      <input type="hidden" name="submit" value="true"> 
        <fieldset> 
        <legend>New Event</legend> 
        Id: <input type="text" name="id"/> <br/> 
        Name: <input type="text" name="name"/> <br/> 
        Time: <input type="text" name="time"/> <br/> 
        Type: <input type="text" name="type"/> <br/> 
        </fieldset> 
        <br /> 
      <input type="submit" value="Create New Event" /> 
     </form> 



      <?php 

      require_once('config.php'); 


      if (isset($_POST['submit'])){ 

       include ('config.php'); 



       $id = $_POST['id']; 
       $name = $_POST['name']; 
       $time = $_POST['time']; 
       $type = $_POST['type']; 
       $results = $db->prepare ("INSERT INTO nolarec.fball_event (id, name, time, type) VALUES ('$id','$name','$time','$type')"); 




      } 
      ?> 
+3

請閱讀有關PDO準備語句的手冊。它都在那裏。 http://php.net/pdo.prepared-statements –

+2

如果你只是想將值串聯到SQL字符串中,你真的沒有太多準備好的語句。 –

+0

謝謝,我會考慮修復我準備好的陳述。這就是爲什麼我的數據沒有到數據庫的原因是,只是不好的編碼? @ Fred-ii- –

回答

1

首先,你應該使用佔位符在查詢數據輸入的所有的,所有第二,你需要實際執行它,你已經準備好了。請嘗試:

$id = $_POST['id']; 
$name = $_POST['name']; 
$time = $_POST['time']; 
$type = $_POST['type']; 

$results = $db->prepare ("INSERT INTO nolarec.fball_event (id, name, time, type) VALUES (:id,:name,:time,:type)"); 

$results->bindValue(":id", $id); 
$results->bindValue(":name", $name); 
$results->bindValue(":time", $time); 
$results->bindValue(":type", $type); 
$results->execute(); 
+0

再次看看這個'(':id',':name',':time',':type')' - 編輯; ;-) –

+0

看到,只要我張貼它大聲笑,知道我不能改變它之前有人評論:)謝謝雖然 –

+0

是的,以防萬一OP看到,並認爲這是正確的語法。一切都好;-) –

相關問題