Current location: Hot Scripts Forums » Programming Languages » PHP » Using forms with auto increase column


Using forms with auto increase column

Reply
  #1 (permalink)  
Old 10-11-03, 04:09 PM
perleo perleo is offline
Coding Addict
 
Join Date: Jul 2003
Location: Ireland
Posts: 269
Thanks: 0
Thanked 0 Times in 0 Posts
Using forms with auto increase column

Hi, I have this script to insert into a db, but in the db i have a id column m which is auto_increasement. But when i submit to the db notting happenes but if I drop the id column it works.

What do I have to do to make the id column work without having to create an input in the form

----------- FORM PAGE CONTENT ---------------------
<form method="POST" action="book.php">
<p> <input type="text" name="departdate" size="14" value="Depart Date"><a target="_blank" href="calandar.htm"><img border="0" src="images/calendar.gif" width="15" height="15"></a></p>
<p> <input type="text" name="returndate" size="14" value="Return Date"><a target="_blank" href="calandar.htm"><img border="0" src="images/calendar.gif" width="15" height="15"></a></p>
<p>&nbsp; <select size="1" name="depart">
<option>-Depart Airport-</option>
<option value="Dublin">Dublin</option>
<option value="Cork">Cork</option>
<option value="Shannon">Shannon</option>
<option value="Los Angeles">Los Angeles</option>
<option value="Chicago">Chicago</option>
<option value="Boston">Boston</option>
<option value="Paris">Paris</option>
<option value="Milan">Milan</option>
<option value="Rome">Rome</option>
<option value="London">London</option>
<option value="Birmingham">Birmingham</option>
<option value="Glasgow">Glasgow</option>
<option value="Manchester">Manchester</option>
<option value="Belfast">Belfast</option>
<option value="New York">New York</option>

</select></p>
<p>&nbsp; <select size="1" name="arrive">
<option>-Arrival Airport-</option>
<option value="Dublin">Dublin</option>
<option value="Cork">Cork</option>
<option value="Shannon">Shannon</option>
<option value="Los Angeles">Los Angeles</option>
<option value="Chicago">Chicago</option>
<option value="Boston">Boston</option>
<option value="Paris">Paris</option>
<option value="Milan">Milan</option>
<option value="Rome">Rome</option>
<option value="London">London</option>
<option value="Birmingham">Birmingham</option>
<option value="Glasgow">Glasgow</option>
<option value="Manchester">Manchester</option>
<option value="Belfast">Belfast</option>
<option value="New York">New York</option>
</select></p>
<p>&nbsp; <input type="text" name="name" size="14" value="Your Name"></p>
<p align="center"><input type="submit" value="Book" name="submit"></p>
</form>

------------BOOK.PHP PAGE CONTENT --------------
<?php

include('dbconnect.php');
$depart = $HTTP_POST_VARS['depart'];
$arrive = $HTTP_POST_VARS['arrive'];
$departdate = $HTTP_POST_VARS['departdate'];
$returndate = $HTTP_POST_VARS['returndate'];
$name = $HTTP_POST_VARS['name'];


if ( !$departdate || !$returndate || !$depart || !$arrive || !$name)
{
echo 'You have not input all the required info.<br />'
.'Please go back and enter all the info.';
exit;
}


$departdate = addslashes($departdate);
$returndate = addslashes($returndate);
$depart = addslashes($depart);
$arrive= addslashes($arrive);
$name = addslashes($name);

$conn = db_connect();

$query ="insert into vbooking values
('" .$departdate."', '".$returndate."', '".$arrive."', '".$depart."', '".$name."')";

$result = mysql_query($query);
if ($result)
echo mysql_affected_rows(). ' Aircraft inserted into database.';



?>
Reply With Quote
  #2 (permalink)  
Old 10-11-03, 04:20 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Your insert query...

$query ="insert into vbooking values
('" .$departdate."', '".$returndate."', '".$arrive."', '".$depart."', '".$name."')";

Try changing to...

$query ="insert into vbooking values
('','$departdate','$returndate','$arrive','$depart ','$name')";

Use single quotes around the info you're inserting. The first field, id, you only use 2 single quotes, not a double quotation mark. See if that helps, and keep your id field auto-incremented.
Reply With Quote
  #3 (permalink)  
Old 10-11-03, 04:27 PM
perleo perleo is offline
Coding Addict
 
Join Date: Jul 2003
Location: Ireland
Posts: 269
Thanks: 0
Thanked 0 Times in 0 Posts
That worked perfect thanks,

but can you explain how one line and a few quotations allowed the id to self increase ????
Reply With Quote
  #4 (permalink)  
Old 10-11-03, 04:37 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
The id field, when set to auto-increment, increases automatically. Thats why you dont add a value for the id when you insert your data. '' marks basically tell the id field to increment on its own even though the value is empty.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 06:38 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.