Error: number of bound variables does not match number of tokens - php

I want to make an insert only if there's no correspondence in the db (mySQL) but he makes me not the statement. Here's the snippet
if ($sql->rowCount() > 0) {
echo 'Non inserisci';
} else {
echo 'Inserisci';
$db->beginTransaction();
echo 'Ciao3';
$sql = $db->prepare("INSERT INTO contatti (nome,cognome) VALUES (?,?)") or die('Ciao2');
echo 'Ciao4';
$sql->execute(array($_POST['nome'],$_POST['cognome']));
echo 'Ciao5';
$db->rollBack();
}
Where The SELECT is
$db->beginTransaction();
$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ? WHERE nome = ? AND cognome = ?") or die ('Ciao1');
$sql->execute(array($_POST['nome'],$_POST['cognome']));
$db->rollBack();
Can you explain me where's the fault?

The fault is in arithmetics
Let's count tokens:
SELECT * FROM contatti WHERE nome = ? // one
AND cognome = ? // two
WHERE nome = ? // three
AND cognome = ? // four
now let's count number of bound variables:
array($_POST['nome'], // one
$_POST['cognome']) // two
4 is apparently not equal to two. that's the problem

It seems you have done a little too much copy/paste here:
$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ? WHERE nome = ? AND cognome = ?") or die ('Ciao1');
should probably just be
$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ?") or die ('Ciao1');
You have had the WHERE clause doubled.

Related

Uncaught PDOException using the PDOStatement -> execute(Array) [duplicate]

I want to make an insert only if there's no correspondence in the db (mySQL) but he makes me not the statement. Here's the snippet
if ($sql->rowCount() > 0) {
echo 'Non inserisci';
} else {
echo 'Inserisci';
$db->beginTransaction();
echo 'Ciao3';
$sql = $db->prepare("INSERT INTO contatti (nome,cognome) VALUES (?,?)") or die('Ciao2');
echo 'Ciao4';
$sql->execute(array($_POST['nome'],$_POST['cognome']));
echo 'Ciao5';
$db->rollBack();
}
Where The SELECT is
$db->beginTransaction();
$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ? WHERE nome = ? AND cognome = ?") or die ('Ciao1');
$sql->execute(array($_POST['nome'],$_POST['cognome']));
$db->rollBack();
Can you explain me where's the fault?
The fault is in arithmetics
Let's count tokens:
SELECT * FROM contatti WHERE nome = ? // one
AND cognome = ? // two
WHERE nome = ? // three
AND cognome = ? // four
now let's count number of bound variables:
array($_POST['nome'], // one
$_POST['cognome']) // two
4 is apparently not equal to two. that's the problem
It seems you have done a little too much copy/paste here:
$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ? WHERE nome = ? AND cognome = ?") or die ('Ciao1');
should probably just be
$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ?") or die ('Ciao1');
You have had the WHERE clause doubled.

Is there a way to execute a subquery on a prepared statement result?

