0

I have 3 PHP files, in select.php file you have to select the number of forms that you want.. the form contain 3 input fields. as below:

  <form method="post" action="index.php" >
     Continue insertion with <select name="counters" id="insert_rows">
    <option value="1">1</option>
    <option value="2" selected="selected">2</option>
    <option value="3">3</option>
    </select>
     rows
    <input type="submit" value="Go" />
    </form>

After selecting the number of insertions, it will pass the number to index.php which is contain:

<?php
    echo "<form method = 'POST' action = 'process.php'>";
    for($counter = 0; $counter < $_POST['counters']; $counter++)
    {
        echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
        echo "Service Version :    <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
        echo "Service type :    <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
    }
    echo "<input type = 'submit' value = 'SEND'/>";
    echo "</form>";
?>

the third file process.php contain

<?php

    $name = array();
    $version = array();
    $type= array();

    $name = $_POST['name'];
    $version = $_POST['version'];
    $type= $_POST['type'];

    for($counter = 0; $counter < sizeof($name); $counter++)
    {
        echo "service_name #".($counter + 1).": ".$name[$counter]."<br />";
        echo "service_version #".($counter + 1).": ".$version[$counter]."<br />";
        echo "service_type #".($counter + 1).": ".$type[$counter]."<br />";
    }
    ?>

and after i pass the value to it, it shows the data correctly as below

service_name #1: noway service_version #1: v1 service_type #1: Private service_name #2: bandar service_version #2: v2 service_type #2: Public

so my question is : how to create a function in 'process.php' file to insert all these values into the database.

I created a table called 'services' contain these columns "name,version,type"

I will be waiting for your support. thank you

5
  • var_dump($_POST) will tell you what you're getting in PHP. From there it's straight forward: grab some values, stuff them into the db. repeat until done. Commented May 7, 2014 at 19:07
  • Use a similar for loop that you use to build your form -> for($counter = 0; $counter < $_POST['name']; $counter++). For each form input, use the $counter -> $_POST['name'][$counter],$_POST['version'][$counter],$_POST['type'][$counter] Commented May 7, 2014 at 19:11
  • 1.) Learn how to write a simple INSERT query. 2.) Learn how to use mysqli or PDO. 3.) Learn how to handle form submissions in PHP. 4.) IF AND ONLY IF you are getting errors or are stuck on a specific problem, ask here. No one here is going to teach you how to do all of that. Commented May 7, 2014 at 19:13
  • @PatrickQ Miss Patrick , first of all thanks for your replying.. 1) I know how to write a simple INSERT query "if you need I can teach you" 2) i Know how to use mysqli :) 3) I know how to handle forms in PHP. 4) I will not reply to you since we are not using your own website.. so I will give you one advice "Teach your self how to improve your communication skills " . BR Commented May 7, 2014 at 19:24
  • 1
    Your original post (pre-update) showed no attempt whatsoever to do any of those mentioned steps. You simply ask how to write a function "to insert all these values into the database." That is a very broad request that involves many parts. And even after the update, you still show no attempt to insert the values. So instead of just saying that you know how to do all of those things, you should show us by making an attempt to solve this yourself. Commented May 7, 2014 at 19:39

1 Answer 1

0

I would recommend to save the first submitted count value in an hidden input in the second form, then you will be able to loop through that and insert your data

<?php
  echo "<form method = 'POST' action = 'process.php'>";
  for($counter = 0; $counter < $_POST['counters']; $counter++)
  {
     echo "Service Name: <input type = 'text' name = 'name[]' class = 'class_name'/><br/>";
     echo "Service Version :    <input type = 'text' name = 'version[]' class = 'class_name'/><br/>";
     echo "Service type :    <input type = 'text' name = 'type[]' class = 'class_name'/><br/>";
  }
  echo '<input type="hidden" name="cntr" value="'.$_POST['counters'].'" />'
  echo "<input type = 'submit' value = 'SEND'/>";
  echo "</form>";
?>

Now in your process.php you will have:

if (!empty($_POST['cntr'])) {
   $query = 'INSERT INTO services (name, version, type) VALUES ';
   for ($x = 0; $x < $_POST['cntr']; $x++)
   {
      $query .= "('".mysqli_escape_string($_POST['name'][$x])."','"
                    .mysqli_escape_string($_POST['version'][$x])."','"
                    .mysqli_escape_string($_POST['type'][$x]).
                "'),";
   }
   $query = substr_replace($query, '', -1);

   mysqli->query($query);
}

Whatever I prepared is just a sample you can modify it base on your changes

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.