How to add dynamically rows into database ?Need help pls

How to add dynamically rows into database ?Need help pls

Hi everyone, I really have a problem of insert multiple rows into the database.Now i can only insert one row into the database.How can insert the rest of the row into the database.I have been trying to solve in for ages.Can anyone pls help me.Thanks in advanced. registerEmployee.php
script language="Javascript" type="text/javascript">
function addRow()
{

var tbl = document.getElementById('mySampleTable');
var lastRow = tbl.rows.length;
var iteration = lastRow;

var row = tbl.insertRow(lastRow);


var cell1 = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cell1.appendChild(textNode);

var cell2 = row.insertCell(1);
var a = document.createElement('input');
a.type = 'text';
a.name = 'fullname' + iteration;
a.id = 'txtRow1' + iteration;
a.size = 30
cell2.appendChild(a);

var cell3 = row.insertCell(2);
var b = document.createElement('input');
b.type = 'text';
b.name = 'nric' + iteration;
b.id = 'txtRow2' + iteration;
b.size = 20
cell3.appendChild(B);

var cell4 = row.insertCell(3);
var c = document.createElement('input');
c.type = 'text';
c.name = 'password' + iteration;
c.id = 'txtRow3' + iteration;
c.size = 25
cell4.appendChild©;

var cell5 = row.insertCell(4);
var d = document.createElement('input');
d.type = 'text';
d.name = 'age' + iteration;
d.id = 'txtRow4' + iteration;
d.size = 5
cell5.appendChild(d);


var cell6 = row.insertCell(5);
var sel3 = document.createElement('select');
sel3.name = 'gender' + iteration;
sel3.id = 'txtRow5' + iteration;
sel3.options[0] = new Option('-- Select M/F --', 'value0');
sel3.options[1] = new Option('Male', 'value1');
sel3.options[2] = new Option('Female', 'value2');
cell6.appendChild(sel3);


var cell7 = row.insertCell(6);
var f= document.createElement('input');
f.type = 'text';
f.name = 'email' + iteration;
f.id = 'txtRow6' + iteration;
f.size = 25
cell7.appendChild(f);



var cell8 = row.insertCell(7);
var sel = document.createElement('select');
sel.name = 'selRow' + iteration;
sel.id = 'textRow7' + iteration;
sel.options[0] = new Option('--Select Position--', 'value1');
sel.options[1] = new Option('Manager', 'value2');
sel.options[2] = new Option('Supervisor', 'value3');
sel.options[3] = new Option('General Worker', 'value4');
cell8.appendChild(sel);


var cell9 = row.insertCell(8);
var sel2 = document.createElement('select');
sel2.name = 'selRow2' + iteration;
sel2.id = 'textRow8' + iteration;
sel2.options[0] = new Option('-- Select Yes/No --', 'value0');
sel2.options[1] = new Option('Yes', 'value1');
sel2.options[2] = new Option('No', 'value2');

cell9.appendChild(sel2);




}

function removeRow()
{
// grab the element again!
var tbl = document.getElementById('mySampleTable');
// grab the length!
var lastRow = tbl.rows.length;
// delete the last row if there is more than one row!
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}

</script>

<html>
<head>
<title>Register Employee</title>
</head>
<body leftmargin="0" topmargin="0">

<form action="doRegister.php" name="eval_edit" method="post" >
<table align="center" width = "300%">
<tr>
<td align = "center">

<h2>SJAS -Register Employee</h2>
<h4>Please fill in the following to sign up for your Employee .</h4>
<h2>Employee Information</h2>
<hr>
</td>
</tr>
<tr>
<td align = "center">

<table border="2" id="mySampleTable"
<tr>
<td>
No.
</td>
<td>
FULL NAME
</td>
<td>
NRIC
</td>
<td>PASSWORD
</td>

<td>
AGE
</td>
<td>GENDER</td>
<td>EMAIL</td>

<td>POSITION</td>
<td>SKILLS DEVELOPMENT FUND</td>
</tr>

<td>
1
</td>

<td>
<input type="text" name="fullname" id="txtRow1" size="30" /></td>
<td>
<input type="text" name="nric" id="txtRow2" size="20" /></td>

<td>
<input type="text" name="password" id="txtRow3" size="25" /></td>
<td>
<input type="text" name="age" id="txtRow4" size="5" /></td>
<td>
<select name="gender" id="txtRow5">
<option value="value0"selected="selected">-- Select M/F --</option>
<option value="Male">Male</option>
<option value="Female">Female</option>

</select>
</td>
<td>
<input type="text" name="email" id="txtRow6" size="25" /></td>
<td>
<select name="selRow" id="textRow7">
<option value="value1"selected="selected" >--Select Position--</option>

<option value="Manager">Manager</option>
<option value="Supervisor">Supervisor</option>
<option value="General Worker">General Worker</option>
</select>
</td>
<td>
<select name="selRow2" id="textRow8">
<option value="value0"selected="selected">-- Select Yes/No --</option>

<option value="Yes">Yes</option>
<option value="No">No</option>

</select>
</td>

</tr>



</table>


<input type="button" value="Add Row" onclick="addRow();" />
<input type="button" value="Remove Row" onclick="removeRow();" />
<label> <input name="h" type="hidden" id="h" value="0" />
</label>

<center> <input type="submit" value="Submit" /></center>

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

doRegister.php

<html>
    <head>
        <title>Register Employee</title>
    </head>
    <body leftmargin="0" topmargin="0">

            <table align="center" width = "300%">
                <tr>
                    <td align = "center">

                        <h2>SJAS -Register Employee</h2>
                        <hr>


</table>

<?php
if(isset($_POST['fullname'])){
        $fullname = $_POST['fullname'];
        $password= $_POST['password'];
        $nric = $_POST['nric'];
        $age = $_POST['age'];
        $gender= $_POST['gender'];
        $email = $_POST['email'];
        $position= $_POST['selRow'];
        $sdf = $_POST['selRow2'];




           //erase spaces
        $fullname = trim($fullname);
        $email = trim($email);

        //allow special character
         $email = htmlspecialchars($email, ENT_QUOTES);
          $nric = htmlspecialchars($nric,ENT_QUOTES);
     $password = htmlspecialchars($password,ENT_QUOTES);

include('config/db.cfg');
        $link = mysqli_connect('localhost', 'root', '', 'sjas') or die(mysqli_connect_error());
        $query = "INSERT INTO employee(fullname,password,nric,age,gender,email,position,sdf) VALUES ('$fullname',SHA1('$password'),'$nric','$age','$gender','$email','$position','$sdf')";
        $result = mysqli_query($link, $query) or die('Error querying database');
        Echo "<center><h3>Thank You! You have <font color=\"Green\">Successfully</font> registered for your employee</h3></center>";
        mysqli_close($link);

                }
  ?>

</html>
View Answers









Related Tutorials/Questions & Answers:

Ads