bindParam insert into table where - php

I have been coding for a few hours on this thing, so I think I am missing something very simple here, but I can't seem to find it.
I am getting these 2 errors
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens on line 77
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens on line 79
public function resetPassword($password, $email){
$rst = $this->db->prepare("insert into users (password) values (?) where email=? ");
$rst->bindParam('?', $password);
$rst->bindParam('?', $email);
$rst->execute();
if($rst->execute()){
return "Password changed!";
}
else echo "Could not change password.";
}
Am I forgetting something?

When using questions marks as placeholders, you send an array to the execute method, like so: $rst->execute(array('placeholder1value', 'placeohlder2value'));
However, if you want to use named placeholders, you would bindParam/bindValue them, like so:
$stmt = $pdo->prepare('INSERT INTO table (key1, key2) VALUES (:key1, :key2)');
$stmt->bindValue(':key1', 'somevalue', PDO::PARAM_STR);
$stmt->bindValue(':key1', 3532, PDO::PARAM_INT);
$stmt->execute();
Please read about the difference between bindParam and bindValue
And another note, your SQL query doesn't make sense, do you mean to do an UPDATE?

Related

insert into multiple rows PDO

i am struggling with the code to insert into multiple rows.
but ended up getting warnings
$rows = array(1,2,3,4,5,6)
$stmt = $connect->prepare("INSERT INTO t_worker_history (uid) VALUES (?)");
foreach($rows as $insert) {
$stmt->execute($insert);
}
Warning: PDOStatement::execute() expects parameter 1 to be array, string given in
As the message says, the first parameter needs to be an array, so just put the $insert value into one:
$stmt->execute(array($insert));
See the manual. The reason the parameter needs to be an array is to allow for multiple parameters to be bound to placeholders.

PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Below is my code, I am not able to resolve this error. Any help is appreciated. I am trying to update a table in my database.
public function updateUnit($params){
$user = 'monil';
$password = 'Masters123';
$dbh = new \PDO('mysql:host=127.0.0.1;dbname=tcsdb', $user, $password);
$task=array(':UnitCode'=>$params['UnitCode'],':UnitDescription'=>$params['UnitDescription'] ,
':UnitName'=>$params['UnitName'], ':UnitID'=>$params['UnitID']);
echo $params['UnitID'];
$sth = $dbh->prepare('UPDATE unit SET UnitCode = :UnitCode,'
. 'UnitDescription = :UnitDescription,UnitName = :UnitName WHERE UnitId=:UnitId');
$sth->execute($task);
return true;
}
Parameter names used in execute()/binding should be exact match for the parameter names used in the SQL query. That's the point of named parameters.
You need to check every placeholder in SQL, whether its name matches the name used in execute(), bindParam() or bindValue().
In your case, :UnitID is not the same as :UnitId, there is a difference in the letter case.
In a rare case, the error can be caused by improper placeholder name. The only characters allowed are [a-zA-Z0-9_].
The same error arise when you missed : colon while creating statement.
ex:
Below statement throws invalid parameter error as password in VALUES is missing : colon.
$stmt = $db->prepare('INSERT INTO members (username,password) VALUES (:username, password)');
same errors may occur if you use a "." dot in bindParam
ex.
$query = "select * from t where t1 = :foo.bar";
$stmt = $pdo->prepare($query);
$stmt->execute([':foo.bar' => 'blah']);

SQLSTATE[HY093]: Invalid parameter number:

function removeMovie($link, $title){
$sth = $link->prepare("DELETE FROM film WHERE title=:title");
$sth->bindValue (':title ', $title, PDO::PARAM_STR);
$sth->execute();
echo 'Removing succeed, Ga back';
var_dump ($sth->errorInfo());
}
I receive the following error message:
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid > parameter number: parameter was not defined in D:\Libraries\Documents\ICT\Webprogrammeren\USBWebserver v8.5\8.5\root\Huiswerk\Week 5\functions.php on line 39
$link
is the database connection i'm making
I've searched google for a long time now and I can't find the right answer. A lot of have a typo but I can't seem to find the typo. So it must be something else:
I have a database with two tables which are connected with one and each other through genre_id. Maybe this has something to do when I try to delete it?
Kind regards,
Danny

PHP PDO INPUT SQLSTATE[HY093]: Invalid parameter number: parameter was not defined error

