I want to retrieve the latest autoincremented id of the record which i have inserted, so that i can use it update query.
I used last_insert_id() but it seems to be not working.Please help as i m still learning PHP

here's the code..

$insert = "INSERT INTO `farmlogin` (`uid`, `name`, `gender`, `date`, `age`, `bb`, `htno`, `htunit`, `lno`, `lunit`, `pric`, `clr`, `insured`,
 `inscomp`, `polname`, `premiumtype`, `premiumamt`, `insstdate`, `matdate`, `remk`) VALUES ('$d', '$n', '$gen', '$dt', '$umar', '$bo', '$hgt', '$unitht', '$lgt', '$unitl', '$pr', '$colo', '$check', '$cname', '$poly', '$premtype', '$amt', '$insurancedat', '$mat', '$rk')";
$result = mysql_query($insert);


$id = intval($_GET['id']);
$sql = "UPDATE `farmlogin` SET `doccity`='$id' WHERE `cowid`='last_insert_id($result)'";
echo "$sql";
$data = mysql_query($sql);


    if($result)
    {
        echo "<script>location.href='http://localhost/assigndoc.php'</script>";
    } 
    else
    {
        echo "sami";
    }

Recommended Answers

All 2 Replies

select id from your table, order by id in descending order and give it a limit of 1.

$sql = "UPDATE `farmlogin` SET `doccity`='$id' WHERE `cowid`=last_insert_id()";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.