This is the report.php view

if(isset($_POST["addReport"])) {
	$report = new Report();
	$report->employee_id = $_POST["employee_id"];
	$report->date = $_POST["date"];
	$report->job_id = $_POST["job_id"];
	$report->description = $_POST["description"];
	$report->attachment = file_get_contents($_FILES['attachment']['tmp_name']);
	for($i = 0; $i < count($_POST['date_range']); $i++) {
              
			  $report->request_date[$i]		= $_POST["request_date"];
    }
		    
    for($i = 0; $i < count($_POST['date_range']); $i++) {
             
		      $report->quantity[$i]			= $_POST["quantity"];
    }
		    
    for($i = 0; $i < count($_POST['date_range']); $i++) {
             
			  $report->ms_num[$i]			= $_POST["ms_num"];
    }
		    
    for($i = 0; $i < count($_POST['date_range']); $i++) {
             
			  $report->info[$i]				= $_POST["info"];
    }
		    
    for($i = 0; $i < count($_POST['date_range']); $i++) {
             
			  $report->description[$i]		= $_POST["description"]; 
    }
		   
    for($i = 0; $i < count($_POST['date_range']); $i++) {
             
			  $report->date_recieved[$i]	= $_POST["date_recieved"];
    }
	$report->addReport();
	redirect_to("index.php#tabs-3");

}


?>

<script>
	var counter = 1;
var limit = 10;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}
</script>

			<h2>Daily Reports</h2>
			<h3>Add a Report</h3>
			<form action="report.php" method="post" enctype="multipart/form-data">
			<table>
				<tr>
					<td>
                        <strong>Project Name:</strong><select name="job_id" id="job_id">
							<?php echo $catlist_job; ?>
						</select>
						<br />
						<strong>Today's Date: <?php echo $today; ?></strong><?php  echo "<input type ='hidden' value='". $today ."' name='created_on' id='created_on' />" ?>
						<br />
						<strong>Energy Wise:</strong><select name="energy_wise" id="energy_wise">
							<?php echo $catlist_job; ?>
						</select>
						<br />
					    <strong>P.I.C Name: <?php echo $profile->first_name ." ". $profile->last_name ; ?></strong><?php  echo "<input type ='hidden' value='".$profile->employee_id."' name='employee_id' id='employee_id' />" ?>
						<br />
						<strong>Project Oversight: Enersol</strong>
						<br />				
					    <strong>Report Generated at</strong>
						<br />
							<div class="hours">
						<strong>ECC IN -</strong><select name="ecc_in" id="ecc_in"> </select>
						<strong> , OUT -</strong><select name="ecc_out" id="ecc_out"> </select>
						<br />
						<strong>DCC IN -</strong><select name="dcc_in" id="dcc_in"> </select>
						<strong> , OUT -</strong><select name="dcc_out" id="dcc_out"> </select>
						<strong> , Dispatcher - </strong><select name="dispatcher" id="dispatcher"> </select>
							</div>
						<br />
						<br />
						<strong>Dailey Notes:</strong><input type="text" name="notes" id="notes" />
						<br />
						<strong>Extra Work Today:</strong><input type="text" name="extra" id="extra" />
						<br />
						<strong>Missing material:</strong>
						<br />
						     <script language="Javascript" type="text/javascript">
      	
      	var counter = 1;
        var limit = 10;
        
        function addInput(divName){
                if (counter == limit)  {
                     alert("You have reached the limit of adding " + counter + " inputs");
                }
                else {
                      var newdiv = document.createElement('div');
                      newdiv.innerHTML = " <table><tr><td><input type='text' name='request_date[]'></td><td><input type='text' name='quantity[]'></td><td><input type='text' name='quantity[]'></td><td><input type='text' name='ms_num[]'></td><td><input type='text' name='info[]'></td><td><input type='text' name='description[]'></td><td><input type='text' name='date_recieved[]'></td></tr></table>";
                      document.getElementById(divName).appendChild(newdiv);
                      counter++;
     }
}
      	
      </script>
      <form method="POST">
     <div id="dynamicInput">
     	<table>
				            <thead>
				               <tr>
				                  <td>Date Requested</td>
				                  <th>QTY</th>
					              <th>M&S#</th>
					              <th>PLATE NUMBER/REASON</th>
					              <th>DESCRIPTION</th>
					              <th>Date Received</th>
				              </tr>
				        </thead>
				        <tr>
                <td><input type='text' name='request_date[]' id=""></td>
				<td><input type='text' name='quantity[]' id=""></td>
				<td><input type='text' name='ms_num[]' id=""></td>
				<td><input type='text' name='info[]' id=""></td>
				<td><input type='text' name='description[]' id=""></td>
				<td><input type='text' name='date_recieved[]' id=""></td>
				</tr>

     <input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</form>
      </table>
           </div>
						<br />
						<strong>Visitors on Site :</strong><input type="text" name="visitors" id="visitors" />
						<br />
						<strong>Weather:</strong><input type="text" name="weather" id="weather" />
						<br />
						<strong>Resources:</strong>
						<br />
						<strong>Number Of Contractors On site:</strong><input type="text" name="contractors_on" id="contractors_on" />
						<br />
						<strong> Hours Worked:</strong><input type="text" name="hours_worked" id="hours_worked" />
						<br />
						<strong>Equipment :</strong><input type="text" name="equipment" id="equipment" />
						<br />
						<strong>Schedule</strong>
						<br />
						<strong>Percent Complete: </strong><input type="text" name="percent_complete" id="percent_complete" />
						<br />
						<strong>CMI:</strong><input type="text" name="cmi" id="cmi" />
						<br />
						<strong>Estimated Date Of Completion: </strong><input type="text" name="est_complete" id="est_complete" />
						<br />
						<strong>Safety Topic of Day:</strong>
						<br />
						<strong>Near Misses:</strong><input type="text" name="misses" id="misses" />
						<br />
						<strong>Accidents:</strong><input type="text" name="accidents" id="accidents" />
						<br />
						<strong>Observations :</strong><input type="text" name="observations" id="observations" />
						<br />
						<input type="submit" name="addReport" id="addReport" value="Create" />
					</td>
				</tr>
			</table>
			</form>

