Jump to content


Photo

passing the id of inserted data to next page


  • Please log in to reply
3 replies to this topic

#1 khan kaka

khan kaka
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts
  • Locationaustralia

Posted 21 April 2004 - 01:47 PM

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:
khan kaka

#2 Shaun

Shaun
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationMidlands

Posted 23 April 2004 - 06:02 PM

try using variables. like
<?php
$data = 'Hello World';

echo ($data);
?>


#3 khan kaka

khan kaka
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts
  • Locationaustralia

Posted 08 May 2004 - 07:03 AM

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


its driving me crazy.

cheers
khan kaka

#4 yesm

yesm
  • Members
  • Pip
  • Newbie
  • 1 posts

Posted 11 May 2004 - 10:38 PM

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 :)
_________________________________________________[br]PHP system developer, SUBSTANCE AS, Oslo, Norway




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users