Search the Community
Showing results for tags 'implode'.
-
Hello, I've googled this thing to death, but still having problems. I'm trying to insert multiple rows into Mysql with one INSERT INTO query (line 39) using an array and the implode(). The user can select multiple qualifications for one employee. I have the multiple select listing all the qualifications from the quals table on the db. Another select table is listing all the employee names from the employee table. Each employee can have multiple qualifications. I would like the qual_id, employee_id, issue_date, and expiration_date inserted into emp-quals table. The following error keeps appearing: Array ( [qual_id] => 3 [employee_id] => 13 [issue_date] => [expiration_date] => [Submit] => Submit [/] => ) An error occurred in script 'C:\wamp\www\****\set_qual.php' on line 36: Uninitialized string offset: 0 And Column count doesn't match value count at row 1 Query: INSERT INTO emp_quals(qual_id, employee_id, issue_date, expiration_date) VALUES ('("3", 3, 3, 3")', '("1", 1, 1, 1")', '("", , , ")', '("", , , ")', '("S", S, S, S")', '("", , , ")') It seems to be close to working, but I'm not sure how to tweak it. Here is the entire code: <?php #Ny Kvalifikasjon for Ansatte. require('includes/config.inc.php'); $page_title = 'Ny Kvalifikasjon for ansatte!'; include ('includes/header.html'); // Create the submission conditional and initialize the $errors array. if ($_SERVER['REQUEST_METHOD'] == 'POST'){ // Add the MySQL connection include(MYSQL); $errors = array(); $qual_id = mysqli_real_escape_string($dbc, trim($_POST['qual_id'])); $employee_id = mysqli_real_escape_string($dbc, trim($_POST['employee_id'])); $issue_date = mysqli_real_escape_string($dbc, trim($_POST['issue_date'])); $expiration_date = mysqli_real_escape_string($dbc, trim($_POST['expiration_date'])); if (empty($_POST['qual_id'])) { $errors[] = 'Kvalifikasjonen mangler!'; } else { $qual_id = mysqli_real_escape_string($dbc, trim($_POST['qual_id'])); } if (empty($_POST['employee_id'])) { $errors[] = 'Ansatte mangler!'; } else { $employee_id = mysqli_real_escape_string($dbc, trim($_POST['employee_id'])); } if (empty($errors)) { print_r($_POST); //Added print_r tip from margaux at L.U.forum post: http://www.larryullman.com/forums/index.php?/topic/2131-103-edit-user-script-modification/ $values = array(); foreach($_POST as $row ) { $values[] = '("'.mysql_real_escape_string($row['qual_id']).'", '.$row['employee_id'].', '.$row['issue_date'].', '.$row['expiration_date'].'")'; } $q = "INSERT INTO emp_quals(qual_id, employee_id, issue_date, expiration_date)"; $q .= " VALUES ('".implode("', '", $values)."')"; $r = @mysqli_query ($dbc, $q); if ($r) { //See pg 276 echo '<h1>Success!</h1> <p>En ny kvalifikasjon er lagret.</p> <p><a href="qualification.php">Gå til kvalifikasjons oversikt.</a><br /></p>'; } else { echo '<h1>System Error</h1> <p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>'; } // End of if ($r) IF. mysqli_close($dbc); include ('includes/footer.html'); exit (); }else { // Report the errors echo '<h1>Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error messages echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } // End of if (empty($errors)) IF. }// End of main submit conditional. //DISPLAY THE FORM ?> <h1>Ny Kvalifikasjon for Ansatte</h1> <form action="set_qual.php" method="post"> <fieldset> <legend>Ny Kvalifikasjon for Ansatte</legend> <label>Qualification:</label><select multiple name="qual_id">'; <?php include(MYSQL); $q = "SELECT qual_id, qual_name FROM quals ORDER BY qual_id ASC"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) > 0) { while ($menu_row = mysqli_fetch_array($r, MYSQLI_NUM)) { echo '<option value="'.$menu_row[0].'">'.$menu_row[0].' - ' . $menu_row[1] . '</option>\n'; } } mysqli_free_result($r); echo '</select><br /><small>Bruk Ctrl knappen for å velge flere.</small><br />'; ?> <label>Employee Name:</label><select name="employee_id">'; <?php $q = "SELECT employee_id, ansattnumber, last_name, first_name FROM employee ORDER BY ansattnumber ASC"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) > 0) { while ($menu_row = mysqli_fetch_array($r, MYSQLI_NUM)) { echo '<option value="'.$menu_row[0].'">' . $menu_row[1] . ' - ' .$menu_row[2] .', '. $menu_row[3]. '</option>\n'; } } mysqli_free_result($r); echo '</select><br />'; ?> <br /> <label>Start dato yyyy-mm-dd:</label><input type="text" name="issue_date" size="15" maxlength="15" value="<?php if(isset($_POST['issue_date'])) echo $_POST['issue_date']; ?>" /><br /><br /> <label>Utløpsdato yyyy-mm-dd:</label><input type="text" name="expiration_date" size="15" maxlength="15" value="<?php if(isset($_POST['expiration_date'])) echo $_POST['expiration_date']; ?>" /><br /><br /> <p><input type="submit" name="Submit" value="Submit" class="button" /></p> <input type="hidden" name= /> </fieldset> </form> <?php include ('includes/footer.html'); ?> Any assistance would be appreciated.