pdo query not running [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i can't seem to figure out why this query isn't running
if ( $productName && $productDescription && $productPrice ) {
// SQL
// UPDATE `prostud_tristurion`.`products` SET `product_title` = 'ajax test', `product_description` = 'Was certainty remaining engrossed applauded sir how discovery.', `product_price` = '524' WHERE `products`.`product_id` = 10;
try {
$query = "update products set product_title = :pName, product_description = :pDescription, product_price = :pPrice, where product_id = :pid";
//prepare query for excecution
$stmt = $con->prepare($query);
//bind the parameters
$stmt->bindParam(':pid', $id);
$stmt->bindParam(':pName', $productName);
$stmt->bindParam(':pDescription', $productDescription);
$stmt->bindParam(':pPrice', $productPrice);
// echo "$productPrice / $productDescription / $productName / $id\n $stmt";
var_dump($_POST);
// Execute the query
if ($stmt->execute() ) {
echo "Record was updated.";
} else {
die('Unable to update record.');
}
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
all i get is Unable to update record.
var_dump($_POST);
is looking good

You have an errant comma at product_price = :pPrice, where
If your code reaches the die statement then you have exceptions turned off (not recommended) but you can get the error message from the database (to log or echo) with $stmt->errorInfo()

Related

Uncaught PDOException: SQLSTATE[HY093] [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I get this erro: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\PHP\tennis\ronde2-wijziging.php:59
// code van het knop wijzigen
if(isset($_POST['wijzig'])){
$id = $_POST['id'];
$speler1 = $_POST['speler1'];
$speler2 = $_POST['speler2'];
$uitslag1 = $_POST['uitslag1'];
$uitslag2 = $_POST['uitslag2'];
$datum = $_POST['datum'];
$veld = $_POST['veld'];
//UPDATE: gegevens in de form wijzigen.
$sql = "UPDATE ronde1 SET speler1 = :speler1, speler2 = :speler2, uitslag1 = :uitslag1,
uitslag2= :uitslag2, datum= :datum, veld= :veld WHERE id=:id";
$stmt = $pdoConnect->prepare($sql); //stuur naar mysql.
$stmt->bindParam(":id", $id );
$stmt->bindParam(":speler1", $speler1 );
$stmt->bindParam(":speler1", $speler1 );
$stmt->bindParam(":uitslag1", $uitslag1 );
$stmt->bindParam(":uitslag2", $uitslag2 );
$stmt->bindParam(":datum", $datum );
$stmt->bindParam(":veld", $veld );
$stmt->execute();
// $_SESSION['message'] = "Speler is gewijzigd";
// $_SESSION['msg_type'] = "warning";
header("location: #.php");
exit;
}
I want to update my data.strong text
My solution worked but didn't explain why it did go wrong in the first place. The user dpant explains in the comments why your code snippet was not working.
Credits go to him
dpant:
Most probably the problem with your original code was that you were binding the :speler1 parameter twice (the :speler2 parameter was never bound). This was just a typo in your code. Take a close look at it.

SQL Insert duplicating values [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to insert data in SQL table, but it's duplicating the values:
$star1 = trim($_GET['star1']);
$star2 = trim($_GET['star2']);
$star3 = trim($_GET['star3']);
$star4 = trim($_GET['star4']);
$star5 = trim($_GET['star5']);
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Desculpe.. Falha ao salvar ");
}
$sql = "INSERT INTO classificacoes (nossa_empresa, produtos_oferecidos,atendimento, colaboradores, indicaria) VALUES ('$star1','$star2','$star3','$star4', '$star5')";
$conn->query($sql);
if ($conn->query($sql) != TRUE){
echo "Erro ao gravar";
}
else{
echo "Gravou";
}
mysqli_close($conn);
That is: The code insert two identincal values in the table.
What's wrong?
The problem is you're running the query twice:
$conn->query($sql);
if ($conn->query($sql) != TRUE){
The if line is also running the query, so you should remove the line above the if. Your code should be like this:
$sql = "INSERT INTO classificacoes (nossa_empresa, produtos_oferecidos,atendimento, colaboradores, indicaria) VALUES ('$star1','$star2','$star3','$star4', '$star5')";
if ($conn->query($sql) != TRUE){
echo "Erro ao gravar";
}
Or another option is to get the result of the query in a variable, like this:
$sql = "INSERT INTO classificacoes (nossa_empresa, produtos_oferecidos,atendimento, colaboradores, indicaria) VALUES ('$star1','$star2','$star3','$star4', '$star5')";
$insertOK = $conn->query($sql)
if ($insertOK != TRUE){ // This is equal to if(!$insertOK){
echo "Erro ao gravar";
}

PDO Update statement, work in PHPmyadmin but not in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to make an update statement with PDO and i found out it doesn´t work.
I have testet the SQL statement in phpMyadmin and it works if i put '' arround the passkey, but why wont it work with this ?
INFO:
The passkey is a md5 string
<?php
include('../mysql/pdoconn.php');
$passkey = $_GET['passkey'];
$stmt = $conn->prepare("UPDATE user SET com_code='' WHERE com_code = :passkey");
$stmt->bindParam(':passkey', $passkey , PDO::PARAM_STR);
$stmt->execute;
$error = "Jon Snow";
$stmt1 = $conn->prepare("SELECT com_code from user where com_code =''");
$stmt1->execute;
$result = $stmt1->fetchColumn();
if($result === "")
{
$error = 'Your account is now active. You may now Log in';
$conn = null;
} else
{
$error = $passkey;
$conn = null;
}
?>
i have tested that it gets the passkey, and it does, but it dont update the table...
I have tried anything, but i cant get it to work
$stmt = $conn->prepare("UPDATE user SET com_code='' WHERE com_code = :passkey");
$stmt->bindParam(':passkey', $passkey , PDO::PARAM_STR);
$stmt->execute();
execute() is a function
You don't need to quote bound parameters

PDO Variable Update Error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
these are my query codes. Please help me.
PDO Error: Array
PDO Eror Code: 00000
<?php if ($_POST){
$title = trim($_POST['title']);
$content = trim($_POST['content']);
$id = $_GET['id'];
$save = $PDO->prepare("UPDATE `news` SET `title` = :title WHERE `id` = :id");
$save->execute(array(
"title" => $title,
"id" => $id
));
print_r("Error: ".$save->errorInfo());
print $save->errorCode();
}
?>
It the OK status code.
You always print error but you should to print that only when the query failed.
$sql = $save->execute(...)
if ($sql === FALSE) {
print ('Error: ' . $save->errorCode());
}

Send message forward along the info [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Send message forward along the info. the info that I select in shell or worse than for the bare lies into the database again what I need .. but the problem is such that it appears with this error:
error 1: Commands out of sync; you can't run this command now
PHP/MYSQLI
if($stmt = $this->mysqli->prepare(' SELECT id, idunik, fra, message, datoTime FROM pm WHERE id = ?' ))
{
$stmt->bind_param('i', $id);
$id = $_GET['id'];
$stmt->execute();
$stmt->bind_result($id, $idunik, $fra, $message, $datoTime);
while($stmt->fetch())
{
if ($stm = $this->mysqli->prepare('INSERT INTO pm (idunik, title, fra, til, message, datoTime) VALUES (?, ?, ?, ?, ?, NOW())')) {
$stm->bind_param('issss', $idunik, $title, $fra, $til, $message);
$idunik = $idunik;
$title = $title;
$fra = $_SESSION["id"];
$til = $fra;
$message = $_POST["tekst"];
header('Location: /besked/' . $_SESSION["id"] . '/');
$stm->execute();
$stm->close();
} else {
echo 'error 1: ' . $this->mysqli->error;
}
}
$stmt->close();
}
else
{
echo 'error 2: ' . $mysqli->error;
}
so the problem is such that when carrying it into the database then comes the error.
Unfortunately your question it not perfectly understandable, so I'm just going to give some general info related to the the error you receive (as I presume this is what you want to resolve):
error 1: Commands out of sync; you can't run this command now
Because Mysqli uses unbuffered queries by default on prepared statements, while Mysqli has previous results to be fetched you cannot call another procedure.
To fix this, and avoid the error you are getting, use mysqli_store_result() to get all rows first, then perform the next query.
mysqli_store_result():
http://php.net/manual/en/mysqli.store-result.php
Transfers the result set from the last query on the database
connection represented by the link parameter to be used with the
mysqli_data_seek() function.
And more info on "commands-out-of-sync":
http://dev.mysql.com/doc/refman/5.1/en/commands-out-of-sync.html

Categories