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.
Related
I'm running the following PHP code bellow. I have a string with seperated data (separated by ";"). I want to split this data into an array and with foreach I want to run the same INSERT sql query, changing with the array actual item only.
After I runned the code the succesfully message appearing. But as I described only one record get into the database (and only one time so if I do the same thing at the second time no more record added, also not the record before what is there in the database already too. (This is why I got so confused.)
String example: "email#email.com;email2#email2.com;"
The PHP code:
$connection = dbconnect();
$mail_addresses = explode(";",$_POST["mailaddresslist"]);
for($i = 0; $i < count($mail_addresses); $i++){
$query = "INSERT INTO `tablename` (`emsd_email`, `emsd_theme`, `emsd_timerdate`) VALUES ('".$mail_addresses[$i]."','". $_POST["themes"]."','".$_POST["emailsenddate"] ."');";
$connection->query($query);
}
print_note("E-mail successfully added!");
There is no any error message the code is running down. In the first time it create one record (just with the first item in the array), but after that the code doesn't make any effect at all. It no matters that how many times I run it does nothing. There's no more record appearing in the database.
I solved it. The problem was the record must have a unique id, and it must be Auto Increment. Now it works fine. :)*
I have a very simple query which updates a 'status', a 'has_note', (both are tinyints in the database) and a time. The time updates correctly every time this is run, however, the other two are not affected and never changed.
Here is the code:
$status_sql = "
UPDATE voe_employment
SET status = 5, email_date = NOW()
WHERE emp_id = " . $_POST['emp_id'] . "
LIMIT 1";
$status_result = mysql_query($status_sql);
I have copied and pasted the resulting $status_sql into pmadmin, and everything updates correctly.
Also, $status_result = 1 after executing this code, which signifies success.
This block of code is wrapped around a "try, catch" statement, and the catch is never run/activated. And I have tried wrapping the table name in ``, wrapping the 5 in '', etc.
Check to make sure no other code is doing another UPDATE right after. We have found this in the past. Another unexpected update was running that we didn't know was that overwrote the data.
I'm attempting to work with two sets of data from the same mySQL table in a PHP script. The idea is data is scraped from an API and into a database hourly. A second script then pulls the information out of the database and displays a rolling 6-hour delta.
I've run into a bit of a problem trying to create the delta from the two datasets. I need to run two mySQL queries to get the data I need (current and from 6 hours ago), but can't really think of a way to get the script to work without including the queries inside the loops that output each entry (These can run up to a couple of hundred times, and I don't think having that many mySQL queries running would be good?)
This is what I have so far:
//select the system table and pulls data acquired within the last hour.
$sql = "SELECT system, vp, vpthreshold, owner, time FROM SysData WHERE time > DATE_SUB(NOW(),INTERVAL 1 HOUR)";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Calculates contested percentage
$vpthreshold = $row["vpthreshold"];
$vp = $row["vp"];
$currentcontested = $vp/$vpthreshold*100;
//Catches potential divide by zeroes, echos system is stable.
if ($vp == 0.0){
echo $row["system"] . " " . "is Stable<br>";
}
//else output contested percentage with system name in readable format.
else{
echo $row["system"] . " " . "{$currentcontested}%" . "<br>";
}
}
}
There's a broadly identical statement that pulls and echos the second set of information underneath this. How can I get these two sets together so I can work with them? Very new to PHP and learning on the fly here.
You can look into nested queries. Something like the following:
SELECT (data_now.somevalue - data_prev.somevalue) as deltavalue FROM
(
(your first select statement) as data_now,
(your 6 hours ago select statement) as data_prev
);
This lets you select data from other select statements all in one go.
The 2 inner "select statements" you should replace with your respective queries. The results will be put temporarily into data_now and data_prev. You can then use these as normal tables in the outer select statement.
EDIT: To be more specific to what you want, here is an updated example:
SELECT (data_now.vp/data_now.vpthreshold - data_prev.vp/data_prev.vpthreshold) as deltavalue FROM
(
(SELECT system, vp, vpthreshold, owner, time FROM SysData WHERE time > DATE_SUB(NOW(),INTERVAL 1 HOUR)) as data_now,
(your 6 hours ago select statement) as data_prev
);
In your PHP code remember to reference the result as:
$row["deltavalue"]
or whatever you put after "as" in the outer SELECT.
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'm having problems debugging a failing mysql 5.1 insert under PHP 5.3.4. I can't seem to see anything in the mysql error log or php error logs.
Based on a Yahoo presentation on efficient pagination, I was adding order numbers to posters on my site (order rank, not order sales).
I wrote a quick test app and asked it to create the order numbers on one category. There are 32,233 rows in that category and each and very time I run it I get 23,304 rows updated. Each and every time. I've increased memory usage, I've put ini setting in the script, I've run it from the PHP CLI and PHP-FPM. Each time it doesn't get past 23,304 rows updated.
Here's my script, which I've added massive timeouts to.
include 'common.inc'; //database connection stuff
ini_set("memory_limit","300M");
ini_set("max_execution_time","3600");
ini_set('mysql.connect_timeout','3600');
ini_set('mysql.trace_mode','On');
ini_set('max_input_time','3600');
$sql1="SELECT apcatnum FROM poster_categories_inno LIMIT 1";
$result1 = mysql_query($sql1);
while ($cats = mysql_fetch_array ($result1)) {
$sql2="SELECT poster_data_inno.apnumber,poster_data_inno.aptitle FROM poster_prodcat_inno, poster_data_inno WHERE poster_prodcat_inno.apcatnum ='$cats[apcatnum]' AND poster_data_inno.apnumber = poster_prodcat_inno.apnumber ORDER BY aptitle ASC";
$result2 = mysql_query($sql2);
$ordernum=1;
while ($order = mysql_fetch_array ($result2)) {
$sql3="UPDATE poster_prodcat_inno SET catorder='$ordernum' WHERE apnumber='$order[apnumber]' AND apcatnum='$cats[apcatnum]'";
$result3 = mysql_query($sql3);
$ordernum++;
} // end of 2nd while
}
I'm at a head-scratching loss. Just did a test on a smaller category and only 13,199 out of 17,662 rows were updated. For the two experiments only 72-74% of the rows are getting updated.
I'd say your problem lies with your 2nd query. Have you done an EXPLAIN on it? Because of the ORDER BY clause a filesort will be required. If you don't have appropriate indices that can slow things down further. Try this syntax and sub in a valid integer for your apcatnum variable during testing.
SELECT d.apnumber, d.aptitle
FROM poster_prodcat_inno p JOIN poster_data_inno d
ON poster_data_inno.apnumber = poster_prodcat_inno.apnumber
WHERE p.apcatnum ='{$cats['apcatnum']}'
ORDER BY aptitle ASC;
Secondly, since catorder is just an integer version of the combination of apcatnum and aptitle, it's a denormalization for convenience sake. This isn't necessarily bad, but it does mean that you have to update it every time you add a new title or category. Perhaps it might be better to partition your poster_prodcat_inno table by apcatnum and just do the JOIN with poster_data_inno when you need the actually need the catorder.
Please escape your query input, even if it does come from your own database (quotes and other characters will get you every time). Your SQL statement is incorrect because you're not using the variables correctly, please use hints, such as:
while ($order = mysql_fetch_array($result2)) {
$order = array_filter($order, 'mysql_real_escape_string');
$sql3 = "UPDATE poster_prodcat_inno SET catorder='$ordernum' WHERE apnumber='{$order['apnumber']}' AND apcatnum='{$cats['apcatnum']}'";
}