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
what's the id when insert to mysql?
Moderator: General Moderators
Re: what's the id when insert to mysql?
for example:
Code: Select all
<?php
$query = mysql_query("INSERT INTO ......");
$id = mysql_insert_id();
?>- 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?
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?
hi guys thanks but don't qorks for me
here's the code plz help
thanks guys
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);
Re: what's the id when insert to mysql?
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)