Jump to content

Archived

This topic is now archived and is closed to further replies.

steff_dk

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

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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.