Jump to content


Photo

auto_increment id


  • Please log in to reply
6 replies to this topic

#1 klinmy

klinmy
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 14 March 2006 - 07:57 AM

when i fill up form A, the information will be displayed in the next page (form B) when i click the "next" button.

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]example data that i entered in Form A in the 1st time,
id = (auto_increment)
name=john
sex = male

example data which is displayed in Form B in the 1st time,
id=0 (supposely 221)
name=john
sex = male[!--colorc--][/span][!--/colorc--]

[!--coloro:#993399--][span style=\"color:#993399\"][!--/coloro--]example data that i entered in Form A in the 2nd time,
id = (auto_increment)
name=selina
sex = female

example data which is displayed in Form B in the 2nd time,
id=221 (supposely 222)
name=selina
sex = female[!--colorc--][/span][!--/colorc--]


the problem is when the first time i fill up the form, the auto_increment id will not be displayed in form B, it will only be displayed with the first id when i fill up for the second time.

below is parts of my script in form A,

$db=mysql_connect("localhost","ky","pw");
if(!$db)
{
    die("Failed to open connection to MySQL server.");
}

mysql_select_db("onlineleave");
check_mysql();

if(!isset($id))
{
    $id=0;
}

elseif(isset($next))
{
        
    if($errors==0)
    {        

    
    check_mysql();
    $id=mysql_insert_id();
    
    header("Location: applyBa.php?name=$_POST[name]&id=$_POST[id]&sex=$_POST[sex]"); exit;
    
    }    
}

<input type="hidden" name="id" <?php echo "VALUE=\"$id\""?>>

Form B

<?php echo $_GET[name] ?> is <?php echo $_GET[sex] ?> with ID <?php echo $_GET[id] ?>. 

if(isset($save))
{
    
    $query="INSERT INTO tableB " . "( name, sex) VALUES ( '$name, '$sex)";
    $result=mysql_query($query);
    check_mysql();
    $id=mysql_insert_id();


thanks
the id value in the database is correct, just the display in form B has problem.

#2 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 14 March 2006 - 08:40 AM

FORM A:

<?php

$db=mysql_connect("localhost","ky","pw") or die("MySQL Error: ".mysql_error());
mysql_select_db("onlineleave") or die("MySQL Error: ".mysql_error());

check_mysql();

if(!isset($id))    $id=0;

if((isset($next)) && ($errors==0)) {
  check_mysql();
  $id=mysql_insert_id();
  header("Location: applyBa.php?name=".$_POST['name']."&id=".$_POST['id']."&sex=".$_POST['sex']);
  exit;
}

?>
<input type="hidden" name="id" value="<?php echo $id; ?>">

FORM B

<?php

echo $_GET['name']." is ".$_GET['sex']." with ID ".$_GET['id'].".";

?>

if(isset($save)) {
  $query="INSERT INTO tableB (name, sex) VALUES ('".$_GET['name']."', '".$_GET['sex']."')";
  $result=mysql_query($query);
  check_mysql();
  $id=mysql_insert_id();
}

This is just what I would change from what I can see. Don't assume register globals is on, because it probably isn't and definitely shouldn't. Additionally if you already have a row in the table with the information and you are making changes to it use UPDATE sql query and condition WHERE id=".$_GET['id'].".

I hope this is helpful.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#3 klinmy

klinmy
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 14 March 2006 - 08:54 AM

thanks txmedic03, but all the other values (name and sex) are working fine except for id only.

i tried to close my Form B. by running only Form A, nomatter how many times i enter data, the id still remains '0'..



#4 klinmy

klinmy
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 14 March 2006 - 01:53 PM

it's the code i tried on form A

i now change the next button to save.

mysql_select_db("onlineleave");
check_mysql();

if(!isset($id))
    $id=0;


if(isset($save)){    
    
    $query="INSERT INTO apply " . "(name, sex) VALUES ( '$name', '$sex')";
    $result=mysql_query($query);
    check_mysql();
    $id=mysql_insert_id();
                echo "id = $id";
}
the message showed the correct id as in database, no problem it seems.

but when i bring the info to Form B,

if(isset($save))
{        
    
    
    $query="INSERT INTO apply " . "(  name, sex) VALUES ( '$name', '$sex')";
    $result=mysql_query($query);
    check_mysql();
    $id=mysql_insert_id();
        
    header("Location: FormB.php?id=$_POST[id]&name=$_POST[name]&sex=$_POST[sex]");exit;
    
}


and placing this in Form B,

<?php echo '<pre>'.print_r($_GET,true).'</pre>'; ?>

it came out like this,

Array
(
[name] => john
[id] => 0
[sex] => male

)

why is it cant get the value id (auto_increment)?

thanks alot!

#5 shocker-z

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

Posted 14 March 2006 - 02:03 PM

In form A you set a POST and a GET for ID which are you after? on form B if your after $ids from form A then you need $_POST['id'] but if your after $_POST[id] which has been sent in as id=$_POST[id] in the URL then you are using the correct one.. so which are u wantiong to pull into form B?
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#6 klinmy

klinmy
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 14 March 2006 - 02:18 PM

there isn't any GET for id in Form A. or is there??

i'm trying to get the value of id to Form B. i've tried using both $_POST[id] and $_POST['id']. but it just made no different..name and sex can stil get the values as entered in form A

#7 klinmy

klinmy
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 14 March 2006 - 02:39 PM

it's now working fine after changing to ths..

if(isset($save))
{        
    
    
    $query="INSERT INTO apply " . "(  name, sex) VALUES ( '$name', '$sex')";
    $result=mysql_query($query);
    check_mysql();
    $id=mysql_insert_id();
        
    header("Location: FormB.php?id=$id&name=$_POST[name]&sex=$_POST[sex]");exit;
    
}

thanks guys




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users