Jump to content

Archived

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

khan kaka

passing the id of inserted data to next page

Recommended Posts

how can i pass the id of inserted data to next page?

 

i want to be able to display the record i have inserted in to a table.

 

i am using php and mysql. :unsure:

Share this post


Link to post
Share on other sites

try using variables. like

<?php

$data = 'Hello World';

 

echo ($data);

?>

Share this post


Link to post
Share on other sites

thanks for your feed back but can you plz explaing a bit more.

 

 

its driving me crazy.

 

cheers

Share this post


Link to post
Share on other sites

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 :)

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.