Bind param not working - php

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.

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.

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.

Inserting data into MySQL server

I'm doing a e-commerce admin panel and I need a quick script for inserting data into MySQL. Here's what i've done and it does nothing.
<form action="#" id="form_sample_1" class="form-horizontal" method="post">
<div class="control-group">
<label class="control-label">Package Name<span class="required">*</span></label>
<div class="controls">
<input type="text" name="pkg_name" data-required="1" class="span6 " value=""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Package Price <span class="required">*</span><small>(In Dollars)</small></label>
<div class="controls">
<input name="pkg_price" type="number" class="span6 " value=""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Package Contains</label>
<div class="controls">
<input name="pkg_contains" type="text" class="span6 " value=""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Your Password</label>
<div class="controls">
<input name="sifre" type="password" class="span6 " value=""/>
</div>
</div>
<div class="form-actions">
<button type="button"name="btn" class="btn btn-primary">Send request to server.</button>
</div>
</form>
<!-- END FORM-->
</div> <!--widget box light-grey end-->
<!-- Mass PHP starts here! -->
<?php
echo mysql_error();
include("include/baglan.php");
// set posts here.
$_POST['pkg_name'] = $pkg_name;
$_POST['pkg_price'] = $pkg_price;
$_POST['pkg_contains'] = $pkg_contains;
$sifre = mysql_real_escape_string(md5($_POST['sifre']));
if($_POST['btn'] and $_POST["sifre"] = $sifre){
mysql_query("INSERT INTO packages (pkg_name, pkg_price,pkg_contains) VALUES $pkg_name $pkg_price $pkg_contains");
echo "Success.";
}
else {
echo mysql_error();}
It returns nothing! I've re-written all code but nothing! please help me. The databae variables are;
id, auto incerment
pkg_name text
pkg_price int
pkg_contains mediumtext
Assign variable name should be the left side.
// set posts here.
$pkg_name=$_POST['pkg_name'];
$pkg_price=$_POST['pkg_price'];
$pkg_contains=$_POST['pkg_contains'];
Values() is function, put all vars in bracket and split them with ','.
mysql_query("INSERT INTO packages (pkg_name, pkg_price,pkg_contains) VALUES($pkg_name,$pkg_price,$pkg_contains)");

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

Mysql insert error from html form

I have built a database called jobs, and I am trying to insert data into it via an html form, that calls a php file. After submitting the form I see the following error in the console.
ERROR: Could not able to execute INSERT INTO jobs (id, title, pay, description, location, max_people, people_going, tasks, start_time, end_time, start, end)
VALUES (Default, 'testTitle', '4.00', 'testd', 'testl', '4', '1', 'testt', '13:00:00', '14:00:00', '2016-05-31 13:00:00', '2016-05-31 14:00:00').
I can manually input data through phpMyadmin, this only happens if I try to update the database via the form. I am relatively new to working with databases, so I am sure it is something very simple. I would appreciate any help that can be given.
The database is laid out as follows and nothing can be Null:
id type:int,auto increment
title type:varchar
pay type:decimal(15,2)
description type:text
location type:text
max_people type:int
people_going type:int
tasks:text
start_time type:time
end_time type:time
start type:datetime
end type:datetime
insert.php
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$host= "localhost";
$user= "";
$pass= "";
$link = mysql_connect($host, $user, $pass);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$job_title = $_POST['j_title'];
$job_pay = $_POST['j_pay'];
$job_start_time = $_POST['j_start'];
$job_end_time = $_POST['j_end'];
$original_job_date = $_POST['j_date'];
$job_summary = $_POST['j_description'];
$job_location = $_POST['j_location'];
$job_people = $_POST['j_people'];
$job_tasks = $_POST['j_tasks'];
$j_going=1;
$job_date=date('Y-m-d',strtotime("$original_job_date"));
$event_start= date('Y-m-d H:i:s', strtotime("$job_date $job_start_time"));
$event_end= date('Y-m-d H:i:s', strtotime("$job_date $job_end_time"));
// attempt insert query execution
$sql = "INSERT INTO jobs (id, title, pay, description, location, max_people, people_going, tasks, start_time, end_time, start, end) VALUES (DEFAULT, '$job_title', '$job_pay', '$job_summary', '$job_location', '$job_people', '$j_going', '$job_tasks', '$job_start_time', '$job_end_time', '$event_start', '$event_end')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
Form code from job_creation.html
<div class="container container-wide z-index">
<h2>Job Creation</h2>
<form class='rd-mailform row' id="job_form" method="post" action="insert.php">
<!-- RD Mailform Type -->
<input type="hidden" name="form-type" value="contact"/>
<!-- END RD Mailform Type -->
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_title">Job Title</label>
<input id="j_title"
type="text"
name="j_title"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_pay">Job Pay</label>
<input id="j_pay"
type="number"
min="0"
step="0.01"
data-number-to-fixed="2"
data-number-stepfactor="100"
name="j_pay"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_start">Job Start Time</label>
<input id="j_start"
class="time"
type="text"
name="j_start"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_end">Job End Time</label>
<input id="j_end"
class="time"
type="text"
name="j_end"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_date">Job Date</label>
<input id="j_date"
class="datepicker"
type="text"
name="j_date"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_location">Job Location</label>
<input id="j_location"
type="text"
name="j_location"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_people">Number of People</label>
<input id="j_people"
type="number"
name="j_people"
/>
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="form-group textarea">
<label class="form-label" data-add-placeholder for="j_description">Job Description</label>
<textarea id="j_description"
name="j_description"
></textarea>
</div>
<div class="form-group textarea">
<label class="form-label" data-add-placeholder for="j_tasks">What Needs to be Done</label>
<textarea id="j_tasks"
name="j_tasks"
></textarea>
</div>
</div>
<div class="form-group btn-wr text-center">
<input type="submit" class="btn btn-sm btn-success" value="Create Job" >
<div class="mfInfo"></div>
</div>
</form>
</div>
Solved
Solution: There were actually multiple problems with the code. After removing the quotes around the integers and decimals, as well as switching all of my statements to use mysqli; I was given the error that it could not connect to a database. This was fixed by adding mysqli_connect to the code, as well as a few variables.
Try this one
$sql = "INSERT INTO jobs (`title`, `pay`, `description`, `location`, `max_people`, `people_going`, `tasks, `start_time, `end_time`, `start`, `end`) VALUES ('$job_title', '$job_pay', '$job_summary', '$job_location', '$job_people', '$j_going', '$job_tasks', '$job_start_time', '$job_end_time', '$event_start', '$event_end')";
start and end is reserved words from MySQL

Categories