I'm trying to find the statement results which have a specific userid column
Here's the code:
$interazionequerystmt_string = "
SELECT USERID
FROM like_calendario_condivisioni
WHERE TIPO LIKE ? AND COMUNE LIKE ? AND VIA LIKE ? AND CIVICO LIKE ?";
$interazionequerystmt = $con->prepare($interazionequerystmt_string);
$tipo = "LIKE";
$interazionequerystmt->bind_param("ssss", $tipo, $comune, $via, $civico);
$interazionequerystmt->execute();
$likesquery = $interazionequerystmt->get_result();
$ennesimaquery = mysqli_query($con, "
SELECT * FROM ($likesquery)
WHERE USERID LIKE '$sessionuserid'");

Combining AND and OR conditions in SELECT statement

I tried to combine AND and OR syntaxes with SELECT, but it won't show me the data.
$cel can be a number or empty, $ruta and $jornada to be assigned
$query = mysqli_query($con,
"SELECT
*
FROM
registros_tty651
WHERE
(cel = '$cel' OR cel = '')
AND ruta = '$ruta'
AND jornada = '$jornada'
ORDER BY fecha_r DESC;");
try this
$query = mysqli_query($con,
"SELECT
*
FROM
registros_tty651
WHERE
(cel = '$cel' OR cel IS NULL)
AND (ruta = '$ruta'
AND jornada = '$jornada')
ORDER BY fecha_r DESC;");

fetch_assoc() loop HTTP ERROR 500

Can't find a way to fetch my results, sql request is good but it keeps crashing (HTTP ERROR 500) when using this code with the while loop.
I don't have any problem when using $row = $result->fetch_assoc(); but I have to loop to update every records.
$sql = "SELECT * FROM reservations WHERE statut = '1' AND NOW() BETWEEN dateAller AND dateRetour"; // requete sql sur les réservations validées dont la tranche horaire est en cours de validité
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$sql = "UPDATE vehicules SET statut = '2' WHERE immat = '$row[immat]'";
$result = $conn->query($sql);
}
}
you override $result in your Loop, so ->fetch_assoc() will be called on the result of the update Statement in the second iteration. You have to change to:
$sql = "SELECT * FROM reservations WHERE statut = '1' AND NOW() BETWEEN dateAller AND dateRetour"; // requete sql sur les réservations validées dont la tranche horaire est en cours de validité
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$sql = "UPDATE vehicules SET statut = '2' WHERE immat = '$row[immat]'";
$result2 = $conn->query($sql);
}
}
Change these lines
$sql = "UPDATE vehicules SET statut = '2' WHERE immat = '$row[immat]'";
$result = $conn->query($sql);
to
$sql = "UPDATE vehicules SET statut = '2' WHERE immat = '".$row['immat']."'";
$conn->query($sql);
You are overwriting $result with the result of the update query. You probably don't need this result (at least you are not using it yet), so you can easily remove the assignment.
Additionally you need ' or " to access an array ($row) by a key.
In your case you are lucky and it works without quotes.
Explanation:
PHP sees a constant called immat. If the constant is defined, the value is returned, it isn't defined, PHP falls back to the string "immat".
Anyway it's always a better practice to use quotes!
Try to use this one:
<?php
$sql = "SELECT * FROM reservations WHERE statut = '1' AND NOW() BETWEEN dateAller AND dateRetour";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$immat = $row["immat"];
$sql = "UPDATE vehicules SET statut = '2' WHERE immat = '$immat'";
$updateResult = mysqli_query($conn,$sql);
}
}
?>

Inserting Data from Grabbing URL Parameter - PHP PDO

I have, for example, this URL with spesific paramater in the end:
http://example.com/index.php?id_user=84759832475
The value [id_user=**84759832475**] is created by myself and I have declared it inside my script.
$txtemail = strip_tags(isset($_POST['txtemail'])) ? strip_tags($_POST['txtemail']) : '';
$txtemail=strip_tags($txtemail);
$txtname = strip_tags(isset($_POST['txtname'])) ? strip_tags($_POST['txtname']) : '';
$txtname =strip_tags($txtname);
$id_user="84759832475";
$stmt="SELECT * FROM table_name WHERE emailz=:txtemail AND namez=:txtnamez";
$pgdata = $myDb->prepare ($stmt);
//bind semua variabel login dalam parameter
$pgdata->bindParam(':txtname', $txtname, PDO::PARAM_STR,31);
$pgdata->bindParam(':txtemail', $txtemail, PDO::PARAM_STR,31);
//eksekusi statemen prepare tadi
$pgdata->execute();
//cek & lihat hasil
//$cekdata = $pgdata->fetchColumn();
if(!$pgdata->rowCount()> 0){
$pgdata = $myDb->prepare('INSERT INTO table_name (namez,emailz,userid) VALUES (:txtname,:txtemail,?????)');
$pgdata->execute(array(':namez'=>$txtname, ':emailz'=>$txtemail, ':userid'=>$id_user));
In this case, the question mark ?????? makes me confused of what to write.
I'm sorry if my English is too bad to explain this question.
Just add another named placeholder inside that other prepared statement, just like the others:
$txtemail = isset($_POST['txtemail']) ? strip_tags($_POST['txtemail']) : '';
$txtname = isset($_POST['txtname']) ? strip_tags($_POST['txtname']) : '';
$id_user = "84759832475";
$stmt = 'SELECT COUNT(id) AS total FROM table_name WHERE emailz = :txtemail AND namez = :txtnamez';
$pgdata = $myDb->prepare($stmt);
$pgdata->bindParam(':txtnamez', $txtname, PDO::PARAM_STR);
$pgdata->bindParam(':txtemail', $txtemail, PDO::PARAM_STR);
$pgdata->execute();
$result = $pgdata->fetch(PDO::FETCH_ASSOC);
if($result['total'] > 0){
$pgdata = $myDb->prepare('
INSERT INTO table_name (namez,emailz,userid)
VALUES (:txtname, :txtemail, :userid)
');
// just add another named placeholer :userid
$pgdata->execute(array(':txtname'=> $txtname, ':txtemail'=> $txtemail, ':userid' => $id_user));
}

Categories