UPDATE
Maybe I am just a dummy and can't see my mistake. Basically this is function is handling the math behind everything else. It has multiple queries and updates and inserts in two different tables..
When I try to process it, it gives me:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/content/53/7311353/html/gs/cca/accounts/include/processAct.php on line 241
Here's my function:
function calculateBilling(){
$date = date('mdY');
$bid = mysql_real_escape_string($_POST['bid']);
$account = mysql_real_escape_string($_POST['account']);
$timein = mysql_real_escape_string($_POST['timein']);
$desc = mysql_real_escape_string($_POST['desc']);
$hrs2calc1 = mysql_real_escape_string($_POST['hrly']);
$hrs2calc2 = mysql_real_escape_string($_POST['rhrly']);
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid;
$result = mysql_query($query);
HERES LINE 241 ----> while($row = mysql_fetch_row($result)){
$accounttobebilled = $row[1];
$hrly = $row[2];
$rhrly = $row[3];
$curbal = $row[4];
}
$sub1 = $hrly * $hrs2calc1;
$sub2 = $rhrly * $hrs2calc2;
$subtotal = $sub1 + $sub2;
$total = $curbal + $subtotal;
$query2 = 'UPDATE billing SET bal = '.$total.' WHERE bid ='.$bid;
$result2 = mysql_query($query2);
// Update Billing Log for this customer
mysql_query("INSERT INTO billingLog (bid, date, hrsOnsite, hrsRemote, timein, descript, total) VALUES ('$bid', '$date', '$hrs2calc1', '$hrs2calc2', '$timein', '$desc', '$subtotal')");
}
I think the problem is coming from my select (drop down) where it posts to the script:
<select class="form-dropdown validate[required]" style="width:150px" id="input_5" name="account">
<?php
while($row =
mysql_fetch_row($result)){
$bid =$row[0];
$account = $row[1];
echo '<option value="'.$bid.'">'.$account.'</option>';
}
?>
</select>
For James:
SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid=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 '' at line 1
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/content/53/7311353/html/gs/cca/accounts/include/processAct.php on line 243
UPDATE billing SET bal = 0 WHERE bid =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 '' at line 1INSERT INTO billingLog (bid, date, hrsOnsite, hrsRemote, timein, descript, total) VALUES ('', '07292011', '2', '2', '2', '2', '0')
If you use this instead, what output do you get:
function calculateBilling(){
$date = date('mdY');
$bid = mysql_real_escape_string($_POST['bid']);
$account = mysql_real_escape_string($_POST['account']);
$timein = mysql_real_escape_string($_POST['timein']);
$desc = mysql_real_escape_string($_POST['desc']);
$hrs2calc1 = mysql_real_escape_string($_POST['hrly']);
$hrs2calc2 = mysql_real_escape_string($_POST['rhrly']);
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid;
echo $query;
$result = mysql_query($query);
echo mysql_error();
while($row = mysql_fetch_row($result)){
$accounttobebilled = $row[1];
$hrly = $row[2];
$rhrly = $row[3];
$curbal = $row[4];
}
$sub1 = $hrly * $hrs2calc1;
$sub2 = $rhrly * $hrs2calc2;
$subtotal = $sub1 + $sub2;
$total = $curbal + $subtotal;
$query2 = 'UPDATE billing SET bal = '.$total.' WHERE bid ='.$bid;
echo $query2;
$result2 = mysql_query($query2);
echo mysql_error();
// Update Billing Log for this customer
$query3 = "INSERT INTO billingLog (bid, date, hrsOnsite, hrsRemote, timein, descript, total) VALUES ('$bid', '$date', '$hrs2calc1', '$hrs2calc2', '$timein', '$desc', '$subtotal')";
echo $query3;
mysql_query($query3);
echo mysql_error();
}
It's your concatenation.
Change
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid.'';
to
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid='.$bid;
I'm also assuming that bid is an integer. Otherwise you need quotes:
$query = 'SELECT bid, account, hrly, rhrly, bal FROM billing WHERE bid="'.$bid.'"';
This is wrong too
mysql_query("UPDATE billing SET bal = '$total' WHERE bid ='.$bid.'");
should be something like
mysql_query("UPDATE billing SET bal = '{$total}' WHERE bid ='{$bid}'");
-- or full concatenation
mysql_query("UPDATE billing SET bal = '" . $total . "' WHERE bid ='" . $bid . "'");
Same goes for you last query.
With the information provided, it's kinda hard to figure out what the problem is. Your best solution is outputting mysql_error() right after you run the query.
$result = mysql_query($query);
echo mysql_error();
Unless you have incorrectly specify table name or field name, the value on your SELECT statement, should be wrapped with proper quoted.
To me, it seems helpful to check the result of generating the SQL query string from php eg. echo $query (that should show the presumed error in the first query).
If reading the string does not spot the errors, feeding it via mysql into a test db might help a lot, especially. Mixing sql, php, single and double quotes is not always easy write nor read ...
Related
I have an orders table in mysql and in that for some orders I set particular order status like 'review'.
I want to setup a way if any order placed by a particular customer(first and last name) for whom i have previously set order status as 'review' to display a warning in the list.
$sql = "select * from order where firstname = ".$firstname." AND lastname = ".$lastname." AND order_status = 'review';";
$SQLresult = mysql_query("$sql", $DBcon_MySQL);
while($row = mysql_fetch_array($SQLresult)) {
foreach($row as $row){
$result = "warning!";
echo $result;
}
}
The above code does not display anything. please let me know how to fix this.
[EDIT After Applying Answer]
This is how i am using it.
<td width="200">
<?
$sql = "select * from cust_order where firstname = '$firstname' AND lastname = '$lastname' AND order_status = 'review'";
$SQLResult = mysql_query("$sql", $DBcon_MySQL);
while($row = mysql_fetch_array($SQLResult )) {
//$result;
foreach($row as $row ){
//$result="";
$result = "Warning!";
}
?>
<p><? echo $result;?></p>
<?} ?>
</td>
How should i insert a check that it should display warning only once No matter how many orders from single customer are marked as review, display warning only once?
try this,
$sql = "SELECT
*
FROM
`order`
WHERE
firstname = '$firstname' AND lastname = '$lastname' AND
order_status = 'review' LIMIT 1";
$SQLresult = mysql_query($sql, $DBcon_MySQL);
while($row = mysql_fetch_array($SQLresult)) {
foreach($row as $row){
$result = "warning!";
echo $result;
}
}
Please be informed that mysql functions are deprecated and not recommended. USE MySQLi or PDO instead. have a reference from following queries.
http://php.net/manual/en/book.mysqli.php
http://php.net/manual/en/book.pdo.php
I Queried Database Table 'users' for 'user_id'. and get an array of ids.
$sel = "SELECT user_id FROM users WHERE status='Approved'";
$result = #mysqli_query ($dbcon, $sel);
Then i inserted values into another table income for all those user ids.
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$ins = "INSERT INTO income (user_id, income_amount) VALUES ('$row', '100')";
$giv = #mysqli_query ($dbcon, $ins);
}
Notice: Array to string conversion in E:\xampp\htdocs\project\t.php on line 109
Can anyone help me resolve this issue.
$sel = "SELECT user_id FROM users WHERE status='Approved'";
$result = #mysqli_query ($dbcon, $sel);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$ins = "INSERT INTO income (user_id, income_amount) VALUES ('" . $row['user_id'] . "', '100')";
$giv = #mysqli_query ($dbcon, $ins);
}
First , Check if $results is in array ..you can put some error handling checked is_array($result).
If it is fine then pass it to mysqli_fetch_array().
Do't add suppress # error ,while developing.
i would like to suggest you a single query for that so after that you need not to use while loop to insert your data in income table:
Just try it :
INSERT INTO income (user_id,income_amount) SELECT user_id,'100' AS income_amount FROM users WHERE status = 'Approved';
You can use it like that way :
$sel = "INSERT INTO income (user_id,income_amount) SELECT user_id,'100' AS income_amount FROM users WHERE status = 'Approved'";
$result = #mysqli_query ($dbcon, $sel);
Recently I copied a PHP script to use it for another database. After I correctly edited all the words and links, I got an weird error.
SELECT naam, aantal, prijs
FROM boeken, bestelling
WHERE boeken.Boekcode = bestelling.Boekcode
AND bestelling.Boekcode IN ('101','102')
AND bestelnummer = 3;
It's not a regular error that say something like
Error on line 42
Basically what the code does, is that when you order books and filled in a form (Name, last name, email etc) it puts that in a database. And afterwards puts it in a "thankyou.html" page.
Here's part of the code that causes this
mysqli_query($con, $query) or die($query . "<br>");
$bestelnummer = MYSQLI_INSERT_ID($con);
$object = array_filter($object);
$objectnaam = join("','",array_keys($object));
$object = http_build_query($object);
$object = str_replace('=', ',', $object);
$object = str_replace('&', "),($bestelnummer,", $object);
$query = "INSERT INTO bestelling (bestelnummer, Boekcode, aantal) VALUES ($bestelnummer,$object)";
$result = mysqli_query($con, $query) or die($query."<br>");
$aantal = "SELECT naam, aantal, prijs FROM boeken, bestelling WHERE boeken.Boekcode = bestelling.Boekcode AND bestelling.Boekcode IN ('".$objectnaam."') AND bestelnummer = $bestelnummer";
$result = mysqli_query($con, $aantal) or die($aantal . "<br>");
$res = mysqli_fetch_all($result);
$prijs = 0;
The $object is an array of the books you choose.
I've Google'd for this problem, and yes, I have Apache and everything enabled.
Sorry if I'm unclear, it's been a while, I can answer any questions you might have.
Well you echo out the query if it fails.
$aantal = "SELECT naam, aantal, prijs FROM boeken, bestelling WHERE boeken.Boekcode = bestelling.Boekcode AND bestelling.Boekcode IN ('".$objectnaam."') AND bestelnummer = $bestelnummer";
$result = mysqli_query($con, $aantal) or die($aantal . "<br>");
You might want to check the error message that is returned from the database using mysqli_error().
$result = mysqli_query($con, $aantal) or die(mysqli_error() . "<br>");
first need to refine
$query = "INSERT INTO bestelling (bestelnummer, Boekcode, aantal) VALUES ($bestelnummer,$object)";
you are selecting values insertion for 3 columns and passing only two values
For second query try to use backticks for tablename and column
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I am trying to insert multiple rows in mysql database using php.
A portion of the code is as below.
$b_address = $_POST["b_address"];
$s_address = $_POST["s_address"];
$query = "INSERT INTO order VALUES";
foreach ($_SESSION['buy'] as $products) {
$username = $_COOKIE["uname"];
$Product_Name = $products["Product_Name"];
$qty = $products["qty"];
$price = $products['qty'] * $products['Price'] ;
$query .= "('',
(select id from user_detail where user_name = $username ) ,
(select Product_id from products where Product_Name = $Product_Name ) ,
$qty,
$price ,
$b_address ,
$s_address ,
NOW()
),";
}
rtrim($query, ',');
But i am getting some syntex error where selecting id.
How to get rid of the syntex error and run the code properly?
error i am getting is as below :
errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order VALUES('', (select id from user_detail where user_name = ar' at line 1
EDIT
I changed the line into $query = "INSERT INTOordersVALUES";
and now the error i am getting is :
errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 21
EDIT 2
Here is the whole code of the page, incase i am blindly mistaking somewhere.
<?php
session_start();
$con=mysql_connect('localhost','root','mypass');
if(!$con)
{
die ('connection error'.mysql_error());
}
mysql_select_db('test1',$con);
if (isset($_POST['submit'])) {
if (!empty($_POST['b_address']) && !empty($_POST['s_address']) ) {
$b_address = $_POST["b_address"];
$s_address = $_POST["s_address"];
$query = "INSERT INTO `orders` VALUES ";
foreach ($_SESSION['buy'] as $products) {
$username = $_COOKIE["uname"];
$Product_Name = $products["Product_Name"];
$qty = $products["qty"];
$price = $products['qty'] * $products['Price'] ;
$query .= "('',
(select id from user_detail where user_name = '$username' ) ,
(select Product_id from products where Product_Name = '$Product_Name' ) ,
'$qty',
'$price' ,
'$b_address' ,
'$s_address' ,
NOW()
),";
}
rtrim($query, ',');
if(!mysql_query($query,$con))
{
die ("error".mysql_error());
}
else
{
echo "Thank you for your purchase. Your order is under processing.";
unset($_SESSION['buy']);
}
}else{
echo 'All fields are required.';
}
}
Try this:
Please observe order
$query = "INSERT INTO `order` VALUES ";
foreach ($_SESSION['buy'] as $products) {
$username = $_COOKIE["uname"];
$Product_Name = $products["Product_Name"];
$qty = $products["qty"];
$price = $products['qty'] * $products['Price'] ;
$query .= "('',
(select id from user_detail where user_name = '$username') ,
(select Product_id from products where Product_Name = '$Product_Name') ,
'$qty',
'$price' ,
'$b_address' ,
'$s_address' ,
NOW()
),";
Explanation: Order is MySQL reserved word.
You can not use it in your SQL for any Table name or field name.
Been tinkering with my website, it is a seat booking website. Still in alpha testing really so not live to the public yet for obvious reasons.
However, I'm having a few problems with updating the values in my database.
I'll post the code and then explain the problem..
else {
$seatID = $_POST['form_submitted'];
$query1 = "SELECT seatTaken FROM SEATS WHERE seatNo = '$seatID'";
$result = mysql_query($query1);
while($row = mysql_fetch_array($result))
{
$taken = $row['seatTaken'];
}
$query2 = "SELECT passNo FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query2);
while($row = mysql_fetch_array($result))
{
$passno = $row['passNo'];
}
$query3 = "SELECT groupID FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query3);
while($row = mysql_fetch_array($result))
{
$groupno = $row['groupID'];
}
$query4 = "SELECT flightNo FROM PASSENGER WHERE username = '$loggedinuser'";
$result = mysql_query($query3);
while($row = mysql_fetch_array($result))
{
$flightno = $row['flightNo'];
}
// if ($taken = 0) {
$update = mysql_query("UPDATE PASSENGER SET seatNo = $seatID WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passNo', groupID = '$groupid' WHERE seatNo = '$seatID'");
// AND flightNo = '$flightno'"
echo '<meta http-equiv="refresh" content="5;url=http://www.mywebsite.com/">';
echo mysql_error();
//}
}
?>
Now the user will have selected their seat in the previous form hence the:
$seatID = $_POST['form_submitted'];
However, at the bottom in my queries, the only value that actually changes in the database when this PHP code is run is the boolean value of 'seatTaken', in that it does change from 0 (not occupied) to 1 (occupied).
The field passNo and groupID in my database DO NOT UPDATE as referenced here in these queries:-
$update = mysql_query("UPDATE PASSENGER SET seatNo = $seatID WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passNo', groupID = '$groupid' WHERE seatNo = '$seatID'");
Is anyone able to help? Many thanks!
Tom
Watch your variable naming and string quotation
When your looking for values in mysql, they usually need to be a string literal (add quotes).
And your other problem is your variable names:
$update = mysql_query("UPDATE PASSENGER SET seatNo = '$seatID' WHERE username = '$loggedinuser'");
$update2 = mysql_query("UPDATE SEATS SET seatTaken = 1, passNo = '$passno', groupID = '$groupno' WHERE seatNo = '$seatID'");
$passno vs $passNo
$groupid vs $groupno
You should also make sure you properly escape any input coming from the user http://php.net/manual/en/function.mysql-real-escape-string.php
One can't see in your code how do you generate the values of $groupid, $passNo, $seatID. Are those varaibles set when you do your update? (just echo the SQL code to see what query is being sent to your database)
Maybe you should try getting the variables from your post request, like $_POST['groupid'], if groupid is the name of the field in the form.