Jump to content

Multiple Forms Data Submit Into Mysql With One Button


whyz3rman

Recommended Posts

Hi Guys. I have a simple question. I want to insert player data into mysql via html forms. It works If I have form for only one player:html part:

<form action="insert.php" method="post">Name: <input type="text" name="name" />Surname: <input type="text" name="surname" />Born: <input type="text" name="born" />Club: <input type="text" name="club" />Email: <input type="text" name="email" /><input type="submit" />

php part:

<?php$con = mysql_connect("localhost","good","password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("league", $con);$sql="INSERT INTO player_data (name, surname, born, club, email)VALUES('$_POST[name]','$_POST[surname]','$_POST[born]','$_POST[club]','$_POST[email]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 record added";mysql_close($con)?> 

Is there a simple way if I want to add multiple players (let's say 5) at the same time:5 duplicate forms one under another and one submit button at the end for all of them. I read that the easiest way is javascript and other ways are ASP and Ajax. Any help or directions would be much appreciated.Thanks.

Link to comment
Share on other sites

Thanks for your replies, but I am lost here and as you can see a newbie. I tried to combine two forms into one (for 2 players):

<html><body><form action="insert.php" method="post">Name0: <input type="text" name="ime" />Surname0: <input type="text" name="priimek" />Born0: <input type="text" name="rojen" />Club0: <input type="text" name="klub" />Mail0: <input type="text" name="mail" /></br>Name1: <input type="text" name="ime" />Surname1: <input type="text" name="priimek" />Born1: <input type="text" name="rojen" />Club1: <input type="text" name="klub" />Mail1: <input type="text" name="mail" /><input type="submit" /></form></body></html>

I tried several things, but all I managed was to successfully insert the last entry, or two entries with no data if I played with PHP part. I don't have a clue how to change insert.php to accept both entries.

Link to comment
Share on other sites

You didn't give the fields unique names. You have 2 fields called "ime", 2 fields called "priimek", etc. So only the last field with a certain name will get submitted. You need to give them unique names.
I don't understand one thing here. My SQL table called player_data has six columns (translations in brackets):ID, NAME(ime), SURNAME(priimek), BORN (rojen) CLUB, MAIL e.g.1 Wayne Gretzky Oilers 1960 gretzky@something.com2 Steve Yzerman Redwings 1965 stevie@something.com3 Curtis Joseph Leafs 1965 cujo@something.comIf I change 'ime' to something else I guess I am not getting player name in the same column? In other words, how to tell the script to enter player's name under the name (ime) column if it would have a different name. Doesn't the post command in php part refer to the column names, which are always the same.All I want is to create a form where I can put custom number of players (like three in an example) and at the end press submit. So I don't have to press submit everytime I want to add a player to the database.
Link to comment
Share on other sites

The form field names don't have to correspond to the database column names. Have a look at the database insertion code:

$sql="INSERT INTO player_data (name, surname, born, club, email)VALUES('$_POST[name]','$_POST[surname]','$_POST[born]','$_POST[club]','$_POST[email]')";

Link to comment
Share on other sites

Guys, many thanks for the tips. It works now with this code:

<form action="insert.php" method="post">Name0: <input type="text" name="ime0" />Surname0: <input type="text" name="priimek0" />Born0: <input type="text" name="rojen0" />Club0: <input type="text" name="klub0" />Mail0: <input type="text" name="mail0" /></br>Name1: <input type="text" name="ime1" />Surname1: <input type="text" name="priimek1" />Born1: <input type="text" name="rojen1" />Club1: <input type="text" name="klub1" />Mail1: <input type="text" name="mail1" /><input type="submit" /></form>

for php:

<?php$con = mysql_connect("localhost","root","Grassw0rd");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("zirafakap", $con);$sql="INSERT INTO igralec_podatki (ime, priimek, rojen, klub, mail)VALUES('$_POST[ime0]','$_POST[priimek0]','$_POST[rojen0]','$_POST[klub0]','$_POST[mail0]'),('$_POST[ime1]','$_POST[priimek1]','$_POST[rojen1]','$_POST[klub1]','$_POST[mail1]') ";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "2 records added";mysql_close($con)?>

Is there anything in the code I should change, add? or is this sufficient. Thx again.

Link to comment
Share on other sites

To be entirely syntactically correct, you need to delimit your inline array index references:

" ... ('{$_POST['ime0']}','{$_POST['priimek0']}',... "

However, to prevent SQL injection, you should sanitize all the post variables using mysql_real_escape_string().Also, it would probably be a good idea to check whether the user actually filled out the extra form fields before inserting potentially empty records.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...