and this is the report.php class

<?php
class Report {
	
	public $job_id;
    public $created_on;
    public $energy_wise;
    public $employee_id;
    public $ecc_in;
    public $ecc_out;
    public $dcc_in;
    public $dcc_out;
    public $dispatcher;
    public $notes;
    public $visitors;
    public $weather;
    public $contractors_on;
    public $hours_worked;
    public $equipment;
    public $percent_complete;
    public $cmi;
    public $est_complete;
    public $misses;
    public $accidents;
    public $observations;

	
	public static function find_all() {
		return self::find_by_sql ( "SELECT * FROM report ORDER BY date ASC" );
	}
	
    public static function find_by_id($employee_id) {
		return self::find_by_sql ( "SELECT * FROM report WHERE employee_id={$employee_id} ORDER BY date ASC" );
	}
	
	public static function get_count() {
		global $database;
		$result_array = $database->query("SELECT * FROM report");
		return $database->num_rows($result_array);
	}
	
	public static function find_by_sql($sql = "") {
		global $database;
		$result_set = $database->query ( $sql );
		$object_array = array();
		while ($row = $database->fetch_array($result_set)){
			$object_array[] = self::instantiate($row);
		}
		return $object_array;
	}
	
	private static function instantiate($record) {
		$object = new self;
		foreach($record as $attribute=>$value) {
			if($object->has_attribute($attribute)) {
				$object->$attribute = $value;
			}
		}
		
		return $object;
	}

	private function has_attribute($attribute) {
		$object_vars = get_object_vars($this);
		return array_key_exists($attribute, $object_vars);
	}
	
