Query returning false - php

I have been stuck on this for the past 2 hours and can't see what the problem is here.
When I execute the SQL in phpmyadmin manually it runs fine when i substitute :item_quant and :item_code for their true values.
When adding an echo on return false to see if $item_quant and $item_code are the correct values it shows them as the correct values.
$sql = "UPDATE inventory SET item_quant=item_quant+:item_quant WHERE item_code=':item_code' LIMIT 1";
$query = $database->prepare($sql);
$result = $query->execute(array(':item_quant' => $item_quant, ':item_code' => $item_code));
if ($result) {
return true;
}
return false;
Any help or insight as to why this would be failing would be greatly appreciated.

Placeholders must not be put inside quotes.
$sql = "UPDATE inventory SET item_quant=item_quant+:item_quant WHERE item_code=:item_code LIMIT 1";

Try removing the quotations:
$sql = "UPDATE inventory SET item_quant=item_quant+:item_quant WHERE item_code=:item_code LIMIT 1";

Related

How would I make a php if statement update my sql column

My Problem
I'm trying to give persons over 16 special options on the website I am creating, but I'm struggling to get the PHP to check the age and then change the column to 1 for Yes or 0 for No.
What I've Tried
if ($account['age']>=16) {
echo 'True';
$sql = 'UPDATE accounts SET o16 = 1 WHERE id = ?';
} else {
echo 'False';
$sql = 'UPDATE accounts SET o16 = 0 WHERE id = ?';
};?>
I'm using MariaDB SQL at this moment.
It returns the values true or false but it does not change the value of the column.
I would be grateful if someone could help. I don't know if I have to do something different or if there's another way I could do this.
But what are you doing about the SQL? You are just doing an update but you don't execute it. Try somethings like this.
<?php
// $user_id = you put users id here, if you want to do it over session or something.
if ($account['age']>=16) {
echo 'True';
$sql = 'UPDATE accounts SET o16 = 1 WHERE id = ?';
$sql->bind_param('i',//$user_id);
$sql->execute();
} else {
echo 'False';
$sql = 'UPDATE accounts SET o16 = 0 WHERE id = ?';
$sql->bind_param('i',//$user_id);
$sql->execute();
}
Hope this helps you in some way, i think this could be right, but correct me if im wrong :)
use print_r($this->db->last_query()); to see the query and check under SQL execution in database server. You can check where you went wrong.

notice trying to get property in php when sql returns 0 row

I am trying to check if the stockid/s exist in peri or nonperi tbl. If the stockid belongs to peri, then I want to update peri. If not, the nonperi table will be updated.
What I did was to check first in the nonperi table and if the result is 0, then search again on nonperi tbl. Here is my code
//update nonperi table
$qryup = "UPDATE nonperi
SET bal='$balafter',modifiedby='$userid', modifiedon='$currdate'
WHERE stockid= $item";
$mysql1 = $conn->query($qryup);
if ($mysql1-> num_rows == 0)
{
//else if not in nonperi, update peri tbl instead
$qryup2 = "UPDATE peri
SET bal='$balafter',modifiedby='$userid', modifiedon='$currdate'
WHERE stockid= $item";
$mysql2 = $conn->query($qryup2);
}
The code works, it updates the nonperi if the result from peri tbl is 0, however, the notice of property obj etc is still displayed.
Do you have any suggestion on how to make this work, or any workaround or if you have any better code to achieve this without the error? PLease help. THanks in advance :)
Have you tried going with a prepared statement instead? I know I never had any notice when using those instead of directly query:
$qryup = "UPDATE nonperi
SET bal=:bal,modifiedby=:userid, modifiedon=:currdate
WHERE stockid= :id";
$stmt = $conn->prepare($qryup);
$stmt->bindValue(":bal", $balafter);
$stmt->bindValue(":userid", $userid);
$stmt->bindValue(":currdate", $currdate);
$stmt->bindValue(":id", $item);
$stmt->execute();
if ($stmt->rowCount() == 0)
{
//similar treatment here
}

Trying to fill a PHP variable with a mysql query. Then updating another table with the data from that query

