Jump to content

Multiple Insert Issue


dajesterofdeath

Recommended Posts

Hi Guys, Im learning php and im just having an issue trying to do the following. Table: apptsColumns: appt_id, company, appt_date, appt_time, appt_active i have a form: [dropdown list of companies] (select name is companyselect)[date popup] (name is date1)[textfield] (name is 0900) I am trying to achieve this: I want to select a company, select a date and then enter the number of appointments for that date in the text field. Then on submit i need to add a row with this data for each number of appts (ie, if i entered 5 into the textfield i want 5 rows of the same data added to the table) This is my code, though i dont know where im going wrong

<?phpmysql_connect("localhost", "root", "password") or die("Connection buggered");mysql_select_db("cms")or die("Connection screwed");for($i = 0; $i <=(count($_POST['0900'])); $i++){if (isset($_POST['0900' .$i]) ANDisset($_POST['date1' .$i]))ANDisset($_POST['0900'.$i])) {$company1 = $_POST['companyselect'.$i];$date1 = $_POST['date1'.$i];$timeslot = $_POST['0900'.$i];if ($timeslot!=""){$sql = "INSERT INTO appts (company, appt_date, appt_time) VALUES ('$company1','$date1','$timeslot')";$query = mysql_query($sql);if (!query){echo "INSERT error in $sql. MySQL error: ".mysql_error();}?>

any assistance with this would be much appriciated. That is my full code for the submission. The Form is on a seperate page that actions to this one. Thank you. Robert

Link to comment
Share on other sites

what is not working? any errors?

Link to comment
Share on other sites

What does your form look like? These statements don't really agree: count($_POST['0900'])if (isset($_POST['0900' .$i]) One of those assumes that $_POST['0900'] is an array, and the other assumes that you have variables like $_POST['09000'], $_POST['09001'], etc.

Link to comment
Share on other sites

Hi Guys, This is my form page, i was just going to make 1 insert script per timeslot as i would be easier for me (not sure how i would write it).only error i get is a HTTP 500 internal error

<?phpinclude_once ('auth.php'); mysql_connect("localhost", "root", "password") or die("Connection buggered");mysql_select_db("cms")or die("Connection screwed");$result = mysql_query("select id,company from companies"); ?><!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>Add Appointments</title><link href="include/loginmodule.css" rel="stylesheet" type="text/css" /><script language="JavaScript" src="include/datepicker.js" type="text/javascript"></script><link href="include/datepicker.css" rel="stylesheet" /></head><body><h1>Add Appointments</h1><a href="home.php">Home</a> | <a href="include/logout.php">Logout</a><center><table width="990" border="0" align="center" cellpadding="10" cellspacing="0" style="background:#ffffff;">  <tr>   <td>	<div align="left">	 <div id="bodyContent" style="background:#ffffff;">	  <form id="form1" name="Update" method="post" action="include/addappts.php">	   <table>	   <tr><td width = "150px" align = "right">Select Company:</td><td><?phpprint "<select name=\"companyselect\">\n";while ($row = mysql_fetch_assoc($result)){  $id = $row['id'];  $company = $row['company'];  print "<option value=$company>$company\n";  }print "</select>\n";?></td></tr>     	   <tr><td width = "50px" align = "right">Date:</td><td> <input class="retext" type="text" size="20" name="date1"  onClick="GetDate(this);" /></td></tr><tr><td width = "50px" align = "right">0900:</td><td> <input class="retext" type="text" size="30" name="0900"/></td></tr><tr><td width = "50px" align = "right">0930:</td><td> <input class="retext" type="text" size="30" name="0930"/></td></tr><tr><td width = "50px" align = "right">1000:</td><td> <input class="retext" type="text" size="30" name="1000"/></td></tr><tr><td width = "50px" align = "right">1030:</td><td> <input class="retext" type="text" size="30" name="1030"/></td></tr><tr><td width = "50px" align = "right">1100:</td><td> <input class="retext" type="text" size="30" name="1100"/></td></tr><tr><td width = "50px" align = "right">1130:</td><td> <input class="retext" type="text" size="30" name="1130"/></td></tr><tr><td width = "50px" align = "right">1200:</td><td> <input class="retext" type="text" size="30" name="1200"/></td></tr><tr><td width = "50px" align = "right">1230:</td><td> <input class="retext" type="text" size="30" name="1230"/></td></tr><tr><td width = "50px" align = "right">1300:</td><td> <input class="retext" type="text" size="30" name="1300"/></td></tr><tr><td width = "50px" align = "right">1330:</td><td> <input class="retext" type="text" size="30" name="1330"/></td></tr><tr><td width = "50px" align = "right">1400:</td><td> <input class="retext" type="text" size="30" name="1400"/></td></tr><tr><td width = "50px" align = "right">1430:</td><td> <input class="retext" type="text" size="30" name="1430"/></td></tr><tr><td width = "50px" align = "right">1500:</td><td> <input class="retext" type="text" size="30" name="1500"/></td></tr><tr><td width = "50px" align = "right">1530:</td><td> <input class="retext" type="text" size="30" name="1530"/></td></tr><tr><td width = "50px" align = "right">1600:</td><td> <input class="retext" type="text" size="30" name="1600"/></td></tr><tr><td width = "50px" align = "right">1630:</td><td> <input class="retext" type="text" size="30" name="1630"/></td></tr><tr><td width = "50px" align = "right">1700:</td><td> <input class="retext" type="text" size="30" name="1700"/></td></tr><tr><td width = "50px" align = "right">1730:</td><td> <input class="retext" type="text" size="30" name="1730"/></td></tr><tr><td width = "50px" align = "right">1800:</td><td> <input class="retext" type="text" size="30" name="1800"/></td></tr><tr><td width = "50px" align = "right">1830:</td><td> <input class="retext" type="text" size="30" name="1830"/></td></tr><tr><td width = "50px" align = "right">1900:</td><td> <input class="retext" type="text" size="30" name="1900"/></td></tr><tr><td width = "50px" align = "right">1930:</td><td> <input class="retext" type="text" size="30" name="1930"/></td></tr><tr><td></td><td><input type="submit" name="submit" value="Submit Appointment Times" style="border: #000080 1px solid; background: orange; padding: 3px; color: #fff;"/></td></tr></table>	  </form>	 </div>	</div>   </td>  </tr></table></center></body></html>

Link to comment
Share on other sites

Ive even tried something like this:

<?phpmysql_connect("localhost", "root", "password") or die("Connection buggered");mysql_select_db("cms")or die("Connection screwed");$company = $_POST['companyselect'];$date1 = $_POST['date1'];$timeslot = $_POST['0900'];$times = $_POST['0900'];$x = 0;do {mysql_query("INSERT INTO appts (company, appt_date, appt_time) VALUES ('$company1','$date1','0900')");++$x;} while ($x < $times)?>

but it only writes to the table once.

Link to comment
Share on other sites

If your inputs are named "0900", "0930", "1000", etc, then you'll need to just access each one individually to see if it's empty, which means a lot of copied and pasted code. One way to do that with a loop would be to store all of the names in an array, and then loop through the array to look each one up. e.g.:

$inputs = array('0900', '0930', '1000'); foreach ($inputs as $name){  $value = $_POST[$name];  // process the value}

You could also find a naming scheme that you could loop through, like use increments of 50 instead of 30 (0900, 0950, 1000, etc)

for ($i = 900; $i < 2000; $i += 50){  $name = str_replace('50', '30', str_pad($i, 4, '0', STR_PAD_LEFT));  $value = $_POST[$name];}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...