Jump to content

Archived

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

klinmy

auto_increment id

Recommended Posts

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,

[code]$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\""?>>[/code]

Form B

[code]<?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();[/code]


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

Share this post


Link to post
Share on other sites
FORM A:

[code]<?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; ?>">[/code]

FORM B

[code]<?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();
}[/code]

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.

Share this post


Link to post
Share on other sites
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'..

Share this post


Link to post
Share on other sites
it's the code i tried on form A

i now change the next button to save.

[code]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";
}[/code]
the message showed the correct id as in database, no problem it seems.

but when i bring the info to Form B,

[code]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;
    
}[/code]

and placing this in Form B,

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

it came out like this,

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

)

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

thanks alot!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
it's now working fine after changing to ths..

[code]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;
    
}[/code]

thanks guys

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.