Using a mysqli_multi_query to allow the user to update elements of an order (Quantity of items and chosen delivery date), the quantity query updates no problem on its own. But the delivery date part is not executing at all. The multi query does involve executing two queries on two different tables (Order & Order information), but wouldn't have thought this would be the issue.
I've tried executing the query as a standard mysqli_query (not multi) to see if it was an issue there, but the result is the same in that it is printing out the query on the page (UPDATE mytable.Order SET Chosen_Delivery_Date = '' WHERE Order_ID = '1')
From what I can see in the above (), it looks like its not reading the changed delivery date? I've tried moving the syntax around in case it was a mistake there but had no luck.
The code I've provided is the standard (not multi) query I've been working with, as would just like to get the initial query working first before i step back into making things more complicated with multi.
The code executes through 2 pages, first the page that takes the new delivery date input:
echo "<td><input type=date name='Chosen_Delivery_Date' value='".$row['Chosen_Delivery_Date']."'></td>";
echo "<td><input hidden = date name = Chosen_Delivery_Date = '".$row['Chosen_Delivery_Date']."'></td>";
Second the page that executes the query based on this input:
// This assigns the new delivery date to a variable
$Delivery = $_POST['Chosen_Delivery_Date'];
//executing the query
$update = "UPDATE Order SET Chosen_Delivery_Date='$Delivery' WHERE Order_ID = '$Order_ID'";
if(mysqli_query($conn,$update)) {
echo "Order updates sucessfully";
}
else {
echo "Error updating order: ".mysqli_error($conn);
}
All connections with the database are working no problem, but do let me know if any of you would like to see these connections or how the table is being read/echoed for the user to read before they change the the values.
I am trying to pull user data from a Cart66 table I have and put it into a shortcode in wordpress. $account is an integer pulled from session data. The code below returns nothing.
$account =Cart66Session::get(Cart66AccountId);
global $wpdb;
$fname=$wpdb->get_results("SELECT * FROM 'vfp_cart66_accounts' WHERE id = '$account', ARRAY_N");
foreach ($fname AS $row)
{
echo $row;
}
This returns "Array"
return $fname;
Ok firstly, maybe I am the only one who saw this, and it could be the source of your entire problem, but you have a misplaced double quote, at the end of your SQL line, which should live at the end of the actual SQL string, not after the requested return type:
// at the end of this line you have: '$account', ARRAY_N");
// this should be changed to: '$account'", ARRAY_N);
$fname=$wpdb->get_results("SELECT * FROM 'vfp_cart66_accounts' WHERE id = '$account', ARRAY_N");
Even the first person who answered the question did not correct you, so I am assuming he didn't see it either. Secondly, using single quotes (') to escape a table name is invalid. If it is quoted at all, use backticks (`). Single quotes indicate a string, not an database, table, or field, all three of which should only be quoted with backticks (except on utility queries like SHOW). Use this instead:
select * from `vfp_cart66_accounts` where id = '$account'
Thirdly, as your commenters point out, you could be vulnerable to SQL Injection. Make sure to use the tools that WP gives you, and do this, or similar, instead:
$fname = $wpdb->get_results(
$wpdb->prepare(
'select * from `vfp_cart66_accounts` where id = %d',
$account
),
ARRAY_N
);
Lastly, you are requesting an array from the DB, but you are trying to echo it as if it were a scalar value. This explains why printing the value of $row yields "Array". When you convert an array() to a string, by default, you get "Array", since arrays can be complex data that may not be beautifully converted to a string. As a correction of this, you can do one of two things.
First, if you need the entire resulting array that represents the entire row of the table, then you can simply change your echo code to this:
foreach ($fname as $row) {
// print the fname of the row
echo $row['fname'];
// do the other stuff you need to do with $row
...
}
OR, if you simply need the fname field out of that table, for the given id, you could use a different $wpdb function, called $wpdb->get_var(), which gets one specific field from the first entry of the resulting data from the database, coupled with some minor SQL changes:
// use the get_var() function instead
$fname = $wpdb->get_var(
$wpdb->prepare(
// 1) change the 'fields' of your sql to only get the `fname` field
// 2) also add limit 1, to reduce load by only asking for one row
// NOTE: #2 is optional really, because WP does this for you when using get_var,
// but is good practice to only ask for what you need. so do it
'select fname from `vfp_cart66_accounts` where id = %d limit 1',
$account
),
ARRAY_N
);
echo $fname; // print the value of field fname from vfp_cart66_accounts for id $account
Now. I don't have specific knowledge of Cart66. That being said, if the above changes to PHP, WordPress, and SQL syntax do not yield results, then you are probably having one of the following other problems instead:
there is a different PHP error somewhere in the code, causing this to never run
this code is never called, and thus it is never executed
you misspelled the table name, which is causing an SQL error
the table exists, but does not have a field named id
both table and field exist, but there are no entries in the table
some other random thing that is not coming to mind
DEBUG #1
For #1, you could try turning on error_reporting() and display_errors early in the code execution. In a normal, run of the mill PHP script you could add the following two lines somewhere early in the code:
error_reporting(E_ALL);
ini_set('display_errors', 1);
However, you are using WordPress, so you will need to do something like this in your wp-config.php file:
// find the line that looks like this and comment it out
// define('WP_DEBUG', false);
// add these two lines directly below it
define('WP_DEBUG', true);
ini_set('display_errors', 1);
DEBUG #2
Make sure your code is running. Don't be afraid to throw a die() statement directly above it, to make sure it is running. Something like this:
// add a die() before everything
die('I am running. Awesome!');
// revised code
$account = Cart66Session::get(Cart66AccountId);
global $wpdb;
$fname = $wpdb->get_var(
$wpdb->prepare(
'select fname from `vfp_cart66_accounts` where id = %d limit 1',
$account
),
ARRAY_N
);
echo $fname;
DEBUG #3
To debug #3, you need either access to a commandline tool for MySQL or some type of GUI interface like phpMyAdmin, so that you can run a query directly from the database. Here is the query you should run:
show tables like 'vfp_cart66_%';
This is an example of one of the only places in SQL that you should ever quote a table name in single quotes. Running this will yield a list of all the tables that start with vfp_cart66_. If you get no results, then your table name is wrong. If your results do not include vfp_cart66_accounts, then your table name is wrong. If you see vfp_cart66_accounts, you are good to go.
DEBUG #4
This one will need to be run directly from the DB or through something like phpMyAdmin also. You are trying to make sure you have the correct field name. The way you do that is:
show create table `vfp_cart66_accounts`;
Assumedly, the field you are calling id would be the auto_incremented field in the table. Thus you are looking for a line, similar to this one:
`id` bigint(20) NOT NULL AUTO_INCREMENT,
Make sure that the line that has AUTO_INCREMENT on it, begins with:
`id`
If it does not, and the name is something else other than id, then you probably have the wrong field name.
DEBUG #5
Make sure you actually have data to display. From your mysql console or phpMyAdmin, run:
select * from `vfp_cart66_accounts` limit 1;
If you bet any results, then you have data, and you are good.
DEBUG #3 - #5 (alternate methods)
Another option you have is to dump the $wpdb object, directly after you run the query, because it contains the last error you received from MySQL. You can do this like so:
$fname = $wpdb->get_var(
$wpdb->prepare(
'select fname from `vfp_cart66_accounts` where id = %d limit 1',
$account
),
ARRAY_N
);
// dump a readable version of the $wpdb object
echo '<pre>';
print_r($wpdb);
die('</pre>');
Often times, reading the MySQL error message helps narrow down the problem in your SQL syntax.
DEBUG #6
If none of this has helped at all, then you will need to use your experience to trackdown a random bug in either your plugins or theme, what could literally be anything. You may as well not even dig in core WP code because, while it does have a couple minor bugs unrelated to your problem, which are getting repaired as we speak, it is one of the most stable CMS platforms out there. It is used by more of the top 10 million sites on the internet than any other CMS, for a good reason. It works, it is up-to-date, and most of all, it is stable.
I really hope you found this helpful or at least learned something from it. Hopefully others find it useful as well.
$fname=$wpdb->get_results(
"SELECT * FROM `vfp_cart66_accounts` WHERE id = '$account'",
ARRAY_N"
);
I can not get an SQL update statement to subtract a variable from a table value. Here is my code:
$_SESSION_Job101=mysql_fetch_array(mysql_query("SELECT * FROM job_101 WHERE job_101.username='$_SESSION_User'"));
mysql_query("UPDATE characters SET currenergy=currenergy-$_SESSION_Job101['ecost'] WHERE username='$_SESSION_User'");
$_SESSION_Job101 is a perfectly valid result, as I pull from it on another page; I even pull the 'ecost' on said page. I also update currenergy this way in another script, except I use the number 1 instead of the variable. So I've narrowed it down to that variable.
It wouldn't matter that $_SESSION_Job101 is the result from a second table (job_101), and that query is updating to the table characters, would it?
We don't have enough information, but since you don't perform ANY error handling or validation that SQL resultset is returned, it could be an error caused by issues such as:
no rows returned in first query
some other parsing issue not directly evident
I would propose that you use temporary strings and echo the actual SQL queries.
Continue by actually testing them with MYSQL (through workbench, queryviewer, or console) in order to see where and what the error is.
Also, it's not recommended to skip error checking and try to combine so many lines/steps into 2 lines.
Imagine the first query does not return any results for example...
Debugging:
$query1 = "SELECT * FROM job_101 WHERE job_101.username='$_SESSION_User'";
echo $query1."<br/>";
$_SESSION_Job101=mysql_fetch_array(mysql_query($query1 ));
$query2 = "UPDATE characters SET currenergy=currenergy-$_SESSION_Job101['ecost'] WHERE username='$_SESSION_User'";
echo $query2."<br/>";
mysql_query($query2);
Update
Based on your comment I suggest you try the following two options:
1) Add a space between the - and $_SESSION_Job101['ecost'].
2) If that doesn't work, change your string to:
mysql_query("UPDATE characters SET currenergy=currenergy-".$_SESSION_Job101['ecost']." WHERE username='".$_SESSION_User."'";`
I am trying to update one of my row with PHP using MySQLi and if I refresh PHPMyAdmin less than 1 second after update it's sucessfull but if I only check the data 2 seconds+ after the update it doesn't update.
For exemple:
'UPDATE orders SET MachineID = '.$id.' WHERE OrderID = '.$OrderID.
'AND ProductID = '.$ProductID
Does not work unless I refresh quickly but...
'UPDATE orders SET MachineID = 2 WHERE OrderID = 4 AND ProductID = 12'
Will work no matter how long after I refresh. (Those are the data I normaly use to test.)
So I though it would be my variables, but I'm using them almost 10 times before this part of the code in other queries and it works perfectly.
I tried to trim() the variable and it did not help.
I also tried to use mysqli_real_escape_string() with no success.
mysqli_error() is not giving me anything.
mysqli_affected_rows() is giving me "1" which is what it is suppose to be.
And the weird part is if I execute 'SELECT MachineID FROM orders WHERE OrderID = 4 AND ProductID = 12', it gives me the updated answer even if phpMyAdmin does not update the data.
There is no other code after this, so nothing that could "reverse" the update.
A normal "test" ouput looks like this:
Edit: This it what the browser outputs.
ID: "2" //$id
OrderID: "4" //$OrderID
ProductID: "12" //$ProductID
UPDATE orders SET MachineID = 2 WHERE OrderID = 4 AND ProductID = 12 //Query
boolean true //Result var_dump
1 //Number of rows affected
2 //Machine ID
Note: The quotes are not part of the variables.
Edit: This is the PHP code
$query = 'UPDATE orders SET MachineID = '.$id.' WHERE OrderID = '.$OrderID.' AND ProductID = '.$ProductID;
echo 'ID: "'.$id.'"<br/>
OrderID: "'.$OrderID.'"<br/>
ProductID: "'.$ProductID.'"<br/>'.$query.'<br/>';
$res = $db->query($query);
var_dump($res);
echo mysqli_affected_rows ($db).'<br/>';
$result = $db->query('SELECT MachineID FROM orders WHERE OrderID = 4 AND ProductID = 12');
$result = $result->fetch_array();
echo $result[0];
I really don't understand why it would work in the file and after a quick refresh but not if it takes to long to retreive the data. It's like it would reset after a certain amount of time if it's not fetch.
I've been working on this for almost 2 days now I have no idea why it's not working. This is some pretty simple SQL query.
Edit: I've look into MySQL binary logs and it seems that I'm updating it back to 1 every time. The only way this could be happening is if the file would run twice, but if it runs twice why would the output be there only once ?
Edit: Ok, so it seems like the problem comes from Google Chrome. I've tested it on IE and it works. For some reason Chrome would be running the file twice.
Ok so I was able to find the problem and fix it.
Problem:
The problem was the famous "favicon" bug with Chrome which is trying to get the icon even if it doesn't exist, therefore the file is called twice.
Fix:
Since this file was meant to be called through an Ajax call the bug is not triggered since Chrome will not try to look for the "favicon". I basicly fixed the bug by testing it how it was supposed to be run and not just testing the file itself.
I'm running this via PHP and well the first query gets run perfectly then right after it I run the second query and it does nothing. Its rows never get updated for some reason, but when I type out the query manually on the sql server, it works perfectly.
Anyone have any idea why its not working in the php code?
$qry = "UPDATE Matches SET winner ='$winner' WHERE TOURN_KEY = '$tournKey'AND MATCH_KEY='$matchKey' ";
$result = #mysql_query($qry);
$qryPoints = "UPDATE members, MemberBets SET members.points = members.points + MemberBets.amountBet + MemberBets.amountBet WHERE members.member_id=MemberBets.member_id and MemberBets.MATCH_KEY ='$matchKey' and MemberBets.TOURN_KEY = '$tournyKey' and MemberBets.player = '$winner'";
$resultPoints = #mysql_query($qryPoints);
You appear to have a typo. You have a $tournKey variable in the first query and a $tournyKey variable in the second. Since you say the first query works, I'm guessing the second variable name is wrong.