0

I'm trying to add multiple records to an attendance table in a mysql database. Having trouble building an array to check for the multiple entries with the same variables.

<?php

if (isset($_POST['submitted'])) {
	
	include('connect-mysql.php');
        
        $number = $_GET['number'];  
        for ($i = 0; $i <= $number; $i++)  
        {  
        $value = $_GET['value']; 
        $text = $_GET['text'];   
        }
	$sqlinsert = "INSERT INTO attendance (value, text) VALUES ('$value', '$text')";
	
	IF (!mysqli_query($dbcon, $sqlinsert)) {
		die('error inserting new record');
	} //end of nested if statement

	$newrecord = "Records added successfully to the database";


} //end of the main if statement 


?>


<html>
<head>
<title>Insert Data into Attendance Record</title>
</head>
<body>

<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true" />

<fieldset>
<table style="width: 400px">
<legend>Attendance</legend>
	<tr>
		<td><label>Early  <input name="value[]" type="text" id="value[]" /></label></td>
                <td><input name="text[]" type="hidden" id="text[]" value="Monday" /><label></td>
	</tr>
        <tr>
		<td><label>Family <input name="value[]"type="text" id="value[]" /></label></td>
                <td><input name="text[]" type="hidden" id="text[]" value="Wednesday" /><label></td>
	</tr>
        <tr>
		<td><label>Evening <input name="value[]"type="text" id="value[]" /></label></td>
                <td><input name="text[]" type="hidden" id="text[]" value="Friday" /><label></td>
	</tr>
                <input type=hidden name=number value=<? echo $number;?>>
</table>	
</fieldset>
<br>
<input type="submit" value"add data" />
</form>

<?php
echo $newrecord 
?>

</body>
</html>
4
Contributors
9
Replies
46
Views
6 Years
Discussion Span
Last Post by ramaswamy123
0

Compare this code to what you have done and you will see what is wrong with you code.

<?php

	if(!isset($_POST['send']))
	{
		echo "<form method = 'post' action = '".$_SERVER['PHP_SELF']."'>";
			echo "<table>";
			echo "<tr>";
				echo "<td align = 'center'>STUDENT ID</td>";
				echo "<td align = 'center'>SCORE</td>";
			echo "</tr>";
			for($counter = 0; $counter < 5; $counter++)
			{
				echo "<tr>";
					echo "<td>".($counter + 1)."<input type = 'text' name = 'id[]'/></td>";
					echo "<td><input type = 'text' name = 'score[]'/></td>";
				echo "</tr>";
			}
			echo "<tr>";
				echo "<td colspan = '2' align = 'center'>";
					echo "<input type = 'submit' name = 'send' value = 'SEND'/>";
					echo "<input type = 'reset' value = 'CLEAR'/>";
				echo "</td>";
			echo "</tr>";
			echo "</table>";
		echo "</form>";
	}
	else
	{
		
		$id = $_POST['id'];
		$score = $_POST['score'];
		
		$connection = mysql_pconnect("localhost", "root", "password") or die(mysql_error());
		mysql_select_db("mm") or die(mysql_error());
		
		//Array to keep track of errors
		$my_errors = array();
		
		for($count = 0; $count < sizeof($id); $count++)
		{
			if(!empty($id[$count]))
			{
				$student = mysql_real_escape_string($id[$count]);
				$mark = mysql_real_escape_string($score[$count]);
				
				$query = "INSERT INTO try (id, score) VALUES('".$student."',$mark)";
				if(!mysql_query($query))
				{
					$my_errors[] = $id[$count];
				}
			}
		}
		
		if(sizeof($my_errors) > 0)
		{
			//At least one error occurred
			echo "An error occurred while processing the following students' records: <br/>";
			echo "<ol>";
				for($err = 0; $err < sizeof($my_errors); $err++)
				{
					echo "<li>".$my_errors[$err]."</li>";
				}
			echo "</ol>";
		}
		else
		{
			//Everything Ok
			echo "All recoreds have been successfully processed.";
		}
	
	}

