farahZ Posted May 6, 2013 Share Posted May 6, 2013 i have an array foodtype of food, i want to get the corresponding calorie of each type of food in the arrayi'm trying this and i get nothing // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } foreach ($_SESSION['foodTypes'] as $food) { $query = "SELECT Calories FROM food where Food='$food'"; $result = mysqli_query($con,$query); while($row = mysqli_fetch_array($result)) { echo $row['Calories']; $calories=$calories + $row['Calories']; } } echo 'The amount of Calories is: ' . $calories; Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/ Share on other sites More sharing options...
Barand Posted May 6, 2013 Share Posted May 6, 2013 Have you checked the content of $_SESSION['foodTypes'] ? echo '<pre>', print_r($_SESSION,1), '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428550 Share on other sites More sharing options...
farahZ Posted May 6, 2013 Author Share Posted May 6, 2013 yup it is printing .. nothing wrong with the array Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428552 Share on other sites More sharing options...
Barand Posted May 6, 2013 Share Posted May 6, 2013 Does it contain valid "food" values to match those in your table? Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428580 Share on other sites More sharing options...
farahZ Posted May 6, 2013 Author Share Posted May 6, 2013 i tried to echo something just to test if its entering the IF statement .. which i realized its not entering!!thats the code for the button of 'Calculate Calories' <input name="Calculate Calories" type="submit" id="Calculate Calories" value="Calculate Calories"> else if (isset($_POST['Calculate Calories'])) { echo 'Calories'; // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } foreach ($_SESSION['foodTypes'] as $food) { $query = "SELECT Calories FROM food where Food=$food"; $result = mysqli_query($con,$query); while($row = mysqli_fetch_array($result)) { echo $row['Calories']; $calories=$calories + $row['Calories']; } } echo 'The amount of Calories is: ' . $calories; } Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428656 Share on other sites More sharing options...
Phear46 Posted May 6, 2013 Share Posted May 6, 2013 if you else if is not being entered, one of two things is happening - 1. another condition before(?) your else if is evaluating to true 2. php/html might not like a space in the button name. Not sure. ive always used _'s for spaces (using linux got me in that habbit pretty quick :/) Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428695 Share on other sites More sharing options...
jcbones Posted May 6, 2013 Share Posted May 6, 2013 I would trim this down a might, as you are running a lot of queries. // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $query = "SELECT Calories FROM food where Food IN (" . implode(',',$_SESSION['foodTypes']) . ")"; $result = mysqli_query($con,$query) or trigger_error(mysqli_error($con)); while($row = mysqli_fetch_array($result)) { echo $row['Calories']; $calories += $row['Calories']; } echo 'The amount of Calories is: ' . $calories; Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428696 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 yup you are Phear46, its the spaces issue ! jcbones, i tried executing your code more errorswith my code, there's an error concering the while loop: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\xampp\htdocs\inshapewebsite\calorieCounter2.php on line 91 and the calories variableNotice: Undefined variable: calories echo 'Calories'; // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } foreach ($_SESSION['foodTypes'] as $food) { $query = "SELECT Calories FROM food where Food=$food"; $result = mysqli_query($con,$query); while($row = mysqli_fetch_array($result)) { echo $row['Calories']; $calories=$calories + $row['Calories']; } } echo 'The amount of Calories is: ' . $calories; Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428806 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 Try echoing the count of the result to make sure that your getting results back and maybe var_dump $row, maybe you need to escape the query.. $query = "SELECT Calories FROM food where Food='$food' "; Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428811 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 (Y)putting quotations around '$food' in the query solved it !!when clicking calculate button, calories are calculated.. but when pressing submit buttom to send the amount of calories "$calories", its sent zeromaybe i should declare $calories as general variable that changes.. static right?whats the code for that?? Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428814 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 $calories is just a variable, not a form object so it wont just post when you click a submit button by default (unless your doing something with it using javascript/ajax) try something like this at the bottom of your page... <form action="process.php" method="post"> <input type="hidden" name="totalCalories" value="$calories" /> <input type="submit" name="submit" value="submit" /> </form> it will probably need changing slightly but here your creating a hidden form that contains the total value $calories which can be posted to another page, in that case process.php, just change that to what you need Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428816 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 the thing is i have 3 buttons: add (the user chooses food from drop down list and adds them) calculate (the user can know the total of the calories of the food he chose) submit (the data (id, date, food chosen, calories) are sent to the db) the 3 buttons already belong to one formsubmit button is working except for the calories because calculation is done in the 'calculate' button Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428820 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 You will still a form element with the value for the total in order to post it, If you could show your code for the form I can help Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428821 Share on other sites More sharing options...
Barand Posted May 7, 2013 Share Posted May 7, 2013 It sounds like the above code should be called by an AJAX request (when Calculate is clicked) which should then return the calories total to a form field Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428827 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 i found another way by creating the function 'calculate' outside.. so i'm calling it when the user presses 'add' or 'submit'the function is called when both buttons are pressed to update the user with the total calories continuously .. its working perfect i've never heard of the AJAX beforebut still i have a problem, the total isn't sent to the database .. its remaining zero in the dbshould i declare the $total as integer because its integer in the db.. i am sensing its data types issethats the code of submission: ps: everything is sent normally except for the '$total' // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $clid=111; $date=date("m.d.y"); foreach ($_SESSION['foodTypes'] as $food) { $foodS= $foodS . ",". $food; } $calories= calculate($_SESSION['foodTypes']); $query = "SELECT Calories FROM caloriescounter where ID='$clid' and Date='$date'"; $result1 = mysqli_query($con,$query); $row = mysqli_fetch_array($result1); $total=$calories + $row['Calories']; echo 'The amount of Calories registered is: ' . $total; $result = mysqli_query($con,"INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES ('$clid', '$date','$foodS', '$total') ON DUPLICATE KEY UPDATE Lunch = '$foodS'"); //mysqli_query($con,"INSERT INTO caloriescounter (ID, Date, Lunch) //VALUES ('$clid', '$date','$foodS')"); mysqli_close($con); // session exists and has content // process the array list here. // after the processing, empty the session $_SESSION['foodTypes'] = array(); } Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428828 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 Does it echo out the total? you shouldn't enclose the $total value in quotes as it is a number like you said "INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES ($clid, '$date','$foodS', $total) Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428829 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 yes its echoing the correct value !!i tried removing the quotes.. not submitting Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428830 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 try this... $result = mysqli_query($con,"INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES ('$clid', '$date','$foodS', '$total') ON DUPLICATE KEY UPDATE Lunch = '$foodS'") or die(mysqli_error()); You might need to check the mysqli_error() bit, I haven't used mysqli for a while Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428831 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 Notice: Undefined variable: foodS in D:\xampp\htdocs\inshapewebsite\calorieCounter2.php on line 99Notice: Undefined variable: calories in D:\xampp\htdocs\inshapewebsite\calorieCounter2.php on line 136 '$foodS' is correctly sent but '$total' no Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428834 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 (edited) try this.. // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $clid=111; $date=date("m.d.y"); $foodS = ''; $total = 0; foreach ($_SESSION['foodTypes'] as $food) { $foodS= $foodS . ",". $food; } $calories = (int) calculate($_SESSION['foodTypes']); $query = "SELECT Calories FROM caloriescounter where ID=$clid AND Date = '$date'"; $result1 = mysqli_query($con,$query); $row = mysqli_fetch_array($result1); $total = $calories + (int) $row['Calories']; $sql = "INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES ($clid, '$date','$foodS', $total) ON DUPLICATE KEY UPDATE Lunch = '$foodS'"; var_dump($sql); echo 'The amount of Calories registered is: ' . $total; $result = mysqli_query($con, $sql) or die(mysqli_error()); //mysqli_query($con,"INSERT INTO caloriescounter (ID, Date, Lunch) //VALUES ('$clid', '$date','$foodS')"); mysqli_close($con); // session exists and has content // process the array list here. // after the processing, empty the session $_SESSION['foodTypes'] = array(); } Edited May 7, 2013 by DarkKnight2011 Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428835 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 error.. Notice: Undefined variable: calories in D:\xampp\htdocs\inshapewebsite\calorieCounter2.php on line 146string(174) "INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES (111, '05.07.13',',Cucumber Pickles,Lettuce', 25) ON DUPLICATE KEY UPDATE Lunch = ',Cucumber Pickles,Lettuce'" The amount of Calories registered is: 25 Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428836 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 The value is being passed into the query correctly, i think it may be due to the ON DUPLICATE KEY statement, why dont you auto increment the ID key? try running that query directly in mysql client / phpmyadmin / mysql workbench and it will show the error that is being thrown Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428837 Share on other sites More sharing options...
Barand Posted May 7, 2013 Share Posted May 7, 2013 you really should normalize your data and not store food list like this Lunch = ',Cucumber Pickles,Lettuce'" Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428843 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 (edited) Darknight you are right, the problem is with duplicate key !!i tried this and it worked out, calories are being saved finally!!! barand what do u mean by normalize??? $sql = "INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES ($clid, '$date','$foodS', $total) ON DUPLICATE KEY UPDATE Lunch = '$foodS', Calories=$total"; Edited May 7, 2013 by farahZ Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428845 Share on other sites More sharing options...
farahZ Posted May 7, 2013 Author Share Posted May 7, 2013 how do i get rid of errors that show up after an action, though the action is done fully??like this Notice: Undefined variable: calories in D:\xampp\htdocs\inshapewebsite\calorieCounter2.php on line 146string(155) "INSERT INTO caloriescounter (ID, Date, Lunch, Calories) VALUES (111, '05.07.13',',Cucumber', 20) ON DUPLICATE KEY UPDATE Lunch = ',Cucumber', Calories=20" Quote Link to comment https://forums.phpfreaks.com/topic/277691-retrieve-data-from-database-variable-in-the-query/#findComment-1428847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.