	public function addReport() {
		global $database;
		
	$job_id	             = $database->escape_value ($this->job_id);
	$created_on	         = $database->escape_value ($this->created_on);
	$energy_wise	     = $database->escape_value ($this->energy_wise);
	$employee_id	     = $database->escape_value ($this->employee_id);
	$ecc_in	             = $database->escape_value ($this->ecc_in);
	$ecc_out	         = $database->escape_value ($this->ecc_out);
	$dcc_in	             = $database->escape_value ($this->dcc_in);
	$dcc_out	         = $database->escape_value ($this->dcc_out);
	$dispatcher	         = $database->escape_value ($this->dispatcher);
	$notes	             = $database->escape_value ($this->notes);
	$visitors	         = $database->escape_value ($this->visitors);
	$weather	         = $database->escape_value ($this->weather);
	$contractors_on	     = $database->escape_value ($this->contractors_on);
	$hours_worked	     = $database->escape_value ($this->hours_worked);
	$equipment	         = $database->escape_value ($this->equipment);
	$percent_co          = $database->escape_value ($this->percent_co);
	$cmi	             = $database->escape_value ($this->cmi);
	$est_complete	     = $database->escape_value ($this->est_complete);
	$misses	             = $database->escape_value ($this->misses);
	$accidents	         = $database->escape_value ($this->accidents);
	$observations	     = $database->escape_value ($this->observations);
	
			$sql = "INSERT INTO report (job_id, created_on, energy_wise, employee_id, ecc_in, ecc_out, dcc_in, dcc_out, dispatcher, notes, extra, visitors, weather, contractors_on, hours_worked, equipment, percent_complete, cmi, est_complete, misses, accidents, observations) VALUES ";
            $sql .= "('{$job_id}', '{$created_on}', '{$energy_wise}', '{$employee_id}', '{$ecc_in}', '{$ecc_out}', '{$dcc_in}', '{$dcc_out}', '{$dispatcher}', '{$notes}', '{$extra}', '{$visitors}', '{$weather}', '{$contractors_on}', '{$hours_worked}', '{$equipment}', '{$percent_complete}', '{$cmi}', '{$est_complete}', '{$misses}', '{$accidents}', '{$observations}')";
            echo $employee_id;

	}

    public static function delete_report($id) {
		global $database;
		$database->query("DELETE FROM report WHERE id='{$id}'");
	}

}

Thank you ahead of time,

Jason

In report.php class, line 94 you echo inside the class but you should use return:

return $employee_id; #last id of multiple insert

In same function you should set a mysql_query() in order to insert. But it seems you want to send groups of arrays, reordered by report.php view, but there is something wrong, when you write:

$report->request_date[$i] = $_POST["request_date"];

you are not considering that multiple values are set under request_date (and also the others fields of the form), try to simply print_r() that variable: <?php print_r($_POST['request_date']); ?> and you will see an array with a value for each report form, so if there are three reports you will have three request_date values. At the moment you get six different arrays from your form and you need to create a single array, like in this example:

<html>
<head><title></title></head>
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
        print_r($_POST['a']);
        echo "<br />";
        print_r($_POST['b']);

        $neworder = '';
        $c = count($_POST['a']);
        for($i = 0; $i < $c; $i++)
        {
                $neworder[] = array($_POST['a'][$i],$_POST['b'][$i]);
        }
        echo "<br /><pre>";
        print_r($neworder);
        echo "<pre>";
}
?>
<form method="post" action="">
a: <input type="text" name="a[]" />
b: <input type="text" name="b[]" />
<br />
a: <input type="text" name="a[]" />
b: <input type="text" name="b[]" />
<br />
<input type="submit" name="button" value="submit" />
</form>
</body>
</html>

The output:

Array ( [0] => a [1] => aa ) # first report
Array ( [0] => b [1] => bb ) # last report

# single array
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => aa
            [1] => bb
        )
)

Or you can try to create part of the insert string, similar to previous example:

