我想从表单中获取数据以使用PDO进入我的SQL数据库这个相同的代码一直在为我工作。代码会发布到此页面,但我不知道我做错了什么或者已经改变以使其不进入数据库。我从phpMyAdmin
获得了插入代码INSERT INTO
car-subclass-desc
(sub-model-id
,car-model
,sub-model-name
,sub-engine
,sub-hp
,sub-top-speed
,{{1 }},sub-0_to_60
,sub-tourque
,sub-weight
,sub-model-desc
)VALUES([value-1],[value-2],[value-3],[value- 4],[值-5],[值-6],[值-7],[值-8],[值-9],[值-10],[值-11])
我修改了它并删除了sub-model-id,因为这里的主键是不包括我的数据库登录信息的代码。
sub-production-start-price
答案 0 :(得分:0)
使用try catch aswell !!
$form = $_POST;
$sub_name = $form[ 'car-sub-name' ];
$engine = $form[ 'engine' ];
$hp = $form[ 'hp' ];
$topspeed = $form[ 'top-speed' ];
$zeroto = $form[ '0to60' ];
$tourqe = $form[ 'tourqe' ];
$weight = $form[ 'weight' ];
$desc = $form[ 'desc'];
$msrp = $form[ 'msrp' ];
$mname = $form[ 'mname' ];
try{
// query
$sql = "INSERT INTO car-subclass-desc(car-model, sub-model-name, sub-engine,
sub-hp, sub-top-speed, sub-0_to_60, sub-tourque, sub-weight, sub-model-desc,
sub-production-start-price) VALUES (:mname,:smname,:engine,:hp,:topspeed,:zeroto,:tourqe,:weight,:desc,:msrp)";
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn>beginTransaction();
$stmt = $conn->prepare($sql, array(PDO::FETCH_ASSOC));
$stmt->bindParam(':mname', $mname, PDO::PARAM_STR, 255);// fix your parameters values here
$stmt->bindParam(':smname', $smname, PDO::PARAM_INT); // fix your parameters values here
$stmt->bindParam(':engine', $engine, PDO::PARAM_STR, 30); // fix your parameters values here
$stmt->bindParam(':hp', $hp, PDO::PARAM_STR, 30); // fix your parameters values here
$stmt->bindParam(':topspeed', $topspeed, PDO::PARAM_STR, 255); // fix your parameters values here
$stmt->bindParam(':zeroto', $zeroto, PDO::PARAM_STR, 20); // fix your parameters values here
$stmt->bindParam(':tourqe', $tourqe, PDO::PARAM_STR, 30); // fix your parameters values here
$stmt->bindParam(':desc', $desc, PDO::PARAM_STR, 255); // fix your parameters values here
$stmt->bindParam(':desc', $desc, PDO::PARAM_STR, 20); // fix your parameters values here
$stmt->bindParam(':msrp', $msrp, PDO::PARAM_STR, 30); // fix your parameters values here
$stmt->execute();
$conn->commit();
$stmt = null;
}catch (Exception $e)
{
echo "error occured : " .$e;
$stmt = null;
$conn->rollback();
}