I am very new to working with Data Bases, I have researched this for several days and I have not been able to get through the Invalid parameter number. I have cut back code and options just trying to get two data into the MySQL 5.1 db using PHP 5.2
I get a connection to the db fine and based on echo statements I feel confident that I am getting to the prepare statement ok.
The full code is below
$DBHandle= new PDO('mysql:localhost;dbname=nameishere','userishere','passishere');
//* $DBHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); --for USE
//* below is what I am currently using for debugging
$DBHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
//*next bit is to insure that if connection is lost database is not partially updated-I think- right now commented out
//* $DBHandle->beginTransaction();
$mainIncrement= NULL;
$firstnameOBS= $_POST['touristfirstname'];
$todaysdateOBS= $_POST['touristdatetoday'];
//*$picturenow= $_POST['picturesubmitted'];
$JSONfirstname = json_encode($firstnameOBS);
$JSONtodaysdate = json_encode($todaysdateOBS);
//*$JSONpicturenow = json_encode($picturenow);
echo ($JSONfirstname);
$senditin = $DBHandle->prepare("INSERT INTO 'fkarnd'('firstname','datetoday') VALUES(:field1,:field2)", array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$senditin->bindValue(':firstname', $JSONfirstname, PDO::PARAM_STR);
$senditin->bindValue(':datetoday', $JSONtodaysdate, PDO::PARAM_STR);
//* $myinputarray = array('firstname'=> $JSONfirstname, 'datetoday' => $JSONtodaysdate );
$senditin->execute();
//* commit allows transaction begun to complete
//* $DBHandle->commit();
//* catch ( PDOException $e )
echo "I'm sorry, I can't do that Dave......";
//*file_put_contents( 'dbErrors.txt', $e->getMessage(),FILE_APPEND);
//* echo "successful submission for the preservation of JohnsPass";
$DBHandle = null;
I have tried putting the data in an array then executing, I have tried several different formats for the prepare statement including INPUT ()INTO table WHERE().... I have tried having no binding. I really am just trying to put data into the db from an HTML5 form I created. The form data comes over ok based on echo statments and most of the PHP examples seem to deal with SELECT not INPUT.
Below is the output and the error I cannot figure out
"Dread"
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /home/mabelsbi/public_html/johnspass.org/Science/FirstTry.php on line 46
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number in /home/mabelsbi/public_html/johnspass.org/Science/FirstTry.php on line 46
$senditin = $DBHandle->prepare("INSERT INTO 'fkarnd'('firstname','datetoday') VALUES(:field1,:field2)", array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$senditin->bindValue(':firstname', $JSONfirstname, PDO::PARAM_STR);
$senditin->bindValue(':datetoday', $JSONtodaysdate, PDO::PARAM_STR);
You're using :field1 and :field2 in your query, and then setting :firstname and :datetoday as parameters; you just need to be consistent in what you're setting:
$senditin = $DBHandle->prepare("INSERT INTO `fkarnd` (`firstname`,`datetoday`) VALUES(:firstname,:datetoday)", array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$senditin->bindValue(':firstname', $JSONfirstname, PDO::PARAM_STR);
$senditin->bindValue(':datetoday', $JSONtodaysdate, PDO::PARAM_STR);
(Edited to use backticks in SQL around field and table names)

php, mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement

I have the following code :
$id = $_GET['id'];
// get the recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM date WHERE id=?"))
{
$stmt->bind_param("isssssssss", $id, $mtcn, $amount, $currency, $sender_name, $sender_country, $receiver_name, $comment, $support, $email);
$stmt->execute();
$stmt->bind_result($id, $mtcn, $amount, $currency, $sender_name, $sender_country, $receiver_name, $comment, $support, $email);
$stmt->fetch();
// show the form
renderForm($mtcn, $amount, $currency, $sender_name, $sender_country, $receiver_name, $comment, $support, $email, NULL, $id);
$stmt->close();
}
// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
which generates the following errors / warnings :
Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in C:\wamp\www\records.php on line 143
Warning: mysqli_stmt::execute() [mysqli-stmt.execute]: (HY000/2031): No data supplied for parameters in prepared statement in C:\wamp\www\records.php on line 144
Warning: mysqli_stmt::bind_result() [mysqli-stmt.bind-result]: Number of bind variables doesn't match number of fields in prepared statement in C:\wamp\www\records.php on line 146
Warning: mysqli_stmt::fetch() [mysqli-stmt.fetch]: (HY000/2053): Attempt to read a row while there is no result set associated with the statement in C:\wamp\www\records.php on line 147
And the form is not completed with the data from db as i was expecting ... Any advice / help would be greatly appreciated.
You misunderstood the use of statements.
In your query statement, you only need to bind one parameter, the id.
$stmt->bind_param("i", $id);
$id = 5; // fetch the fifth record
$stmt->execute();
And then you access the values from your query's result by fetching the result object.
Check out the mysqli_stmt::execute exemple, php.net

Categories