First post, here it goes.
So this is the code that I have so far:
include('Connection/connect-test.php');
$selected1 = $_POST['selected'];
$sqlget = "SELECT paymentid FROM highschoolpayment WHERE hsgameid = '$selected1'";
$sqldata = mysqli_query($dbcon, $sqlget);
$sqlupdate = "UPDATE highschool SET paymentid = '$sqldata' WHERE hsgameid = '$selected1'";
mysqli_query($dbcon, $sqlupdate);
What I'm trying to do is grab the 'paymentid' from the 'highschoolpayment' table and store that value into the $sqldata variable (line 4). Then I want to update a value in the 'highschool' table using the value that I got from line 4 as well as a value that was pulled from a POST submission (line 6). I know for a fact that the first 3 lines execute as they should. It is after those lines when things become iffy. I don't see the form (reappear) like I normally would when everything else is working. To me, this indicates that the PHP has successfully run. I go to the 'highschool' table but I don't see the value (paymentid) that I am expecting to see. I personally can't think of a single reason why this wouldn't work, but, I am not that experienced in PHP or MySQL so I am open to any help that I can get.
I hope this makes sense without seeing the structure of the tables but if I need to post those, let me know. I've spent a couple hours trying to troubleshoot this problem but with no forward progress.
Thanks!
Assuming this query returns only one row:
$sqldata = mysqli_query($dbcon, $sqlget);
$row = mysqli_fetch_array($sqldata);
$paymentid = $row['paymentid']; // then use $paymentid in the next query
$sqlupdate = "UPDATE highschool SET paymentid = '$paymentid'
WHERE hsgameid = '$selected1'";
if(mysqli_query($dbcon, $sqlupdate)){
echo 'Update successfull';
} else {
echo 'Update query is wrong. The query generated was <br />'.$sqlupdate;
}
try like this,
include('Connection/connect-test.php');
$selected1 = $_POST['selected'];
$sqlupdate = "UPDATE highschool SET paymentid = (select paymentid FROM highschoolpayment WHERE hsgameid = '$selected1') where hsgameid = '$selected1'";
mysqli_query($dbcon, $sqlupdate);
you need to do fetch_assoc(), and while you are at it you should parameterize your query to make it more secure, good practice for the future. here is what your code should look like
$selected1 = $_POST['selected'];
$connect = mysqli_connect("localhost","user","pass","database");//i connect this way to my database
//the first statement that will get your paymentid
$stmt = $connect->prepare("SELECT paymentid FROM highschoolpayment WHERE hsgameid = ?")
mysqli_stmt_bind_param($stmt, 's', $selected1);//'s' is for string, 'i' for int, google rest
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){//it fetches each id
//the second statement that will use the payment id and update the database
$stmt2 = $connect->prepare("UPDATE highschool SET paymentid = ? WHERE hsgameid = ? ;")
mysqli_stmt_bind_param($stmt2, 'ss',$row['paymentid'], $selected1 );//'s' is for string, 'i' for int, google rest
$stmt2->execute();
$stmt2->close();
}
$stmt->close();
I just threw this quickly together, so if anyone sees something wrong don't hesitate to edit it or mark it down if completely wrong, Would rather that.

SQL Multiple Statements

I am using php and mysql to update rows in my DB. I have 4 update statements in a row, yet only the last one works. I have confirmed that the statements work if they are used alone, but when I have them executed one after another only the last one executed works. I am receiving no error messages. Any help? Thanks!
$sql = "UPDATE comlog SET name='$name1', message='$message1' WHERE id=1";
$sql = "UPDATE comlog SET name='$name2', message='$message2' WHERE id=2";
$sql = "UPDATE comlog SET name='$name3', message='$message3' WHERE id=3";
$sql = "UPDATE comlog SET name='$name', message='$message' WHERE id=4";
In the above code, only the row with id 4 is being updated.
The answer is simple.
You are declaring the same variable for EACH sql string.
You need to declare it something like:
$sql1 = "";
$sql2 = "";
$sql3 = "";
$sql4 = "";

My PHP SQL query is throwing errors, even though it works in the SQL console

I'm trying to create a function for my forum that will increment my user's "Posts" attribute by 1. For whatever reason, the following PHP does not work.
function postCountIncrease($username) {
//get the connection variable
global $con;
//change to the users database (this function works correctly)
sqlconnect_users();
//get current post number (this is also working)
$getCurrentPosts = "SELECT Posts\n"
. "FROM users\n"
. "WHERE Username='".$username."'";
$query1 = mysqli_query($con, $getCurrentPosts) or die(mysqli_error($con));
$currentPosts = mysqli_fetch_array($query1);
//here is the problematic post. Assume that $username is a valid value, and that I've already done mysqli_real_escape_string() on it
$incrementPostsQuery = "UPDATE users.users SET Posts=". $currentPosts[0]+1 ." WHERE Username='". $username ."'";
$query2 = mysqli_query($con, $incrementPostsQuery) or die(mysqli_error($con));
//return the result
$result = mysqli_fetch_array($query2);
return $result;
}
I honestly don't see what I'm doing wrong, because the SQL works fine. If I use UPDATE users.users SET Posts=1 WHERE Username='Lampitosgames' in the console, it works with no errors. Help is much appriciated. Also, here is the error it is throwing at me:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 WHERE Username='Lampitosgames''
You can not concatenate that way "toto ".$var+1, you have to surround with brackets "toto ".($var+1)
In your case, this is declaration of var $incrementPostsQuery which fails
Look at your errors, your syntax is off
$getCurrentPosts = "SELECT Posts
FROM users
WHERE Username='$username'";
The error is in the building of your query.
$incrementPostsQuery = "UPDATE users.users SET Posts=". $currentPosts[0]+1 ." WHERE Username='". $username ."'";
I'll suggest you some tips to create query like this:
"update table set field = value"; // you can write the value directly
"update table set field = ". $value; // easy
"update table set field = ". ($a+$b); // ...
"update table set field = {$value}"; // you can add a variable with curly braces
"update table set field = {$va[3]}"; // more compless way
"update table set field = {$a->b}"; // an object field

Categories