June 24, 201016 yr Hi there, I am attempting to update a MySQL table with the following script but I can't get it to work even though I've tried all sorts of changes in the query. <?php require('dbconfig.php'); $updateid = $_SESSION['ID']; $new = "0"; $query = "UPDATE users SET new='$new' WHERE ID='$updateid'"; mysql_query($query); mysql_close(); header("Location: home.php"); ?> Can you see anything wrong? Thanks for your help, Jim
June 24, 201016 yr Hi there, I am attempting to update a MySQL table with the following script but I can't get it to work even though I've tried all sorts of changes in the query. <?php require('dbconfig.php'); $updateid = $_SESSION['ID']; $new = "0"; $query = "UPDATE users SET new='$new WHERE ID='$updateid;'"; mysql_query($query); mysql_close(); header("Location: home.php"); ?> Can you see anything wrong? Thanks for your help, Jim This $query = "UPDATE users SET new='$new WHERE ID='$updateid;'"; should be this $query = "UPDATE users SET new='".$new."' WHERE ID='".$updateid."'";
June 25, 201016 yr Author This $query = "UPDATE users SET new='$new WHERE ID='$updateid;'"; should be this $query = "UPDATE users SET new='".$new."' WHERE ID='".$updateid."'"; Thanks but it didn't work
June 25, 201016 yr Thanks but it didn't work Well that is the right way to do it but u might have other code wrong in ur script thats why its not updating display all ur code
June 25, 201016 yr should be this $query = "UPDATE users SET new='".$new."' WHERE ID='".$updateid."'"; What he did originally was valid from a php perspective, You should read see String Variable parsing to see about including variables in strings. Also i'm not sure if you're just not using any escaping on this query because you know the variable is safe or something, or whether you're writing all your SQL queries like this, but really its best practice to escape/paramaterise *all* your sql queries for security reasons. On a basic level you can escape all variables before adding them to your sql using mysql_real_escape_string. More advanced projects however use either the MySQLi advanced mysql interface or PDO generic data abstraction layer which both contain the ability to paramaterise your SQL queries so nothing unexpected ever gets in.
June 25, 201016 yr The ID field is presumably an INT in which cases the WHERE clause should read $query = "UPDATE users SET new='$new' WHERE ID=$updateid"; and not $query = "UPDATE users SET new='$new' WHERE ID='$updateid'"; Steve
June 25, 201016 yr Author Thanks guys but: webdesigner93 - That is all my code from this script. pat24 - That's what my code already is. I changed it yesterday as somehow I copied and pasted wrong :S. James - thanks, I'll have a look at escape strings. Sorry rafflemine - that didn't work :/ I can't understand why it isn't working. Plus I don't really need a variable for $new as it should always be updated to 0 however I tried using a variable to see if I could get it to work.
June 25, 201016 yr Try to echo $query and see if the id is showing. If it isn't you may have a problem with $_SESSION['ID'].
June 25, 201016 yr Thanks guys but: webdesigner93 - That is all my code from this script. pat24 - That's what my code already is. I changed it yesterday as somehow I copied and pasted wrong :S. James - thanks, I'll have a look at escape strings. Sorry rafflemine - that didn't work :/ I can't understand why it isn't working. Plus I don't really need a variable for $new as it should always be updated to 0 however I tried using a variable to see if I could get it to work. Hey dude i just noticed this u dont even have a session_start on the page u cant start a session without it and u did not even set the session to equal anything like this $_SESSION['ID'] = something;
June 25, 201016 yr Author Thanks guys! I've looked through this script loads but not noticed that! I changed it to my ID cookie but changed the query aswell and the second time spelt my cookie wrong. Will use the cookie from now on as it worked. Thanks alot!
Create an account or sign in to comment