I have been practising mysql and then I have came across an issue that I can't solve. I am making a large form that will be able to store information from user in the database. I have managed to make and store data from simple form fields like Name, Country, Age, Interest and etc using mysql and php.
I have a table called users
in my database which have approximate 15 column for different data like Name, Country, Age etc.
Now I have added this from field to my from and confused about how to store data from this field.
Here is the img of my form ....
Now if I create a column emp_name
then how am I going to store 1-4 values for each user with 5 fields each?
Even if I create a different table ex - emp_history
then how to store data from this form for each user?
Create two tables
tbl_more_detail ( here you store all for info 1-4)
id (PK)
emp_id
emp_name
emp_country
emp_position
emp_from
emp_to
tbl_emp_detail ( here you give reference to above ids, will have 4 rows with different detail_id)
id(PK)
emp_id
detail_id ( FK Refer totbl_more_detail
.id
)
NOTE: to prevent duplicate entry, create UNIQUE KEY INDEX on (emp_id, detail_id)
Your form should be like this:
<form name="xyz" method="post"> <table> <tr> <td>Emp name</td><td>country</td><td>position</td> </tr> <tr> <td> <input type="text" name="data[emp_name][]" value="" /> <input type="text" name="data[country][]" value="" /> <input type="text" name="data[position][]" value="" /> </td> </tr> <tr> <td> <input type="text" name="data[emp_name][]" value="" /> <input type="text" name="data[country][]" value="" /> <input type="text" name="data[position][]" value="" /> </td> </tr> <tr> <td> <input type="text" name="data[emp_name][]" value="" /> <input type="text" name="data[country][]" value="" /> <input type="text" name="data[position][]" value="" /> </td> </tr> </table>
when you post the form, you will get the value in php file like this:
$arr = $_POST['data']; $cnt = count($arr['emp_name']);
for($i=0; $i<$cnt; $i++) {
$name = $arr['emp_name'][$i];
$country = $arr['country'][$i];
$position = $arr['position'][$i];
// insert sql
$sql = insert into emp_table(name, country, position) values('".$name."', '".$country."', '".$position."'); }
First create a database table:
create table emp_table {
id int(10) auto_increment primary key,
emp_name varchar(100),
country varchar(50),
position varchar(50)
}
now create a php file that includes below code:
<h2>Insert new records</h2>
<form name="emp" method="post">
<table border="1">
<tr>
<td>Employee name</td>
<td>Country</td>
<td>Position</td>
</tr>
<tr>
<td><input type="text" name="data[emp_name][]" value="" /></td>
<td><input type="text" name="data[country][]" value="" /></td>
<td><input type="text" name="data[position][]" value="" /></td>
</tr>
<tr>
<td><input type="text" name="data[emp_name][]" value="" /></td>
<td><input type="text" name="data[country][]" value="" /></td>
<td><input type="text" name="data[position][]" value="" /></td>
</tr>
<tr>
<td><input type="text" name="data[emp_name][]" value="" /></td>
<td><input type="text" name="data[country][]" value="" /></td>
<td><input type="text" name="data[position][]" value="" /></td>
</tr>
<tr>
<td><input type="text" name="data[emp_name][]" value="" /></td>
<td><input type="text" name="data[country][]" value="" /></td>
<td><input type="text" name="data[position][]" value="" /></td>
</tr>
</table>
</form>
<?php
mysql_connect('localhost', 'dbuser', 'dbpassword');
mysql_select_db('db_name');
if(isset($_POST['data'])) {
$cnt = $_POST['data']['emp_name'];
for($i=0; $i<$cnt; $i++) {
$name = $arr['emp_name'][$i];
$country = $arr['country'][$i];
$position = $arr['position'][$i];
// insert sql
$sql = "insert into emp_table(name, country, position) values('".$name."', '".$country."', '".$position."')";
mysql_query($sql);
}
}
?>
<br/><br/>
<h2>Existing Records</h2>
<table border="1">
<tr>
<td>Employee name</td>
<td>Country</td>
<td>Position</td>
</tr>
<?php
$str = '';
$objQuery = mysql_query('select * from emp_table');
while($result = mysql_fetch_array($objQuery))
{
$str .= '<tr>';
$str .= '<td>'.$result['emp_name'].'</td><td>'.$result['country'].'</td><td>'.$result['position'].'</td>';
$str .= '</tr>';
}
echo $str;
?>
</table>
Now run your application. You can see the result on the same page (with an existing records)