Jump to content

multiple insert into database


phpjay

Recommended Posts

Hi to all / master /guru

 

can you help me on my code I have a form which is the user can put a number let say the user put a value number which is 3 then i have a form 3- this is already done on my side im used ajax,

 

My problem was how can i insert does 3 data form into my database see my code below

<?php
session_start();
//error_reporting(0);
$q = $_GET['q']; // a value number to display form
$i=2;


while ($i<=$q):

?>

<div id='form'>
<form id="form1" name="clock" onSubmit="0">
				<input type="hidden" name="manstart2[]" size='13' id="copy_from" onclick="copy_data(this)" readonly />
				<!--<input type='button' name="button" id="copy_from" onclick="copy_data(this)" Value="Stop" align="right"/>-->
				</form>
			
					<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="clockstart" onsubmit="return validateForm()" >
				
				<?php
            
			include 'connection.php';
			
			
            $link=mysql_connect($host, $username, $password) or die ("Error connecting to mysql server: ".mysql_error());
            
            mysql_select_db($db,$link) or die ("Error selecting specified database on mysql server: ".mysql_error());
            
            $query="SELECT name,groupname FROM $table2 where username = '$_SESSION[username]'";
			$result=mysql_query($query) or die ("Query to get data from firsttable failed: ".mysql_error());
            
            while ($row=mysql_fetch_array($result)) 
			{
            $agentfullname=$row['name'];
			$groupname=$row['groupname'];
			                echo "<input type='hidden' value='$agentfullname' name='agentname2[]'>";
							echo "<input type='hidden' value='$groupname' name='groupname2[]'>";
			}
                mysql_close($link);
            ?>
				
				
				<table border='0' bgcolor="#EFEEEC" width='100%'>
				<tr>
				<td align='right'><font size='2px'>Date:</td>
				<td><input type="text" name="mandate2[]" size='7' value="<?php echo date("m/d/Y");?>" readonly /></td>
				<td align='right'><font size='2px'>Agent:</td>
				<td><input type="text" name="managent2[]" value="<?php echo "$_SESSION[username]";?>" readonly size='5'></td>
				<td align='right'><font size='2px'>Provider Name:</td>
				<td>
			
					<input type="text" id="manprovidername2" name="manprovidername2[]" />
					

				</td>
				<td align='right'><input type="hidden" name="manstart2" size='12'  readonly />
			<font size='2px'>Nature of Call:
				<?php echo'<script>


						atoj = new Date();
						atoj1= atoj.getHours();
						atoj2 = atoj.getMinutes();
						atoj3= atoj.getSeconds();

						if(atoj1==0)
						{
						atoj4=" A.M";atoj1=12
						}
						else if(atoj1 <= 11)
						{
						atoj4=" A.M"
						}
						else if(atoj1 == 12)
						{
						atoj4=" P.M";atoj1=12
						}
						else if(atoj1 >= 13)
						{
						atoj4=" P.M";atoj1-=12
						}

						if(atoj2 <= 9){atoj2="0"+atoj2}
						document.clockstart.manstart.value = (""+atoj1+":"+atoj2+":"+atoj3+""+atoj4+""+"");
						</script>';?>
			
				</td>
				<td>
				<input type="text" name="natureofcall2[]" size='12' >
		
				<input type="hidden" name="manstop2[]" size='12' id="copy_to"  readonly />   <font size='2px'>
				<input type="hidden" name="manduration2[]" size='12' >
		</td>
			
				</tr>
				
		
        	<td></td>
			<td></td>
				
				<tr>
				
				<td align='right'><font size='2px'>Action taken:</td>
				<td>
				
				<input type="text" name="actiontaken2[]" size='31' >
				
				</td>
				
				<td align='right'><font size='2px'>POC: </td>
				<td><input type="text" name="poc2[]" size='12'></td>
					
										
			
				
					<td align='right'><font size='2px'>Member:</font></td>
					<td><font size='2px'><input type="text" placeholder='Card No' name='cardno2[]'/></font></td>
					<td align='right'><font size='2px'>Non Member:</font></td>
						<td><font size='2px'><input type="text" placeholder='Input Name here' name='name2[]'/></font></font></td>
					<tr>
							
								<td><font size='2px'>Comments:</td>
								<td colspan='4'><input type='text' name="comments2[]"  placeholder="Enter your comments here..." size='54px'></td>
				</tr><br>
				
				<tr>
								
								<td colspan='3' align='left'><input type="Submit" name="Submit" value="Submit" onclick="copy_data(this)" id="copy_from"/> 
				</tr>
				
			
				
					</form>				
				
				</table>
				</div>
		<?php 
				
				$i=$i+1;

   endwhile;

?>		



<?php

if(isset($_POST['Submit'])) 
	{
		
		// need to insert multiple data here I dont know how
		
	}
?>

	

Link to comment
Share on other sites

to start with, you cannot repeat the same id values in the html and you would never open a database connection, run a query that fetches the same result set each time it is ran, and close the database connection inside of a loop.

 

assuming you actually reference the ids at all (if you are not referencing them, don't put them in the code at all), you would need to append a changing value to them to make them unique.

 

the query you are running inside the while ($i<=$q) loop produces the same result set every time  (i.e. there's nothing dynamic in it that changes for each iteration through the loop.) you should run that query once, before the start of the loop and store the output it produces in a php variable. then just use that php variable inside the while ($i<=$q) loop.

 

your code is producing individual forms, each with an opening/closing form tag, inside the while ($i<=$q) loop. in this case, only one of them can be submitted at a time and there won't be multiple sets of data to be inserted. if your goal is to be able to repeat the sets of form fields, you would have one opening form tag, before the start of your loop, and one closing form tag, after the end of your loop, and you would just be repeating the form fields inside of the loop.

 

you need to fix the above problems, before you can get to the point of submitting the multiple sets of form fields all at once to be in a position of needing to insert multiple sets of data. once you do this, the array names you are using for your form fields will result in arrays of data being submitted to your php code. you would loop over the arrays to get the corresponding data from each set of form fields.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.