0

Still trying to get my head around submitting arrays and using Mysqli.

So on a form I want to have several inputs:

<form action='newmem2.php' method='post'>
  <input type="text" name="memname[1]"><br>
  <input type="text" name="memname[2]"><br>
  <input type="text" name="memname[3]"><br>
  <input type='submit' value='submit'>
</form>

then on newmem2.php I have:

$newmember = $_POST['memname'];
$sql = 'insert into members (name,active)values(?,?)';
$update = $db->prepare($sql);
   foreach($newmember as $memname => $val) {
     $update->bind_param('si', $val, '1');// the 1 indicates I want to force that
     entry.
     $update->execute();
   }    

the print_r($_post) revelas: Array ( [memname] => Array ( [1] => joe [2] => bob [3] => eric ) )

but there isn't anything submitted to the database. (yes I have a valid connection). Searches indicate usage of

Also, if I only have one name entered, how can I go about skipping the remaining fields so I am not submitting empties?

Bill Flippen
  • 453
  • 1
  • 4
  • 19
  • 1
    Turn up your error reporting. You can't use constant values (like `'1'`) in `bind_param`. You must use variables as they are passed by reference – Phil Nov 06 '14 at 06:23
  • @Phil turn up error reporting? how? – Bill Flippen Nov 06 '14 at 06:29
  • 1
    While developing, your `php.ini` file should have `error_reporting = E_ALL` and `display_errors = On`. You can also put `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before creating your database connection for better error reporting from the *mysqli* extension – Phil Nov 06 '14 at 06:31
  • @Phil the variable did the trick `$active=1` then `$update->bind_param('si', $val, $active)` now names are submitted! – Bill Flippen Nov 06 '14 at 06:32
  • @Phil thanks for the error reporting info. Any thoughts on how to handle empties? – Bill Flippen Nov 06 '14 at 06:36
  • try to output the aute generated sql insert statement and paste it on your terminal for debugging purpose.. about the skipping empty fields.. add is_empty condition before binding params and executing the code.. – Semi-Friends Nov 06 '14 at 06:39
  • @Semi-Friends `foreach($newmember as $memname => $val) { if (!empty($val) ){ $update->bind_param('si', $val, $active); $update->execute();}` did the trick, thanks – Bill Flippen Nov 06 '14 at 07:36

0 Answers0