Update PHP prepare bindparam [duplicate] - php

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 6 years ago.
This is my PHP code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$UpdateChecklist = 'UPDATE checklists SET ADMIN_ID=?, COMPUTER_ID=? WHERE id=?';
$stmtChecklist = $connection->prepare($UpdateChecklist);
$stmtChecklist->bind_param('ii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID']);
$isUpdate = $stmtChecklist->execute();
$lastUpdateId = mysqli_insert_id($connection);
$stmtChecklist->close();
$UpdateInstalledProgram = 'UPDATE checklist_programs SET CHECKLIST_ID=?, PROGRAM_ID=? WHERE id = ?';
$stmtProgramId = $connection->prepare($UpdateInstalledProgram);
$stmtProgramId->bind_param('ii', $lastUpdateId, $programId);
foreach ($_POST['PROGRAM_ID'] as $program) {
$programId = $program;
$stmtProgramId->execute();
}
$connection->close();
if ($isUpdate) {
header('Location: OverViewCheckList.php');
exit(0);
}
}
?>
and, I got some error which I don't know how to fix it:
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\checklist\updateChecklist.php on line 34
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\checklist\updateChecklist.php on line 44
Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\checklist\updateChecklist.php on line 57
Fatal error: Call to a member function fetch_assoc() on null in C:\xampp\htdocs\checklist\updateChecklist.php on line 59

In your both of your queries you are using 3 ?'s but only binding 2 variables.
Query 1:
$UpdateChecklist = 'UPDATE checklists SET ADMIN_ID=?, COMPUTER_ID=? WHERE id=?';
$stmtChecklist = $connection->prepare($UpdateChecklist);
$stmtChecklist->bind_param('ii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID']); /* Here*/
Query 2:
$UpdateInstalledProgram = 'UPDATE checklist_programs
SET CHECKLIST_ID=?, PROGRAM_ID=? WHERE id = ?';
$stmtProgramId = $connection->prepare($UpdateInstalledProgram);
$stmtProgramId->bind_param('ii', $lastUpdateId, $programId); /* Here */
You need to add a third variable e.g.:
$stmtChecklist->bind_param('iii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID'], $id3);
$stmtProgramId->bind_param('iii', $lastUpdateId, $programId, $id3);

Related

Does anyone know why am I getting a fetch_array() boolean error? [duplicate]

