php - How to process multiple html inserts? -


i'm trying create multiple html inserts in same form can insert multiple lines database save time. i'm not sure how process this.

<form action="admin1.php" method="post"> <?php  function multiform($x){     ($x = 0; $x < 3; $x++){         echo 'episode: <input type="number" name="episode[]">         date: <input type="date" name="date[]">         guest: <input type="text" name="guest[]">         type: <input type="text" name="type[]">         youtube:<input type="text" name="youtube[]"> mp3: <input type="text" name="mp3[]"> itunes:<input type="text" name="itunes[]"><br/><br/>';     } }  multiform(0);  ?> <input type="submit" value="submit form" name="submitform"> </form> 

this tried use:

$con = mysqli_connect("server","root","","database");  function multiformpost($x) {         ($x = 0; $x < 3; $x++)  {          $episode = $_post['episode'][$x];         $date = $_post['date'][$x];          $guest = $_post['guest'][$x];         $type = $_post['type'][$x];          $youtube = $_post['youtube'][$x];         $mp3 = $_post['mp3'][$x];         $itunes = $_post['itunes'][$x];            $sql = "insert podcasts(episode, date, guest, type, youtube, mp3, itunes) values ('{$episode}', '{$date}', '{$guest}', '{$type}', '{$youtube}', '{$mp3}', '{$itunes}')";     }     if (mysqli_connect_errno()) {     echo "failed connect mysql: " . mysqli_connect_error();      if (!mysqli_query($con, $sql)) {     die ('error: ' . mysqli_error($con));     }     echo "added database";     } }  multiformpost(0);  mysqli_close($con); 

which returns blank screen.. know it's wrong i'm not entirely sure why.

you need building values section of sql in loop , executing single query. this:

$con = mysqli_connect("","","",""); if (mysqli_connect_errno()) {     echo "failed connect mysql: " . mysqli_connect_error(); } multiformpost($con); mysqli_close($con);  function multiformpost($db) {     if(empty($db) {         throw new exception('you need pass valid mysqli connection method');     }      $sql = "insert podcasts(episode, date, guest, type, youtube, mp3, itunes) values ";     $size = count($_post['episode']);     ($x = 0; $x < $size; $x++)  {          $episode = mysqli_real_escape_string($db,$_post['episode'][$x]);         $date = mysqli_real_escape_string($db,$_post['date'][$x]);          $guest = mysqli_real_escape_string($db,$_post['guest'][$x]);         $type = mysqli_real_escape_string($db,$_post['type'][$x]);          $youtube = mysqli_real_escape_string($db,$_post['youtube'][$x]);         $mp3 = mysqli_real_escape_string($db,$_post['mp3'][$x]);         $itunes = mysqli_real_escape_string($db,$_post['itunes'][$x]);            $sql .= "('{$episode}', '{$date}', '{$guest}', '{$type}', '{$youtube}', '{$mp3}', '{$itunes}'),";     }     $sql = rtrim($sql,',');      if (!mysqli_query($db, $sql)) {         die ('error: ' . mysqli_error($db));     }      echo "added database"; } 

note made following changes suggest:

  • i pass in db connection function. have no idea original parameter being used for, since can detect array size of post arrays directly in function. better served moving object-oriented mysqli usage (as verify instantiate mysqli object passed function), didn't make change here.
  • i differentiated use of $con (for global scope) , $db (for local sope in function) not confuse two. previously, code referenced $con inside function scope without declaring global variable not have been available. dependency injection approach highly recommended opposed using global.
  • i moved db connection error checking outside function
  • i added string escaping mitigate against sql injection.
  • i moved global script elements together, functions typically should not inserted in middle of procedural code have done, make code more difficult follow.

Comments

Popular posts from this blog

jquery - isAjaxRequest always return false -

php - SPIP: From Tag directly to an article -