about new id after inserting

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

about new id after inserting

Post by hrubos »

I have a table, which are id_reservation, day_moveInto, day_move, id_student,number_room.

page 1 : user chose room --> save to database number_room, so indatabase now there are id_reservation and number_room

page 2 : complete userself informatoin --> I have problem here, information will be saved into databse in new row, and have new id_reservation.But I want save it into id_reservation when user chose room.

my code here
page1 : for chose room

Code: Select all

$id_numberRoom = $_POST['id_numberRoom'];

if(!get_magic_quotes_gpc())
{
	$id_numberRoom = addslashes($id_numberRoom);
}

@ $db = mysql_connect("localhost", "root","") or die("Error");
mysql_select_db("",$db) or die("Can't choose database");
//user book room
$query = "insert into reservation " . "(id_numberRoom) values"
. "('$id_numberRoom')";
mysql_query($query) or die(mysql_error());
//volny pokoj -> obsazeno in database
$query_edit_stateRoom = "UPDATE pokoj p,reservation r SET p.state_room = 'obsazeno'
               
                     WHERE p.id_numberRoom = r.id_numberRoom";
mysql_query($query_edit_stateRoom) or die(mysql_error());

 ?>
page 2 for complete information

Code: Select all

$day_moveInto = $_POST['day_moveInto'];
$day_move = $_POST['day_move'];

if(!$day_moveInto||!$day_move){
	echo 'Třeba vyplnit upnle informace !!!';
	exit;
}
if(!get_magic_quotes_gpc())
{
	$day_moveInto  = addslashes($day_moveInto);
	$day_move  = addslashes($day_move);

}
@ $db = mysql_connect("localhost", "root","") or die("No way");
mysql_select_db("",$db) or die("Nelze database");
$query = "insert into reservation " . "(day_moveInto,day_move) values"
. "('$day_moveInto','$day_move')";


mysql_query($query) or die(mysql_error());
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$query = "insert into reservation " . "(day_moveInto,day_move) values"
. "('$day_moveInto','$day_move')";

mysql_query($query) or die(mysql_error()); 

echo 'The last id inserted was '. mysql_insert_id();
Is this what you wanted?
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

No, my problem that when use go to page 2, and complete information, information can't be saved in row users's id, it is saved in new row.

Example : I chose number of room 2 then click into radioubutton a submit --> indatabase there is id_reservation = 1, number_room =2, day_moveInto, day_move = null;

nexpage i have to complete information about my self then submit -> in database there is id_resrevation = 2,number room = null, day_moveInto = blala, day_move =blabla....

But I don't want it, I would like day_moveInto and day_move will be saved in row with id_reservation =1
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

nobody can help me???
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If I understand your requirement, for multiple page forms (wizards) I normally save the information in a SESSION variable. When the wizard is complete you then have all the information you need and can insert into the database. The only problem is when to reset the data. Obviously when the information is saved the information can be cleared, but also at the start. Wizard page 1 - are you editing the first page (user pressed back button etc), or creating a new thing.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

CoderGoblin wrote:If I understand your requirement, for multiple page forms (wizards) I normally save the information in a SESSION variable. When the wizard is complete you then have all the information you need and can insert into the database. The only problem is when to reset the data. Obviously when the information is saved the information can be cleared, but also at the start. Wizard page 1 - are you editing the first page (user pressed back button etc), or creating a new thing.
Would you show me in code ??? howto save information ina session then to do inserting.

Thanks for help
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

page1.php

Code: Select all

<?php
  session_start();
  // Set default values
  $base_url="www.mysite.com/";

  if (!isset($_SESSION['form_vals'])) $_SESSION['form_vals']=array();
  if (!empty($_POST['my_submit']) {
    $ok=1;
    // cheat for demonstration purposes. Always validate entries.... Set OK to 0 if error
    foreach ($_POST as $key=>$value) {
       $_SESSION['form_vals'][$key]=$value;  
    }
    if ($ok) {
        $_SESSION['form_vals']['page_1_ok']=1;
        header('Location:'.$base_url.'page2.php');
        exit;
    }
  }
?>
// Output form 1 (contains x,y for example)
page2.php

Code: Select all

<?php
  session_start();
  $base_url="www.mysite.com/";

  // Check if first page ok
  if (empty($_SESSION['form_vals']['page_1_ok'])) {
        header('Location:'.$base_url.'page1.php');
        exit;
  }
  
  // process this second page form values
  if (!empty($_POST['my_submit']) {
    $ok=1;
    // cheat for demonstration purposes. Always validate entries.... Set OK to 0 if error
    foreach ($_POST as $key=>$value) {
       $_SESSION['form_vals'][$key]=$value;  
    }
    if ($ok) {
     // we save the information
      $sql="INSERT INTO mytable (x,y,z) VALUES ({$_SESSION['form_vals']['x']},{$_SESSION['form_vals']['y']},{$_SESSION['form_vals']['z']})";
      /* 
       * execute the sql;
       */

      // clear the session variable.
      unset($_SESSION['form_vals']);
    }
?>
// Output form2 (contains z for example).
OK not tested, not neat but should give you the idea.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hrubos, you waited all of nine hours before bumping this thread. I strongly suggest you not break that rule again.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not receive a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

So you have other idea, because really I don't understand your code
Post Reply