Page 1 of 1

what's the id when insert to mysql?

Posted: Mon Jun 30, 2008 3:41 am
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

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

Posted: Mon Jun 30, 2008 4:05 am
by Glazz
for example:

Code: Select all

<?php
$query = mysql_query("INSERT INTO ......");
$id = mysql_insert_id();
?>

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

Posted: Mon Jun 30, 2008 6:25 pm
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.

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

Posted: Sat Jul 12, 2008 4:49 am
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

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

Posted: Sat Jul 12, 2008 6:16 am
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