Jump to content

passing the id of inserted data to next page


Recommended Posts

  • 2 weeks later...

You can access the id of data inserted to a MySQL table, by storing the value of mysql_insert_id() to a session var. Make sure the table has a AUTO_INCREMENT option assigned to it (in my case 'member_id). This way you can pass the value on to the next page. Here is a sample script:

 

<?php

 

// Start a session and connect to MySQL

session_start();

db_connect();

 

 

// Make query

$query = "INSERT INTO members ( username, password ) ";

."VALUES ( '$u', '" . md5( $p . "' )";

 

// Run query

$result = mysql_query( $query );

 

if ( $result ) { // If the query was succesful

// Store the row id in session var

$_SESSION['member_id'] = mysql_insert_id();

$rows_aff = mysql_affected_rows();

echo "$rows_aff row affected.";

} else {

echo "0 rows affected.";

}

 

?>

 

Script to print the value on the next page:

 

<?php

 

// Start the session to access its values

session_start();

 

// Print value

echo $_SESSION['member_id'];

 

?>

 

I hope this solve your problems :)

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.