我收到此错误,我确保所有名称都正确并且idk有什么不对,似乎在rsn列中没有添加任何内容。一直在寻找,我知道问题,但我不知道是什么导致我的问题,如果你可以注释我的代码,如果它是可注射的,第一次使用PDO。
HTML
<form action="rsdenar.php" method="post">
<div id="gold-calc">
<div class="col-md-4">
<label for="amount"><h3><i class="fa fa-database"> Kolicina</i></h3></label>
<input type="text" class="form-control" id="amount" name="gpamount">
</div>
<div class="col-md-4">
<select class="form-control" style="margin-top:30px; width: 70%;" id="goldtype">
<option value="0.5">RS3</option>
<option value="1.6">RS 07</option>
</select>
</div>
<div class="col-md-4">
<label for="price"><h3><i class="fa fa-database"> Cena</i></h3></label>
<input type="text" class="form-control" id="price">
</div>
<div class="row" style="padding-top: 170px;">
<label for="idrsn">RSN: </label>
<input type="text" class="form-control" id="idrsn" name="rsn" style="width: 40%">
</div>
<div class="row">
<label for="emailbuy">Email: </label>
<input type="text" class="form-control" id="emailbuy" name="email-nakup" style="width: 40%;">
</div>
<div class="buy-order">
<button type="submit" class="btn btn-primary"><a style="text-decoration: none" href="#"><h5 style="font-family: arial; font-size: 20px">NAKUP</h5></a></button>
</div>
</div>
</form>
PHP
<?php
include 'php_includes/db_connect.php';
try {
$stmt=$conn->prepare("INSERT INTO purchase (rsn,email,amount,unique_id)
VALUES (:rsn, :email, :amount, :unique_id)");
$stmt->bindParam(':rsn', $_POST['rsn']);
$stmt->bindParam(':email', $_POST['email-nakup']);
$stmt->bindParam(':amount', $_POST['gpamount']);
$stmt->bindParam(':unique_id', $_POST['unique_id']);
$stmt->execute();
}catch (exception $e){
echo $e;
}
?>
SQL
答案 0 :(得分:1)
正如您的错误所述,Integrity constraint violation: 1048 Column 'rsn' cannot be null in
,因此在尝试在表格中插入数据之前,您需要始终检查rsn
的值是否为空。
您可以通过这种方式在PHP代码上执行此操作:
<?php
// validation added here
if(isset($_POST) && !empty($_POST['rsn'])) {
try {
$stmt=$conn->prepare("INSERT INTO purchase (rsn,email,amount,unique_id) VALUES (:rsn, :email, :amount, :unique_id)");
$stmt->bindParam(':rsn', $_POST['rsn']);
$stmt->bindParam(':email', $_POST['email-nakup']);
$stmt->bindParam(':amount', $_POST['gpamount']);
$stmt->bindParam(':unique_id', $_POST['unique_id']);
$stmt->execute();
}catch (exception $e){
echo $e;
}
}