what's the id when insert to mysql?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Blondy
Forum Commoner
Posts: 32
Joined: Thu Mar 06, 2008 5:55 pm

what's the id when insert to mysql?

Post by Blondy »

hi guys I want to echo the id (auto incermit) of mysql table when I insert data into it
can anyOne help
(I mean when I insert into db it automatocally gives the table an id i want it )
thanks sarah
Glazz
Forum Newbie
Posts: 2
Joined: Sun Jun 22, 2008 4:27 am

Re: what's the id when insert to mysql?

Post by Glazz »

for example:

Code: Select all

<?php
$query = mysql_query("INSERT INTO ......");
$id = mysql_insert_id();
?>
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: what's the id when insert to mysql?

Post by Bill H »

Note that you have to call this right after the insert, since it returns the last result regardless of database selected or table inserted into. You should issue the INSERT query and immediately call the mysql_insert_id() function.
Blondy
Forum Commoner
Posts: 32
Joined: Thu Mar 06, 2008 5:55 pm

Re: what's the id when insert to mysql?

Post by Blondy »

hi guys thanks but don't qorks for me
here's the code plz help

Code: Select all

function add_ticket($description,$affected,$scope,$problemstart,$problemend,$status,$severity,$owner,$id)
{
    //strip values from unwanted characters
    $description    = strip_html($description);
    $affected       = str_replace('>',')',str_replace('<','(',$affected));
    $affected       = strip_html($affected);
    $scope          = strip_html($scope);
 
    $query  = "INSERT INTO $GLOBALS[mysql_prefix]ticket (affected,scope,owner,description,problemstart,problemend,status,date,severity) VALUES('$affected','$scope','$owner','$description','$problemstart','$problemend',$status,NOW(),'$severity')";
    $id = mysql_insert_id();
    echo "no. ".$id."";
    $result = mysql_query($query) or do_error("add_ticket($description, $affected,$scope,$problemstart,$problemend,$status,$date,$severity,$owner,$id)::mysql_query()", 'mysql query failed', mysql_error());
    //report_action($GLOBALS[ACTION_OPEN],0,0,$_POST[$frm_owner]);
}
////
add_ticket($_POST['frm_description'],$_POST['frm_affected'],$_POST['frm_scope'],$problemstart,$problemend,$GLOBALS['STATUS_OPEN'],$_POST['frm_severity'],$_POST['frm_owner'],$id);
 
thanks guys
Glazz
Forum Newbie
Posts: 2
Joined: Sun Jun 22, 2008 4:27 am

Re: what's the id when insert to mysql?

Post by Glazz »

Code: Select all

function add_ticket($description,$affected,$scope,$problemstart,$problemend,$status,$severity,$owner,$id)
{
    //strip values from unwanted characters
    $description    = strip_html($description);
    $affected       = str_replace('>',')',str_replace('<','(',$affected));
    $affected       = strip_html($affected);
    $scope          = strip_html($scope);
 
    $query  = "INSERT INTO $GLOBALS[mysql_prefix]ticket (affected,scope,owner,description,problemstart,problemend,status,date,severity) VALUES('$affected','$scope','$owner','$description','$problemstart','$problemend',$status,NOW(),'$severity')";
    $result = mysql_query($query) or do_error("add_ticket($description, $affected,$scope,$problemstart,$problemend,$status,$date,$severity,$owner,$id)::mysql_query()", 'mysql query failed', mysql_error());
    $id = mysql_insert_id();
    echo "no. ".$id."";
    //report_action($GLOBALS[ACTION_OPEN],0,0,$_POST[$frm_owner]);
}
////
add_ticket($_POST['frm_description'],$_POST['frm_affected'],$_POST['frm_scope'],$problemstart,$problemend,$GLOBALS['STATUS_OPEN'],$_POST['frm_severity'],$_POST['frm_owner'],$id)
you need to use the mysql_insert_id function after the mysql_query function
Post Reply