我的.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')");
}
?>
請閱讀有關PDO準備語句的手冊。它都在那裏。 http://php.net/pdo.prepared-statements –
如果你只是想將值串聯到SQL字符串中,你真的沒有太多準備好的語句。 –
謝謝,我會考慮修復我準備好的陳述。這就是爲什麼我的數據沒有到數據庫的原因是,只是不好的編碼? @ Fred-ii- –