how to generate auto number in php?

here's my code in saving a data

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("reservation", $con);

[B]$id=mysql_query("Select * from guest order by code desc");
if ($id == NULL)
{
$id = 1;
}
else
{
$number=$id + 1;
}[/B]
$sql="INSERT INTO guest (code, fname, mname, lname, address, emailadd, contactno)
VALUES ('$number','".$_POST["FirstName"]."','".$_POST["Mname"]."','".$_POST["LastName"]."','".$_POST["Country"]."','".$_POST["Email"]."','".$_POST["Phone"]."')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con)
?>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("reservation", $con);

$sql1="Insert into rent (checkin, checkout, noofrooms, roomtype) VALUES ('".$_POST[ADate]."','".$_POST[AnotherDate]."','".$_POST[Rooms]."','".$_POST[RoomType]."')";


if (!mysql_query($sql1,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con)
?>

help please :(

Recommended Answers

All 2 Replies

in the database table set one field as primary key and set extra(this is coloumn in table ) field as auto_increment.

hey set code as a primary key and set auto-increment. there is no need to insert from here. just left that column when you write the query insert.

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.