1 回答
TA贡献1786条经验 获得超11个赞
这里的问题是 PHP POST 数据是字符串,因此您需要在插入之前对其进行转换。
<--PDO code-->
<?php
$pdo = new PDO('mysql:host=localhost;port=8888;dbname=xxxx',
'xxx', 'xxx');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//SQL
$sql = "INSERT INTO cars (make, model, year, mileage)
VALUES (:make, :model, :year, :mileage)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':make' => $_POST['make'],
':model' => $_POST['model'],
':year' => intval($_POST['year']),
':mileage' => intval($_POST['mileage'])));
$_SESSION['success'] = 'Record Added';
header( 'Location: index.php' ) ;
return;
?>
<form method="post">
<p>Make:
<input type="text" name="make"></p>
<p>Model:
<input type="text" name="model"></p>
<p>Year:
<input type="number" name="year"></p>
<p>Mileage:
<input type="number" name="mileage"></p>
<p><input type="submit" value="Add New"/>
- 1 回答
- 0 关注
- 87 浏览
添加回答
举报