Jump to content

Autoincrement - how do I find the latest added record and update it?


steff_dk

Recommended Posts

The below code adds a new user to my website. The table holding the members has a primary key named 'ID' that is set to autoincrement.

How can I modify the script to update the newly added user (the one with the highest ID) and set the field md5user to the value md5(ID)?

I know I have to do something like 'UPDATE members set md5user=MD5('$ID') WHERE ID=$ID' but how do I get the $ID for the newest user?
I have tried 'SELECT ID from members ORDER BY ID DESC LIMIT 1' but it doesn't seem to work (unless I'm doing it wrong ...)  ???

Any help will be highly appreciated.

[code]<?PHP

//start the session
session_start();

//check to make sure the session variable is registered
if(session_is_registered('bestyrelse')){

//the session variable is registered, the user is allowed to see anything that follows

//posted variables
$titel = $_POST['titel'];
$fornavn = $_POST['fornavn'];
$efternavn = $_POST['efternavn'];
$adresse = $_POST['adresse'];
$postnr = $_POST['postnr'];
$postby = $_POST['postby'];
$mailadresse = $_POST['mailadresse'];
$enhed = $_POST['enhed'];
$cpr = $_POST['cpr'];
$bestyrelse = $_POST['bestyrelse'];
$senior = $_POST['senior'];
$noter = $_POST['noter'];

//set the database connection variables

$dbHost = "localhost";
$dbUser = "thedomain_com";
$dbPass = "thepassword";
$dbDatabase = "thedomain_com";

//connect to the database

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

mysql_query("INSERT INTO members(titel, fornavn, efternavn, adresse, postnr, postby, mailadresse, enhed, cpr, bestyrelse, senior, noter, md5pass) VALUES('$titel', '$fornavn', '$efternavn', '$adresse', '$postnr', '$postby', '$mailadresse', '$enhed', '$cpr', '$bestyrelse', '$senior', '$noter', md5('mentor'))", $db);


//View the newly added user page
header( "Location: viewnewuser.php" );

}
else{

//the session variable isn't registered, send them back to the login page
header( "Location: source.php?textID=1" );
}

?>[/code]
Link to comment
Share on other sites

try this

[code]<?PHP

//start the session
session_start();

//check to make sure the session variable is registered
if(session_is_registered('bestyrelse')){

//the session variable is registered, the user is allowed to see anything that follows

//posted variables
$titel = $_POST['titel'];
$fornavn = $_POST['fornavn'];
$efternavn = $_POST['efternavn'];
$adresse = $_POST['adresse'];
$postnr = $_POST['postnr'];
$postby = $_POST['postby'];
$mailadresse = $_POST['mailadresse'];
$enhed = $_POST['enhed'];
$cpr = $_POST['cpr'];
$bestyrelse = $_POST['bestyrelse'];
$senior = $_POST['senior'];
$noter = $_POST['noter'];

//set the database connection variables

$dbHost = "localhost";
$dbUser = "thedomain_com";
$dbPass = "thepassword";
$dbDatabase = "thedomain_com";

//connect to the database

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

mysql_query("INSERT INTO members(titel, fornavn, efternavn, adresse, postnr, postby, mailadresse, enhed, cpr, bestyrelse, senior, noter, md5pass) VALUES('$titel', '$fornavn', '$efternavn', '$adresse', '$postnr', '$postby', '$mailadresse', '$enhed', '$cpr', '$bestyrelse', '$senior', '$noter', md5('mentor'))", $db);

$ID=mysql_insert_id();
mysql_query("UPDATE members set md5user=MD5('$ID') WHERE ID='$ID'") or die(mysql_error());

//View the newly added user page
header( "Location: viewnewuser.php" );

}
else{

//the session variable isn't registered, send them back to the login page
header( "Location: source.php?textID=1" );
}

?>[/code]

:)

regards
Liam
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.