Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more

Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Web Programming > PHP Development
User Name
Password

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-18-2006, 10:17 PM   #1 (permalink)
dosser
Junior Member
 
Join Date: May 2006
Posts: 2
Default form handling problem

Hi guys sure you can help me with this one, sorry for the long post just want to give as much info as poss.

I have database set up in mysql and an html form set up to enter data into it. works fine and dandy. however I now have to go one step further and got stuck.

The database is called food. Four tables are:

recipe - recipe_id (primary key, auto-incr), and recipe_name
chef - chef_id (pr.key, auto-incr) and chef_name
restaurant - restaurant_id (pr.key, auto-incr) and restaurant_name
meal - meal_id (pr.key, auto-incr), meal_info, recipe_id, creator_id, restaurant_id.

Meal=the reason you were at the restaurant (eg Son's Birthday, Marriage etc)

The code for the html form is as follows:


<html><body>

<form action="process.php" method="post">
Recipe: <input type=text name="recipe" size=40><br/><br/>
Chef: <input type="text" name="chef1" size=40><br/><br/>
Restaurant: <input type="text" name="restaurant1" size=40><br/><br/>
Meal: <input type=text name="meal1" size=140><br /><br/>

<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
<input type=reset name"reset" value="reset">

</form>
</body></html>

This then goes to a to the process.php as follows.

<?php

$conn=mysql_connect("localhost","******","******") ;
if (!$conn)
{die('Could not connect to mysql');}

$db= mysql_select_db("food",$conn);
$recipe_name=mysql_query("insert into recipe values('null','$_POST[recipe]')");
$recipe_id=mysql_insert_id();
$chef_name=mysql_query("insert into chef values('null','$_POST[chef1]')");
$chef_id=mysql_insert_id();
$restaurant_name=mysql_query("insert into restaurant values('null','$_POST[restaurant1]','null','null')");
$restaurant_id=mysql_insert_id();
$meal_info=mysql_query("insert into meal values('null',$recipe_id,$chef_id,'$_POST[meal1]',$restaurant_id)");

if (!$recipe)
{die('Error with recipe');} echo"Recipe Success!";
if (!$chef1)
{die('Error with artist');} echo"Chef Success!";
if (!$restaurant1)
{die('Error with restaurant');} echo"Restaurant Success!";
if (!$ingredient1)
{die('Error with meal');} echo"Meal Success!";


?>

Obviously this is all just to test it was working so not worried about the output just yet. However, I now realise that this will only work for one set of chef/restaurant/meal - hence why I put eg 'chef1' rather than chef..

So the problem is that this works fine for one set of data, but I want to have the html form set out to accept more than one chef/restaurant/meal eg:

<form action="process.php" method="post">
Recipe: <input type=text name="recipe" size=40><br/><br/>
Creator: <input type="text" name="creator1" size=40><br/><br/>
Restaurant: <input type="text" name="restaurant1" size=40><br/><br/>
Meal: <input type=text name="meal1" size=140><br /><br/>

Creator: <input type="text" name="creator2" size=40><br/><br/>
Restaurant: <input type="text" name="restaurant2" size=40><br/><br/>
Meal: <input type=text name="meal2" size=140><br /><br/>

Creator: <input type="text" name="creator2" size=40><br/><br/>
Restaurant: <input type="text" name="restaurant2" size=40><br/><br/>
Meal: <input type=text name="meal2" size=140><br /><br/>

Creator: <input type="text" name="creator2" size=40><br/><br/>
Restaurant: <input type="text" name="restaurant2" size=40><br/><br/>
Meal: <input type=text name="meal2" size=140><br /><br/>

<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
<input type=reset name"reset" value="reset">

</form>

The idea is, you could have eaten eg lasagna at many different restaurants by different chefs at different times, or the same chef at different restaurants etc etc. The recipe will be the same for all the

So what I think I need is something like a PHP loop which encorporates the mysql INSERT command I have in process.php. But how do I then name the parts of the form? eg 'chef2', 'restaurant2', 'meal2'?

Do i use while... or for ($i++) and if so what how does that fit into the rest of the process.php?

Any ideas ?

Thanks in advance

D
dosser is offline   Reply With Quote
Sponsored Links
Old 05-22-2006, 03:22 PM   #2 (permalink)
etono
Moderator
 
Join Date: May 2006
Posts: 86
Default Re: form handling problem

I would name the inputs like this:
Code:
Creator: <input type="text" name="chefs[]" size=40><br/><br/> Restaurant: <input type="text" name="restaurants[]" size=40><br/><br/> Meal: <input type=text name="meals[]" size=140><br /><br/>

Then I would insert each element of the array on your process.php page like this:
Code:
<?php foreach( $_POST['chefs'] as $chef ) { // add chef to chefs database mysql_query( "INSERT INTO chefs ( name ) VALUES ( '$chef' )" ); } //same idea for restaurants and meals ?>

You should also be doing some validation of your form data before inserting it into your database to avoid sql injection. Also you should make sure to escape your strings before inserting it.
__________________
Jeremy Moseley
My Links: Design Related Blog | Personal Website | Portfolio
etono is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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

Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00



» Sponsors

» Links

» Affiliates
Web Hosting
Marketing Find
Merchant Select
SiteMap Builder
Host Compare
Dedicated Servers

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 02:52 AM.



LinkBacks Enabled by vBSEO 3.0.0 RC8
Webmaster Forums