0

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 :(

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by muralikalpana
0

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

0

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.

Edited by muralikalpana: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.