Jump to content

Multiple entries into database


tjverge

Recommended Posts

Hello,

 

For starters I'm not sure if what I want to do is possible, but if it is I would like your input. I have a script that will show a number of fields to fill out in a second form based on the number the user puts into the first from. the problem is that only the last one saves into the database and not all of them.

 

<form auction="index.php" method="post">
System Name: <input type="text" name="systemname">
Number of E-sites: <input type="text" name="events">
Number of Sigs: <input type="text" name="sigs"><br>
<input type="reset" name="reset" value="Reset">
<input type="submit" name="start" value="Start">
</form>
<form auction="index.php" method="post">
<?php
$events = $_POST['events'];
$system = $POST['systemname'];

if (isset($_POST['start'])) {
$num = $_POST['sigs'];
$i = 0;
While ($i < $num)
{
echo "Sig ID: <input type=text name=sigid>";
echo "Type: <input type=text name=type>";
echo "Name: <input type=text name=name>";
echo "Notes: <input type=text name=notes>";
echo "<br>";
$i++;
}
}
?>
<input type="submit" name="enter" value="Enter">
</form>
<?php
$sigid = $_POST['sigid'];
$type = $_POST['type'];
$name = $_POST['name'];
$notes = $_POST['notes'];
mysql_connect('xt', 'x', 'x');
mysql_select_db('wormhole');
if (isset($_POST['enter'])) {

$query = "INSERT INTO sites VALUES ('$system','$events','$sigid','$type','$name','$notes')";
mysql_query($query);

}
?>

 

How do I get it so all the data saves, lets say that $num = 5, I want all 5 to save not just the last one.

Link to comment
https://forums.phpfreaks.com/topic/219359-multiple-entries-into-database/
Share on other sites

You need to modify how the fields are given their names, in order to make the fields into arrays of fields.

 

if (isset($_POST['start'])) {
   $num = $_POST['sigs'];
   $i = 0;
   while ($i < $num) {
      echo "Sig ID: <input type=\"text\" name=\"fields[$i][sigid]\">\n";
      echo "Type: <input type=\"text\" name=\"fields[$i]['type']\">\n";
      echo "Name: <input type=\"text\" name=\"fields[$i]['name']\">\n";
      echo "Notes: <input type=\"text\" name=\"fields[$i]['notes']\">\n";
      echo "<br>\n";
      $i++;
   }
}

 

If you do it this way, all of the added fields will be in an array $_POST['fields'], with each set of fields in $_POST['fields'][0], $_POST['fields'][1], etc. Each of those arrays will hold the individual values, i.e. the print_r($_POST) would return

Array
(
    [fields] => Array
        (
            [0] => Array
                (
                    [sigid] => 
                    ['type'] => 
                    ['name'] => 
                    ['notes'] => 
                )

            [1] => Array
                (
                    [sigid] => 
                    ['type'] => 
                    ['name'] => 
                    ['notes'] => 
                )

            [2] => Array
                (
                    [sigid] => 
                    ['type'] => 
                    ['name'] => 
                    ['notes'] => 
                )

            [3] => Array
                (
                    [sigid] => 
                    ['type'] => 
                    ['name'] => 
                    ['notes'] => 
                )

            [4] => Array
                (
                    [sigid] => 
                    ['type'] => 
                    ['name'] => 
                    ['notes'] => 
                )
        )
)

this is what I now have for code

<hr><h1>Enter Data</h1><hr>
<form auction="index.php" method="post">
Number of Sigs: <input type="text" name="sigs"><br>
<input type="submit" name="start" value="Start">
</form>
<?php
echo "<form auction=index.php method=post>";
if (isset($_POST['start'])) {
$num = $_POST['sigs'];
$i = 0;
echo "System Name: <input type=text name=systemname>";
echo "Number of E-sites: <input type=text name=events><br>";
While ($i < $num)
{
echo "Sig ID: <input type=\"text\" name=\"fields[$i][sigid]\">\n";
      echo "Type: <input type=\"text\" name=\"fields[$i]['type']\">\n";
      echo "Name: <input type=\"text\" name=\"fields[$i]['name']\">\n";
      echo "Notes: <input type=\"text\" name=\"fields[$i]['notes']\">\n";
      echo "<br>\n";
      $i++;
   }
}

?>
<input type="submit" name="enter" value="Enter">
</form>
<?php
$system = $_POST['systemname'];
$events = $_POST['events'];
$sigid = $_POST['sigid'];
$type = $_POST['type'];
$name = $_POST['name'];
$notes = $_POST['notes'];
mysql_connect('xt', 'x', 'x');
mysql_select_db('wormhole');
if (isset($_POST['enter'])) {

$query = "INSERT INTO sites VALUES ('$system','$events','$sigid','$type','$name','$notes')";
mysql_query($query);
}
?>

Found spelling error, still have not been able to get it working

<form auction="index.php" method="post">
Number of Sigs: <input type="text" name="sigs"><br>
<input type="submit" name="start" value="Start">
</form>
<?php
echo "<form auction=index.php method=post>";
if (isset($_POST['start'])) {
$num = $_POST['sigs'];
$i = 0;
echo "System Name: <input type=text name=systemname>";
echo "Number of E-sites: <input type=text name=events><br>";
While ($i < $num)
{
echo "Sig ID: <input type=\"text\" name=\"fields[$i][sigid]\">\n";
      echo "Type: <input type=\"text\" name=\"fields[$i]['type']\">\n";
      echo "Name: <input type=\"text\" name=\"fields[$i]['name']\">\n";
      echo "Notes: <input type=\"text\" name=\"fields[$i]['notes']\">\n";
      echo "<br>\n";
      $i++;
   }
}

?>
<input type="submit" name="enter" value="Enter">
</form>
<?php
$system = $_POST['systemname'];
$events = $_POST['events'];
$sigid = $_POST['fields[]']['sigid'];
$type = $_POST['fields[]']['type'];
$name = $_POST['fields[]']['name'];
$notes = $_POST['fields[]']['notes'];
mysql_connect('localhost', 'root', 'eagle5');
mysql_select_db('wormhole');
if (isset($_POST['enter'])) {

$query = "INSERT INTO sites VALUES ('$system','$events','$sigid','$type','$name','$notes')";
mysql_query($query);
}
?>

Any ideas or feedback are welcome

This should do the trick for you. I didn't put in any validation, so if there are empty fields, they'll be inserted in the database as well. Inserts a complete record for each set of form fields. I didn't test it, but it should work.

 

if (isset($_POST['start'])) {
   
mysql_connect('xt', 'x', 'x');
mysql_select_db('wormhole');
$_POST = array_map('mysql_real_escape_string', $_POST);
$events = $_POST['events'];
$system = $_POST['systemname'];

   foreach( $_POST['fields'] as $v ) {
      $query = "INSERT INTO `table` VALUES ('$system', '$events', '" . implode('\', \'', $v) . "')";
      mysql_query($query);
   }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.