Jump to content

arrays and inserting data in to the database


dbdummy

Recommended Posts

hi, I have a form that uses two arrays one get's a list of courses from the database and one creates dates...

 

i have two questions

 

1. how do i get the $i and $d value off of the arrays to insert in to the database, but still have each know they belong in the same row?

 

2. how do i change the dates from a multiselect dates ie, Jun 3, Jun 6, and Jun 12 to 'Jun 3, 6, 12'

 

here is the snippet of code from my select page. $numposts is a value selected on an earlier page.

 

for ($i = 0; $i < $numposts; $i++){
echo "<tr> 
      <td><strong>Course</strong></td>
      <td>";
$query_course = "SELECT * FROM courses ORDER BY course_number";
$course = mysql_query($query_course) or die(mysql_error());
if (!$result) 
{
}
echo "<select name='$session_course$i' id='$session_course$i'>
          <option selected='selected'>Select Course</option>";

while ($row_course = mysql_fetch_assoc($course))
{
$cnumber=$row_course['course_number'];
$cnumberid=$row_course['course_id'];
          echo "<option value='$cnumberid$i'>$cnumber</option>";

}
echo "</select></td>
      <td> 
        <select name='session_dates$i' id='session_dates$i' multiple='multiple' size='4'>
          <option selected='selected'>Select Date(s)</option>";

$date = mktime(0,0,0);
for ($d=0; $d<365; $d++)
{
    $d1 = date('M d Y', $date);
    $d2 = date('M d Y', $date);
    echo "<option value='$d1$d'>$d2</option>\n";
    $date = strtotime('+1 days', $date);
}

        echo "</select></td>";
	}

 

below is currently is my insert statement...but currently gets the error message:

Cannot add or update a child row: a foreign key constraint fails (`prosched/prodates`, CONSTRAINT `prodates_ibfk_3` FOREIGN KEY (`session_course_id`) REFERENCES `courses` (`course_id`))

 

$dsessioncourse = $_POST["date_session_id"];
$citycourse = $_POST["session_city_id"];
$monthcourse = $_POST["session_month_year_id"];
$activecourse = $_POST["session_ative"];
$loop = count($_POST) / 2;

for ($i = 0; $i < $loop; $i++) {
if($_POST["session_course$i"]) $product[$i]["session_course"] = $_POST["session_course$i"];
}
for ($d = 0; $d < $loop; $d++) {
if ($_POST["$d1$d"]) $product[$d]["session_dates"] = $_POST["$d1$d"];
}
foreach ($product as $item) {
$course = $item["session_course"];
$sessiond = $item["session_dates"];


$query = mysql_query("INSERT INTO `prodates` (`date_session_id`, `session_month_year_id`, `session_dates`, `session_active`, `session_city_id`, `session_course_id`) 
VALUES ('$dsessioncourse', '$monthcourse', '$session_d', '$activecourse', '$citycourse', '$course')") or die(mysql_error()); 

}

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.