swipeales swipeales - 5 months ago 17
PHP Question

SQLSTATE[23000]: Integrity constraint violation: 1048 Column

I am getting this error, i make sure all names are correct and idk what's wrong, seems like nothing is added in rsn column. Been searching around and i know issue but i don't know what is causing my issue and if you can comment my code if it's injectable, first time using 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">&nbsp;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">&nbsp;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

enter image description here

Answer

As your error states, Integrity constraint violation: 1048 Column 'rsn' cannot be null in, so you will need to always check if the value of rsn is empty before you try to insert the data on the table.

You can do this by this way on your PHP code:

<?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;
    }

}