Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

MySQL Update script not working

Featured Replies

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

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."'";


  • 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 :(

$query = "UPDATE users SET new='$new' WHERE ID='$updateid'";

 

Try that

 

Pat

 

 

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.

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

  • 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.

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;

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.