<html>
<head><title></title></head>
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
        print_r($_POST['a']);
        echo "<br />";
        print_r($_POST['b']);

        $neworder = '';
        $c = count($_POST['a']);
        for($i = 0; $i < $c; $i++)
        {
                $a = $_POST['a'][$i];
                $b = $_POST['b'][$i];
                if($i != $c-1)
                {
                        $neworder .= "values('$a','$b'), ";
                }
                else
                {
                        $neworder .= "values('$a','$b')";
                }

        }
        echo "<br /><pre>";
        print_r($neworder); # values to insert
        echo "<pre>";
}
?>
<form method="post" action="">
a: <input type="text" name="a[]" />
b: <input type="text" name="b[]" />
<br />
a: <input type="text" name="a[]" />
b: <input type="text" name="b[]" />
<br />
<input type="submit" name="button" value="submit" />
</form>
</body>
</html>

The output will be:

Array ( [0] => a [1] => aa ) # first group
Array ( [0] => b [1] => bb ) # last group

# string
values('a','b'), values('aa','bb')

At this point you need only to insert:

mysql_query("insert into table ('field_A','field_B') $neworder");

In order to make it work with your class, you can: send a single array (reordered) to addReport() instead of single strings just like in my first example (I think you will need to serialize* data to get the array inside the function and unserialize to get it to work again) and finally create a loop to sanitize each value and, similarly to my second example, create the values() part of the insert string.

Hope is enough clear and not too long, bye :)

* http://php.net/manual/en/function.serialize.php

commented: Informative, shed light on flaw in code, and explained process very well +0

Hai socialmd !!
Your question is how to insert multiple to your MySql table..
My suggestion is get max count loop it..
here is an example , i tried it its works for me..
I have attached it..

<?php
error_reporting(E_ALL ^ E_NOTICE);
$con=mysql_connect("localhost","root","");
$table=mysql_select_db("practise") or die (mysql_error());
if(isset($_POST['submit']))
{
$count2 = $_POST["hidden"];
 for($i=1;$i<=$count2;$i++)
 {
 $save = mysql_query("INSERT into multiple_action (name, age, phno, qualification) VALUES ('".$_POST["name$i"]."', '".$_POST["age$i"]."', '".$_POST["phone$i"]."', '".$_POST["qualification$i"]."')");
 }
if($save)
{
echo '<script type="text/javascript">alert("Saved Success !");</script>';
}
else
{
echo '<script type="text/javascript">alert("Failed");</script>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>My Multiple Save </title>
</head>

<body>
<center>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="get"> <!-- Form for generate no of times -->
<table align="center">
<tr>
	<th colspan="3" align="center">Order Num Of Rows</th>
</tr>
<tr>
	<td>No Of</td>
	<td><input type="text" name="no" /></td>
	<td><input type="submit" name="order" value="Generate" /></td>
</tr>
</table>
</form><!-- End generate Form -->


<!---- Create multiple Save Form,-->
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table align="center">
<tr>
	<th colspan="3" align="left">Dynamic multiple Save To MySql</th>
</tr>
<tr>
	<td>Name</td>
	<td>Age</td>
	<td>Phone</td>
	<td>Qualification</td>
</tr>
<?php
if(isset($_GET['order']))
{
$count = $_GET['no']-1; //get the num of times as numeric
while($i <= $count)// loop by using while(),NOTE the looping dynamic textbox must be under the for loop othet must be outside of while()
{
$i++;
?>
<tr>
	<td><input type="text" name="name<?php echo $i; ?>" /></td>
	<td><input type="text" name="age<?php echo $i; ?>" /></td>
	<td><input type="text" name="phone<?php echo $i; ?>" /></td>
	<td><input type="text" name="qualification<?php echo $i; ?>" /></td>
</tr>
<?php
}}
?>
<tr>
	<td colspan="3" align="center">
	<input type="hidden" name="hidden" value="<?php echo $i; ?>" /><!-- Get max count of loop -->
	<input type="submit" name="submit" value="Save Multiple" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>

Try it it will work, Hope it may work for you

commented: Very well written, easy to follow, and well documented +0

Dynamic Fields js/php to MySql need to submit dynamically to the database
not database.php is not include page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.