This question already has answers here:
How to fetch all in assoc array from a prepared statement?
(4 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I am trying to fetch TaxRate data from a table and I'm trying to select the first array only. However, I keep getting this error
"Fatal error: Uncaught Error: Call to a member function fetch_array()
on boolean in C:\xampp\htdocs\flowerique\shoppingCart.php:77 Stack
trace: #0 {main} thrown in C:\xampp\htdocs\flowerique\shoppingCart.php
on line 77"
Does anyone know the cause of this error and how do I fix it? Thanks!
This is my code :
//Retrieve Current GST Pricing
$qry = "SELECT * FROM gst GROUP BY EffectiveDate DESC";
$stmt = $conn->prepare($qry);
$stmt->execute();
//$stmt->close();
$result = $conn->query($qry);
$row = $result->fetch_array(); // This is line 77
while($row["EffectiveDate"] < date("Y-m-d"))
{
$row = $result->fetch_array();
}
$currentTaxRate = $row["TaxRate"];

Trying to output data as an array [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
I made a table that holds post and likes and I'm trying to output the of everything in the table but I get the
error: mysqli_num_rows() expects parameter 1 to be mysqli_result,
boolean given on line 18.
$sql3 = "SELECT * FROM post;";
$result = mysqli_query($conn,$sql3);
$datas = array();
if (mysqli_num_rows($result)> 0){
while($row = mysqli_fetch_assoc($result)){
$datas[] = $row;
}
}
foreach ($datas as $data){
echo $data;
}
I expect the all the information to be outputted, but the actual output is
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean
given on line 18.
This means that your query is returning false instead of a mysqli_result object. The problem appears to be from the semicolon at the end of your statement. I believe that mysqli will see that as a multistatement. From https://www.php.net/manual/en/mysqli.quickstart.multiple-statement.php:
Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.
mysqli has a method to get the last error it encountered:
https://www.php.net/manual/en/mysqli.error.php
Example:
if(!$result){
printf("Error message: %s\n", mysqli_error($conn));
}

Unable to run query using prepared statement in MySQLi [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
I am working on the below code. Why am I not able to run the query properly? I already check the database connection and it is fine
<?php
$sql = "SELECT dt, events, eventtype FROM events";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$stmt->bind_result($dt,$events,$eventtype);
$stmt->store_result();
if($stmt->num_rows >0) {
$stmt->fetch();
}
else {
echo "Cant Find The data!";
}
$stmt->close();
$mysqli->close();
echo $dt;
echo $events;
echo $eventtype;
?>
getting this error
Fatal error : Call to a member function execute() on boolean in
/srv/disk1/2555378/www/domain.net/index.php on line 113
This means that the variable $mysqli contains a boolean value, probably false.
According to the php docs, http://php.net/manual/en/mysqli.prepare.php, the function mysqli::prepare will return false in case of an error.
You should use the error variable to get more information, like here: http://php.net/manual/en/mysqli.error.php

Invalid parameter in select from database? [duplicate]

This question already has an answer here:
MysQl error : Invalid parameter number
(1 answer)
Closed 8 years ago.
Its probley something small but i been looking at this for ages and still cant get it to work at all im getting two errors
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in
the code i have is this:
public function getdata ($tran_id)
{
$sql = "SELECT tran_id, seller_user_name, user_name_buyer
FROM trade_transaction, feedback Where feedback.feedback_username = trade.user_name_of_buyer
AND user_name_of_buyer = :user_name_buyer ";
$sth = $this->db->prepare($sql);
$sth->execute(array(':tran_id' => $tran_id, ':user_name_buyer ' => $_SESSION['user_name']));
$user = $sth->fetch();
You're binding a :tran_id parameter during your call to execute, but you're not using that parameter in your query.
Change your execute line to this
$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));
Remove :tran_id from your parameter list or add a condition for that parameter. I hope this help.
Your select statement does not have a where clause for tran_id, either remove the tran_id from your execute call
$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));
or add a extra where clause to your sql statement
$sql = "SELECT tran_id, seller_user_name, user_name_buyer
FROM trade_transaction, feedback
WHERE feedback.feedback_username = trade.user_name_of_buyer
AND tran_id = :tran_id
AND user_name_of_buyer = :user_name_buyer ";

mysqli::prepare(): Couldn't fetch MySQL [duplicate]

This question already has answers here:
mysqli::query(): Couldn't fetch mysqli
(4 answers)
Closed 9 months ago.
I started using mysqli_* functions instead of the old mysql_* functions in PHP, and I'm having a bit of a problem with this code:
public function addUser($table, $code = false, $rows = false) {
if (!$code) {
header("Location: " . $this->authenticate());
} else {
$this->getToken($code);
}
$user = $this->getEndpoint('users/current', false, true);
$user = $user->response->user;
if (!$rows)
$rows = array(
"remote_id" => $user->id,
"firstName" => $user->first_name,
"lastName" => $user->last_name,
"photo" => $user->photo->medium,
"gender" => $user->gender == 'male' ? 1 : 2,
"email" => $user->contact->email,
);
$rows['access_token'] = $this->accessToken;
$stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = '{$this->accessToken}'"); //line 136
$stmt->execute(); //line 137
}
The code returns these 2 errors:
Warning: mysqli::prepare(): Couldn't fetch MySQL in C:\Users\Grega\Server\application\inc\classes\APIConnect.php on line 136
Fatal error: Call to a member function execute() on a non-object in C:\Users\Grega\Server\application\inc\classes\APIConnect.php on line 137
What is the reason for 'Couldn't fetch MySQL'? The database connection is correct, it works in other classes, and the query returns a valid result, if I echo it and execute it in phpMyAdmin. Also, my variable is named mysql NOT mysqli!
You should read more about the difference between MYSQL to MYSQLi.
While your code is:
$stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = '{$this->accessToken}'");
You should do it like this:
$stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = ?");
$stmt->bind_param("s" , $this->accessToken ); //Used 's' as I guess that the accessToken is a string
The binding part is the critical part of the prepare thing. (Your queries are safe)
After that you can use $stmt->execute(); and get_result().
For the first error: you probably closed the connection somewhere before this code, and this is why ($stmt -> close() )
For the second error: when you use (prepare()), you first introduce a SQL template to the database, which then has to pass the parameters to the "bind_param()" method to send it to the database with another protocol (to prevent SQL injection) and reduce the request and attach the parameters to SQL with execute() method

Categories