SQLSTATE[23000]: Integrity constraint violation: 1048 Column - php

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"> 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

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

Related

Cannot bind date value using bindvalue function with PDO::PARAM_STR

So, I cannot find the solution to the problem I'm having. I'm really new to coding but learned how to start coding using basic HTML, PHP, PDO, and AJAX. So my problem comes from a form that retrieves dates from a calendar using the type=date from the form. The code of the form is down below.
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title text-center"><i class="fa fa-bar-chart fa-fw"></i> Ingreso de reporte</h3>
</div>
<div id="alert_success" class="panel-body">
<br>
<form method="post" class="form-horizontal" role="form" action="ajax_form_post.php" id="insertreport">
<div class="form-group">
<label class="control-label col-sm-2" for="video" style="color:#777;">ID de video</label>
<div class="col-sm-10">
<input type="text" name="video" class="form-control" id="video" placeholder="Ingresa id del video" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date_i" style="color:#777;">Fecha de arriendo</label>
<div class="col-sm-10">
<input type="date" name="date_i" class="form-control" id="date_i" placeholder="" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date_f" style="color:#777;">Fecha de devolución</label>
<div class="col-sm-10">
<input type="date" name="date_f" class="form-control" id="date_f" placeholder="" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="hidden" name="c_id" class="form-control" id="user_id" value="<?php echo $id ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" name="update_customer" value="Enviar" id="submitdata">
</div>
</div>
</form>
<div class="text-right">
<i class="fa fa-arrow-circle-right"></i>
</div>
</div>
</div>
</div>
Now the problem starts with this Ajax form I built. BTW the script is working fine, the problem is inside this set of code.
<?php
/****************Get customer info to ajax *******************/
//require database class files
require("includes/pdocon.php");
//instatiating our database objects
$db = new Pdocon ;
if(isset($_POST['c_id'])){
$id = $_POST['c_id'];
$date_i = date("Y-m-d", strtotime($_POST['date_i']));
$date_f = date("Y-m-d", strtotime($_POST['date_f']));
$raw_v_id = clean_data($_POST['video']);
$v_id = val_int($raw_v_id);
$db->query('SELECT * FROM videos WHERE v_id = :v_id');
$db->bindvalue(':v_id', $v_id, PDO::PARAM_INT);
$row = $db->fetchSingle();
$db->query('INSERT INTO arriendo (transaccion, c_id, v_id, f_arriendo, f_devolucion)
VALUES (NULL, :c_id, :v_id :f_arriendo, :f_devolucion)');
$db->bindvalue(':f_arriendo', $date_i, PDO::PARAM_STR);
$db->bindvalue(':f_devolucion', $date_f, PDO::PARAM_STR);
$db->bindvalue(':c_id', $id, PDO::PARAM_INT);
$db->bindvalue(':v_id', $v_id, PDO::PARAM_INT);
$run = $db->execute();
}
if($run){
echo "<p class='bg-success text-center' style='font-weight:bold;'>Valor actualizado </p>";
}
?>
I get the following error:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''2021-08-05', '2021-08-06')' at line 2
Any help or a little guidance would be greatly appreciated. Thanks in advance.

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cne' cannot be null

I got an error while saving data.
Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cne' cannot be null (SQL: insert into `students` (`cne`, `firstName`, `secondName`, `age`, `speciality`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, 2020-04-03 20:47:34, 2020-04-03 20:47:34))
I wonder what I could do to fix that.
Route
Route::get('/',"StudentController#index") ;
Route::get('/edit/{id}',"StudentController#edit") ;
Route::get('/show/{id}',"StudentController#show") ;
Route::get('/create',"StudentController#create") ;
Route::get('/store',"StudentController#store") ;
Route::get('/update/{id}',"StudentController#update") ;
Controller
public function store(Request $request )
{
$student = new Student() ;
$student->cne = $request->input('cne') ;
$student->firstName = $request->input('firstName') ;
$student->secondName = $request->input('secondName') ;
$student->age = $request->input('age') ;
$student->speciality = $request->input('speciality') ;
$student->save() ;
return redirect('/') ;
//
}
HTML
#if($layout == 'index')
<div class="container-fluid">
<div class="row">
<selctio class="col">
#include("studentslist")
</selction>
<selction class="col"></selction>
</div>
</div>
#elseif($layout == 'create')
<div class="container-fluid" id="create-form">
<div class="row">
<selctio class="col">
#include("studentslist")
</selction>
<selction class="col">
<form action="{{ url('/store') }}" method="POST" >
#csrf
<div class="form-group">
<label>CNE</label>
<input name="cne" id="cne" type="text" class="form-control" placeholder="Enter cne">
</div>
<div class="form-group">
<label>First Name</label>
<input name="firstName" id="firstName" type="text" class="form-control" placeholder="Enter the first name">
</div>
<div class="form-group">
<label>second Name</label>
<input name="secondName" type="text" class="form-control" placeholder="Enter second name">
</div>
<div class="form-group">
<label>Age</label>
<input name="age" type="text" class="form-control" placeholder="Enter the Age">
</div>
<div class="form-group">
<label>Speciality</label>
<input name="speciality" type="text" class="form-control" placeholder="Enter Sepeciality">
</div>
<input type="submit" class="btn btn-info" value="Save">
<input type="reset" class="btn btn-warning" value="Reset">
</form>
</selction>
</div>
</div>
#elseif($layout == 'show')
<div class="container-fluid">
<div class="row">
<selctio class="col">
#include("studentslist")
</selction>
<selction class="col"></selction>
</div>
</div>
#elseif($layout == 'edit')
<div class="container-fluid">
<div class="row">
<section class="col-md-7">
#include("studentslist")
</section>
<section class="col-md-5">
</section>
</div>
</div>
#endif
The "1048 Column' cannot be null" error is triggered when a column.. cannot be null.
You should take a look at your migration file. Add the nullable method like this:
$table->string('cne')->nullable();
If you do not want to rewrite your migration, you will need to use something like that:
$table->string('cne')->nullable()->change();
Futhermore, change the Route file so that the store route uses the post method:
Route::post('/store',"StudentController#store") ;

Bind param not working

I'm trying to update informations in my database
this is the html page:
<form method="post" action="update.php">
<fieldset>
<legend>Modifier informations:</legend>
<div class="row">
<div class="col-sm-3">
<label class="form-control">Nom</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="text" name="title">
</div>
</div>
<div class="row">
<div class="col-sm-3">
<label class="form-control">Adresse</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="text" name="adresse">
</div>
</div>
<div class="row">
<div class="col-sm-3">
<label class="form-control">Date fe FONDATION</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="date" name="date_creation">
</div>
</div>
<div class="row">
<div class="col-sm-3">
<label class="form-control">Détails</label>
</div>
<div class="col-sm-9">
<textarea class="form-control" name="details"></textarea>
</div>
</div>
<br>
<button style="float: right" type="submit" class="btn btn-primary">Valider</button>
</div>
</fieldset>
</form>
</div>
this is update.php
<?php
require "DB/config.php";
include "Service/Association.php";
/*require '../connected.php';*/
session_start();
$ASS = new Association("1",$_POST["title"],$_POST["adresse"],$_POST["details"],$_POST["date_creation"]);
$c=new config();
$conn = $c->getConnexion();
$ASS->ModifierAssociation($ASS,$conn);
and this is the method in my association class:
function ModifierAssociation($Animaux,$conn){
try {
$stmt = $conn->prepare("update `association`( `nom`, `adresse`, `details`, `date_creation` )VALUES(:nom,:adresse,:details,:date_creation)");
$nom=$Animaux->getnom();
$stmt->bindParam(':nom',$nom);
$adresse=$Animaux->getadresse();
$stmt->bindParam(':adresse',$adresse);
$details=$Animaux->getdetails();
$stmt->bindParam(':details',$details);
$date_creation=$Animaux->getdate_creation();
$stmt->bindParam(':date_creation',$date_creation);
print_r($stmt);
$stmt->execute();
}catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
}
i get this error
PDOStatement Object ( [queryString] => update association( nom, adresse, details, date_creation )VALUES(:nom,:adresse,:details,:date_creation) )
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '( nom, adresse, details, date_creation )VALUES('1','','','')' at line 1
The syntax for MySQL UPDATE is:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
So you need:
$stmt = $conn->prepare(
"UPDATE `association` SET
`nom` =:nom,
`adresse` = :adresse,
`details` = :details,
`date_creation` = :date_creation");
Note this will update the entire association table so you should identify the rows you want to update with the WHERE clause.

Failing to insert data from a form into a database using PDO

Im trying to get my data from a form into a database, i've used a tutorial online to do so, everything works right to the point where it has to actually get it to the database i just cant find what's wrong with it
$kenteken = $_POST['kenteken'];
$werkplaatsnr = $_POST['werkplaatsnr'];
$datum = $_POST['datum'];
$medewerker = $_POST['medewerker'];
$pdoQuery = "INSERT INTO `WPOnderhoud`(`kenteken`, `werkplaats`, `datum`, `medewerker`) VALUES (:kenteken,:werkplaatsnr,:datum,:medewerker)";
$pdoResult = $pdoConnect->prepare($pdoQuery);
$pdoExec = $pdoResult->execute(array(":kenteken"=>$kenteken,":werkplaatsnr"=>$werkplaatsnr,":datum"=>$datum,":medewerker"=>$medewerker));
if($pdoExec)
{
echo 'Onderhoudsbeurt ingevoerd';
} else {
echo 'Er ging iets mis';
}
}
this is my code, it's not all because above it is the connection to the database but since it does connect i don't see any wrong in it.
the $pdoConnect is the database connection
this is the mentioned form:
<form action="onderhoud.php" method="POST">
<div class="form-group">
<label class="control-label">Kenteken</label>
<input type="text" class="form-control" name="kenteken">
</div>
<div class="form-group">
<label class="control-label">Merk</label>
<input type="text" class="form-control" name="merk">
</div>
<div class="form-group">
<label class="control-label">Werkplaats nr</label>
<input type="number" class="form-control" name="werkplaatsnr">
</div>
<div class="form-group">
<label class="control-label">Datum</label>
<input type="date" class="form-control" name="datum">
</div>
<div class="form-group">
<label class="control-label">Medewerker</label>
<select class="form-control" id="state_id">
<option>R. Krol</option>
<option>B. de Vries</option>
<option>J. Jansen</option>
<option>P .Bakker</option>
</select>
</div>
<div class="form-group">
<button type="submit" name="insert" class="btn btn-primary">Invoeren</button>
</div>
</form>
Try below,
$pdoExec = $pdoConnect->prepare("INSERT INTO WPOnderhoud values('',?,?,?,?)");
$pdoExec ->bindParam(1,$kenteken);
$pdoExec ->bindParam(2,$werkplaatsnr);
$pdoExec ->bindParam(3,$datum);
$pdoExec ->bindParam(4,$medewerker);
$pdoExec ->execute();
Hope will help you.

sql update not executing

Apologies if there is another feed with this same problem, I have tried different suggested solutions but I still get an error, and I cant see why!
I want to update a row in my table using a html form. I have populated the form with the existing values, and want to be able to edit those and update them when the form is submitted, but I am getting this error:
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'
in
/Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php:46
Stack trace: #0
/Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php(46):
PDOStatement->execute(Array) #1 {main} thrown in
/Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php
on line 46
Here is the php / sql code:
if(isset($_POST['update'])) {
$updateTitle = trim($_POST['title']);
$updateDesc = trim($_POST['desc']);
$updateRedeem = trim($_POST['redeem']);
$updateStart = trim($_POST['start']);
$updateExpiry = trim($_POST['expiry']);
$updateCode = trim($_POST['code']);
$updateTerms = trim($_POST['terms']);
$updateImage = trim($_POST['image']);
$updateUrl = trim($_POST['url']);
$updateSql = 'UPDATE codes SET (title,description,redemption,start,expiry,textcode,terms,image,url) = (:title,:description,:redeem,:start,:exp,:code,:terms,:image,:url) WHERE id=:offerid';
$update = $db->prepare($updateSql);
$update->execute(array(':title'=>$updateTitle,':description'=>$updateDesc,':redeem'=>$updateRedeem,':start'=>$updateStart,':exp'=>$updateExpiry,':code'=>$updateCode,':terms'=>$updateTerms,':image'=>$updateImage,':url'=>$updateUrl,':id'=>$offerID));
}
and the html form:
<form id="update_offer" class="col-md-6 col-md-offset-3" method="post" action="update_offer.php?id=<?php echo $offerID; ?>">
<div class="form-group col-md-12">
<label class="col-md-12" for="title">Title</label>
<input id="title" class="form-control col-md-12" type="text" name="title" placeholder="Offer Title" value="<?php echo $title; ?>" required>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="desc">Description</label>
<textarea id="desc" class="form-control col-md-12" name="desc" placeholder="Description" value="<?php echo $desc; ?>"></textarea>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="redeem">Redemption</label>
<input id="redeem" class="form-control col-md-12" type="text" name="redeem" placeholder="Where to redeem" value="<?php echo $redeem; ?>" required>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="start">Start Date</label>
<input id="start" class="form-control col-md-12" type="date" name="start" value="<?php echo $startDate->format('Y-m-d'); ?>" min="<?php echo date('Y-m-d') ?>" max="2021-12-31" required>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="expiry">Expiry Date</label>
<input id="expiry" class="form-control col-md-12" type="date" name="expiry" value="<?php echo $expDate->format('Y-m-d'); ?>" min="<?php echo date('Y-m-d') ?>" max="2021-12-31" required>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="code">Code</label>
<input id="code" class="form-control col-md-12" type="text" name="code" placeholder="Code (if applicable)" value="<?php echo $code; ?>">
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="terms">Terms</label>
<textarea id="terms" class="form-control col-md-12" name="terms" placeholder="Terms & Conditions" value="<?php echo $terms; ?>" required></textarea>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="url">Offer URL</label>
<input id="url" class="form-control col-md-12" type="text" name="url" placeholder="Offer URL (if applicable)" value="<?php echo $url; ?>">
</div>
<div class="form-group col-md-12">
<label class="col-md-8" for="image">Image <img src="../images/offers/<?php echo $image; ?>" alt="" style="width: 200px;" /></label>
<input id="image" class="form-control col-md-4" type="file" name="image">
</div>
<div class="form-group col-md-12 pull-right">
<button id="update" type="submit" name="update" class="btn btn-primary"><i class="glyphicon glyphicon-refresh"></i> Update</button>
</div>
</form>
what am i doing wrong?! Im still learning php etc, so please be gentle, any help is much appreciated.
First, you have wrong syntax for update statement, as other guys mentioned already, change:
UPDATE codes SET (title,description,redemption,start,expiry,textcode,terms,image,url) = (:title,:description,:redeem,:start,:exp,:code,:terms,:image,:url) WHERE id=:offerid
Into
UPDATE `codes`
SET `title` = :title,
`description` = :description,
`redemption` = :redeem,
`start` = :start
`expiry` = :expiry
`textcode` = :code
`terms` = :terms
`image` = :image
`url` = :url
WHERE `id` = :offerid
Learn more about the SQL Update syntax here.
Then, one thing more you have a mistake in execute(). Change your :id into :offerid like below:
$update->execute(array(
':title' => $updateTitle,
':description' => $updateDesc,
':redeem' => $updateRedeem,
':start' => $updateStart,
':exp' => $updateExpiry,
':code' => $updateCode,
':terms' => $updateTerms,
':image' => $updateImage,
':url' => $updateUrl,
':offerid' => $offerID
));
You are using wrong syntax of Update
It would be
$updateSql = "UPDATE codes SET title =:title,
description =:description,
redemption =:redeem,
start =:start,
expiry =:exp,
textcode =:code,
terms :=terms,image =:image,
url =:url
WHERE id=:id";// write id instead of offset because you are binding ':id'=>$offerID
Check http://dev.mysql.com/doc/refman/5.7/en/update.html
thanks for your replies. My original update syntax was actually as you mentioned, I had changed it when looking through some other solutions, and not changed it back, but either way, even with correct syntax, I still got the same error.
Looking through your replies, I can see that I have ':id'=> $offerID but have used :offerid in the sql code, which obviously needs to be updated, so thanks for pointing that out! Hopefully that will fix the problem...

Categories