?>
0
$number = $_GET['number'];          
for ($i = 0; $i <= $number; $i++)          
{          
   $value = $_GET['value'];         
   $text = $_GET['text'];             
}

Forget this - it won't work. You're overwriting the value and text variables on every iteration of the loop, so you will always only insert the last set of values.

I assume that you need to build a list of values for insertion. For this you need to use the .= concatenator.

In addition, you are looping what seem to be non-array variables.I can't really see the rationale behind this.

DO you want to create x number of value-text pairs in the table?? If so, you could try this:

if(isset($_GET['value']) && isset($_GET['text']) && isset($_GET['number'])){
   $sVal = intval($_GET['value']); //this is lazy - check for numerics and ranges
   $sNum = intval($_GET['number']); //this is lazy - check for numerics and ranges
   $sText = mysql_real_escape_string($_GET['text']); //sanitize
   $sqlSnipSimple = "($sVal,'$sText'),"; //simple 'unit' string
   $sqlSnip = substr(str_repeat($sqlSnipSimple, $sNum),0,-1); // create list of insert snippets and take off the last comma
  
   $sqlinsert = "INSERT INTO attendance (value, text) VALUES $sqlSnip";
   //rest of script....
}

Edited by diafol: n/a

0

Ok sorry maybe I didnt explain properly what I was trying to do. I have 3 meetings that happen in the week one Monday, one Wednesday and one Friday. The form ive written is intended to add a record of attendance and the name of the meeting to one table in the database example of database

ID Value Text
1 256 Monday
2 261 Wednesday
3 279 Friday

The form would look like this

Monday: Textbox
Wednesday : Textbox
Friday: Textbox

Submit

I though that because the 'value' and 'text' variables change I thought id need to put them in an array somehow so tried this counter approach which doesnt seem to work would a foreach loop be better?

0

How about using method = "post" and:

<input type="text" name="day[Monday]" value="" />
<input type="text" name="day[Wednesday]" value="" />
<input type="text" name="day[Friday]" value="" />

The the php handler:

$sql = "";
foreach($_POST['day'] as $key => $val){
  if($val != ""){
     $sql .= "($val,'$key'),"
  }

}
$sql = substr($sql,0,-1);
//check to see $sql != ""
$sqlinsert = "INSERT INTO attendance (value, text) VALUES $sql";
0

OK the new code gives me an "error inserting new record" page and enters one of the rows to the table for with the correct data

ID Value Text
54 545 Monday

There are no records added for Wednesday or Friday

0

do this at the end of the code to see what's being outputted:

echo "INSERT INTO attendance (value, text) VALUES $sql";
0

That's not showing anything else, have I put it in the right place?

<?php

if (isset($_POST['submitted'])) {
	
	include('connect-mysql.php');
        $sql = "";
        foreach($_POST['day'] as $key => $val){
        if($val != "")
        $sql .= "($val,'$key'),";

        $sql = substr($sql,0,-1);
//check to see $sqlhttp://www.daniweb.com/rxrvbimages/editor/menupop.gif != ""
        $sqlinsert = "INSERT INTO attendance (value, text) VALUES $sql";
	
	IF (!mysqli_query($dbcon, $sqlinsert)) {
		die("error inserting records");
        
	} //end of nested if statement

	$newrecord = "Records added successfully to the database";
}
} //end of the main if statement
echo "INSERT INTO attendance (value, text) VALUES $sql";
?>

Edited by macgibbo83: n/a

0
if (isset($_POST['submitted'])) { 	
   include('connect-mysql.php');
   $sql = "";
   foreach($_POST['day'] as $key => $val){
        if($val != ""){
             $sql .= "($val,'$key'),";
        }
   }
   $sql = substr($sql,0,-1);
   $sqlinsert = "INSERT INTO attendance (value, text) VALUES $sql";
   if(!mysqli_query($dbcon, $sqlinsert)) {
	die("error inserting records"); 	
   } 
   $newrecord = "Records added successfully to the database";
   echo "INSERT INTO attendance (value, text) VALUES $sql";
}
0

am to insert the student attendance to insert multiple rows at time
rtrive the student id and name attendance and submit at time

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.