Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 steff_dk

steff_dk
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationSilkeborg, Denmark

Posted 29 October 2006 - 07:51 PM

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.

<?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" );
}

?>


#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 29 October 2006 - 07:59 PM

try this

<?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" );
}

?>

:)

regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 steff_dk

steff_dk
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationSilkeborg, Denmark

Posted 29 October 2006 - 08:10 PM

Cool  :D

It works - Cheers!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users