Hey,
I have this query:
Code $sql_breakfast1 = "UPDATE member_food SET food_id='$breakfast1' WHERE member_id='$id' AND food_type = 'breakfast'";
This is the result I get when I echo it:
Quote:UPDATE member_food SET food_id='7' WHERE member_id='4' AND food_type = 'breakfast'
It sets all the food_id columns to '1' where the members id is 4 and food_type is breakfast... however if I go into SQL and do it manually, it works perfectly.
Any suggestions?
Try this;
PHP Code
<?php $sql_breakfast1 = "UPDATE `member_food` SET `food_id`='".$breakfast1."' WHERE `member_id`='".$id."' AND `food_type` = 'breakfast'";
However; I strongly discourage the use of mysql_* queries.
(Oct 29, 2014 03:49 AM)FantaŠec Wrote: Try this;
PHP Code
<?php $sql_breakfast1 = "UPDATE `member_food` SET `food_id`='".$breakfast1."' WHERE `member_id`='".$id."' AND `food_type` = 'breakfast'";
However; I strongly discourage the use of mysql_* queries.
Hey dude,
using mysqli, also, it doesn't appear to be changing it:
Code Query: UPDATE `member_food` SET `food_id`='6' WHERE `member_id`='4' AND `food_type` = 'breakfast'
Try this one:
PHP Code
<?php $sql_breakfast1 = "UPDATE member_food SET food_id=".$breakfast1." WHERE member_id=".$id." AND food_type = 'breakfast'";
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
(Oct 29, 2014 04:35 AM)Squito Wrote: Try this one:
PHP Code
<?php $sql_breakfast1 = "UPDATE member_food SET food_id=".$breakfast1." WHERE member_id=".$id." AND food_type = 'breakfast'";
Nope, I decided to give it another whirl and test if it would actually send a query.
Tried this: Quote:UPDATE member_food SET food_type = 'test' WHERE member_id='4'
Worked, all the food_types are now test...
When I change it to:
UPDATE member_food SET food_id= '16' WHERE member_id='4'
it sets them to 1...
Could you post the whole code? If these queries are not executed, then maybe something else is the source of the problem.
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
Show us the block of code in which you're executing the query.
(Oct 29, 2014 05:25 AM)Squito Wrote: Could you post the whole code? If these queries are not executed, then maybe something else is the source of the problem.
For reference, here is the WHOLE piece of code:
Code <?php
include 'header.php';
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: login-form.php");
} else {
//Inputs
// Escape user inputs for security
$id = mysqli_real_escape_string($mysqli, $_POST['id']);
$user = mysqli_real_escape_string($mysqli, $_POST['username']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
//Breakfast
$breakfast1 = mysqli_real_escape_string($mysqli, $_POST['breakfast1']);
//inserting data
//WORKING QUERY: UPDATE member_food SET food_id=12 WHERE member_id=4 AND food_type="breakfast"
//$sql_breakfast1 = "UPDATE `member_food` SET `food_id`='".$breakfast1."' WHERE `member_id`='".$id."' AND `food_type` = 'breakfast'";
$sql_breakfast1 = "UPDATE member_food SET food_id='12' WHERE (member_id='5' AND food_type='breakfast')";
if ($mysqli->query($sql_breakfast1) === TRUE) {
echo "Query: " . $sql_breakfast1 . $mysqli->error;
} else {
echo "Query: " . $sql_breakfast1 . "<br> Error: " . $mysqli->error;
}
}
?>
Where are you connecting to the database?
(Oct 29, 2014 05:45 AM)firefusion Wrote: Where are you connecting to the database?
in header.php
That's not the issue. It connects fine.
The provided code above should've worked.
PHP Code
<?php $sql_breakfast1 = "UPDATE `member_food` SET `food_id` = '" . $breakfast1 . "' WHERE `member_id` = '" . $id . "' AND `food_type` = 'breakfast'";
Ensure you used it correctly.
(Oct 29, 2014 06:50 AM).russo Wrote: The provided code above should've worked.
PHP Code
<?php $sql_breakfast1 = "UPDATE `member_food` SET `food_id` = '" . $breakfast1 . "' WHERE `member_id` = '" . $id . "' AND `food_type` = 'breakfast'";
Ensure you used it correctly.
Huh, now it's worked. Thanks :)
I have still got to figure out a few more things, would it be possible for me to add you on a different platform to chat?
(Oct 29, 2014 06:56 AM)bwt Wrote: Huh, now it's worked. Thanks :)
I have still got to figure out a few more things, would it be possible for me to add you on a different platform to chat?
...
That is the same exact code I gave you.
PHP Code
<?php include 'header.php';
if(ctype_digit($_POST['id']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) && ctype_alnum($_POST['username']) && ctype_alpha($_POST['breakfast1'])) {
//Inputs $id=$_POST['id']; $user=$_POST['username']; $email=$_POST['email']; $breakfast1=$_POST['breakfast1'];
//inserting data $sql_breakfast1="UPDATE member_food SET food_id=".$id." WHERE member_id=5 AND food_type='".$breakfast1."'";
if ($mysqli->query($sql_breakfast1) == TRUE) { echo "Query: ".$sql_breakfast1.$mysqli->error; } else { echo "Query: ".$sql_breakfast1."<br> Error: ".$mysqli->error; } } ?>
Put the session variable check into header.php before mysqli_connect, if the user is not logged in don't bother initiating a mysql connection.
PHP Code
<?php if (!isset($_SESSION['login'])) { header ("Location: login-form.php"); exit; }
If you log a user out simply use session_destroy(). Also put exit after header if there are contents not 'protected' by an if (eg: not everything else is in the else{} section), because browsers can/may ignore redirection.
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
|