Jump to content

Inserting multiple rows into table


thelung

Recommended Posts

Hi, I'm very new to these forums and php alike.  I've been working on this bit of code for weeks now.  I've written numerous versions and made endless revisions; trying pieces from many similiar source and well, it's still not quite working out for me.

 

The form I've been trying to finish will have multiple rows. The user will input values specific to each row, but I'm trying to make it so that each row is synonymous with each other in regard to the database table. Like an order form, an inventory form, or a roster. I'd really like to make this code work out because I can think of numerous applications for it's use. I'm sure others could use it for even more.

 

So, as you see; the html code represents the table that contains the form. Currently, I only have five of these rows in the form, but would like for anybody using the code to be able to add in code for as many rows in the table as needed.

<html>

<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<FORM action="post_multiple.php" method="POST">
<table width="80%" border="1">
  <tr>
    <td>Vendor</td>
    <td>Item Description</td>
    <td>Quantity</td>
    <td>Price</td>
  </tr>
  <tr>
    <td><select name="vendor" id="vendor" >
<option value="other1">other1</option>
<option value="other2">other1</option>
<option value="other3">other3</option>
<option value="other4">other4</option>
<option value="other5">other5</option>
</select></td>
    <td><input name="description" type="text" id="description" size="50" /></td>
    <td><input name="quantity" type="text" id="quantity" size="50" /></td>
    <td><input name="price" type="text" id="price" size="50" /></td>
  </tr>
  <tr>
    <td><select name="vendor" id="vendor">
      <option value="other1">other1</option>
      <option value="other2">other1</option>
      <option value="other3">other3</option>
      <option value="other4">other4</option>
      <option value="other5">other5</option>
    </select></td>
    <td><input name="description" type="text" id="description" size="50" /></td>
    <td><input name="quantity" type="text" id="quantity" size="50" /></td>
    <td><input name="price" type="text" id="price" size="50" /></td>
  </tr>
  <tr>
    <td><select name="vendor" id="vendor">
      <option value="other1">other1</option>
      <option value="other2">other1</option>
      <option value="other3">other3</option>
      <option value="other4">other4</option>
      <option value="other5">other5</option>
    </select></td>
    <td><input name="description" type="text" id="description" size="50" /></td>
    <td><input name="quantity" type="text" id="quantity" size="50" /></td>
    <td><input name="price" type="text" id="price" size="50" /></td>
  </tr>
  <tr>
    <td><select name="vendor" id="vendor">
      <option value="other1">other1</option>
      <option value="other2">other1</option>
      <option value="other3">other3</option>
      <option value="other4">other4</option>
      <option value="other5">other5</option>
    </select></td>
    <td><input name="description" type="text" id="description" size="50" /></td>
    <td><input name="quantity" type="text" id="quantity" size="50" /></td>
    <td><input name="price" type="text" id="price" size="50" /></td>
  </tr>
  <tr>
    <td><select name="vendor" id="vendor">
      <option value="other1">other1</option>
      <option value="other2">other1</option>
      <option value="other3">other3</option>
      <option value="other4">other4</option>
      <option value="other5">other5</option>
    </select></td>
    <td><input name="description" type="text" id="description" size="50" /></td>
    <td><input name="quantity" type="text" id="quantity" size="50" /></td>
    <td><input name="price" type="text" id="price" size="50" /></td>
  </tr>
  <tr>
    <td><select name="vendor" id="vendor">
      <option value="other1">other1</option>
      <option value="other2">other1</option>
      <option value="other3">other3</option>
      <option value="other4">other4</option>
      <option value="other5">other5</option>
    </select></td>
    <td><input name="description" type="text" id="description" size="50" /></td>
    <td><input name="quantity" type="text" id="quantity" size="50" /></td>
    <td><input name="price" type="text" id="price" size="50" /></td>
  </tr>
</table>
<input type="submit" name="Submit" value="Submit" />
</FORM>

</body>
</html>

 

In the php script I've tried using the count, loop, if, several others functions to try to get it to insert all the completed rows from the form into the table but just can't seem to get it to work out.  Anyways, here's a representation of what i'd like to achieve.  Not necessarily the code I've written trying to accomplish this, I'd have to upload a book if that were the case.

<?php 
ini_set('display_errors','On'); 
error_reporting(E_ALL); 

$host="localhost"; // Host name  
$username="root"; // Mysql username  
$password="password"; // Mysql password  
$db_name="testing"; // Database name  
$tbl_name="test_multiple"; // Table name 

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect");  
mysql_select_db("$db_name")or die("cannot select DB"); 

//declarations 
$vendor = $_POST['vendor']; 
$description = $_POST['description']; 
$quantity = $_POST['quantity']; 
$price = $_POST['price']; 

// Check if button name "Submit" is active, do this  
$sql1="INSERT INTO $tbl_name (vendor, description, quantity, price) VALUES ('".$vendor."', '".$description."', '".$quantity."', '".$price."')"; 
$result1=mysql_query($sql1); 

mysql_close(); 
?>

 

If anybody can suggest where I need to go with that, that would be great.  Like I said above, I've tried about everything I could find any reason to from forum threads I had found in google searches and whatnot.  Also, if there is a good article, link or tutorial that would address this that anybody knows about that would be great.  However, I'm not looking for a link to a product or plugin that will do this stuff for me.  I'm trying to get halfway decent at php and would like to have working code for myself to build upon, modify, and learn from.  Any help is greatly appreciated!!!!

Link to comment
Share on other sites

One of the codes I had put together to address that issue was like this:

html:

<html>

<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<FORM action="post_multiple.php" method="POST">
<table width="80%" border="1">
  <tr>
    <td>Vendor</td>
    <td>Item Description</td>
    <td>Quantity</td>
    <td>Price</td>
  </tr>
  <tr>
    <td><select name="vendor[0]" id="vendor" >
<option value="other1">other1</option>
<option value="other2">other1</option>
<option value="other3">other3</option>
<option value="other4">other4</option>
<option value="other5">other5</option>
</select></td>
    <td><input name="description[0]" type="text" id="description" size="50" /></td>
    <td><input name="quantity[0]" type="text" id="quantity" size="50" /></td>
    <td><input name="price[0]" type="text" id="price" size="50" /></td>
  </tr>
  <tr>
    <td><select name="vendor[1]" id="vendor">
      <option value="other1">other1</option>
      <option value="other2">other1</option>
      <option value="other3">other3</option>
      <option value="other4">other4</option>
      <option value="other5">other5</option>
    </select></td>
    <td><input name="description[1]" type="text" id="description" size="50" /></td>
    <td><input name="quantity[1]" type="text" id="quantity" size="50" /></td>
    <td><input name="price[1]" type="text" id="price" size="50" /></td>
  </tr>
  <tr>
    <td><select name="vendor[2]" id="vendor">
      <option value="other1">other1</option>
      <option value="other2">other1</option>
      <option value="other3">other3</option>
      <option value="other4">other4</option>
      <option value="other5">other5</option>
    </select></td>
    <td><input name="description[2]" type="text" id="description" size="50" /></td>
    <td><input name="quantity[2]" type="text" id="quantity" size="50" /></td>
    <td><input name="price[2]" type="text" id="price" size="50" /></td>
  </tr>
  <tr>
    <td><select name="vendor[3]" id="vendor">
      <option value="other1">other1</option>
      <option value="other2">other1</option>
      <option value="other3">other3</option>
      <option value="other4">other4</option>
      <option value="other5">other5</option>
    </select></td>
    <td><input name="description[3]" type="text" id="description" size="50" /></td>
    <td><input name="quantity[3]" type="text" id="quantity" size="50" /></td>
    <td><input name="price[3]" type="text" id="price" size="50" /></td>
  </tr>
</table>
<input type="submit" name="Submit" value="Submit" />
</FORM>

</body>
</html>

 

And meanwhile trying to use a loop function to grap each row something like this:

<?php  

$host="localhost"; // Host name   
$username="root"; // Mysql username   
$password="password"; // Mysql password   
$db_name="testing"; // Database name   
$tbl_name="test_multiple"; // Table name  

// Connect to server and select databse.  
mysql_connect("$host", "$username", "$password")or die("cannot connect");   
mysql_select_db("$db_name")or die("cannot select DB");  

//declarations  
$vendor[] = $_POST['vendor'];  
$description[] = $_POST['description'];  
$quantity[] = $_POST['quantity'];  
$price[] = $_POST['price'];  

// declare your limit for the loop  
$limit = count($description);  

//count up the descriptions and hopefully insert them into the table
for($i=0;$i<$limit;$i++){  
$sql1="INSERT INTO $tbl_name (vendor, description, quantity, price) VALUES ('".$vendor[$i]."', '".$description[$i]."', '".$quantity[$i]."', '".$price[i]."')";  
$result1=mysql_query($sql1);  
}  

mysql_close();  
?>


 

Link to comment
Share on other sites

I would generate the form programatically, and group the elements together in arrays. It would end up something likethis, but I'd probably structure it differently to separate the logic from the display.

 

<html>
<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<FORM action="post_multiple.php" method="POST">
<?php echo '<pre>'; print_r($_POST); echo '</pre>'; ?>
<table width="80%" border="1">
  <tr>
    <td>Vendor</td>
    <td>Item Description</td>
    <td>Quantity</td>
    <td>Price</td>
  </tr>
<?php
for( $i = 1; $i < 6; $i++ ) {
echo "<tr><td><select name=\"set{$i}[vendor]\" id=\"vendor{$i}\">\n";
$opts = range(1, 5);
foreach( $opts as $val ) {
	echo "<option value=\"other{$val}\">other{$val}</option>\n";
}
echo "</select>\n</td>\n
<td><input name=\"set{$i}[description]\" type=\"text\" id=\"description{$i}\" size=\"50\" /></td>\n
<td><input name=\"set{$i}[quantity]\" type=\"text\" id=\"quantity{$i}\" size=\"50\" /></td>\n
<td><input name=\"set{$i}[price]\" type=\"text\" id=\"price{$i}\" size=\"50\" /></td>\n
</tr>\n";
}
?>
</table>
<input type="submit" name="Submit" value="Submit" />
</FORM>

</body>
</html>

Link to comment
Share on other sites

Which would organize each group nicely into its own array.

 

// print_r($_POST); returns:

Array
(
    [set1] => Array
        (
            [vendor] => other1
            [description] => desc1
            [quantity] => qua1
            [price] => price1
        )

    [set2] => Array
        (
            [vendor] => other2
            [description] => desc2
            [quantity] => qua2
            [price] => pri2
        )

    [set3] => Array
        (
            [vendor] => other3
            [description] => desc3
            [quantity] => qua3
            [price] => pri3
        )

    [set4] => Array
        (
            [vendor] => other4
            [description] => desc4
            [quantity] => qua4
            [price] => pri4
        )

    [set5] => Array
        (
            [vendor] => other5
            [description] => desc5
            [quantity] => qua5
            [price] => pri5
        )

    [submit] => Submit
)

Link to comment
Share on other sites

thanks!!  I like the way that generates the form.  A lot easier than coding out a whole bunch of table!!

 

 

A problem I immediately spot is you can't have multiple form fields with the same name. If you do, only the value of the last one in the form will be present in the $_POST array.

So with them having the same name they won't post?  Do you think there would be a way I could put the declarations into a loop or something and pull it off??  :shrug:  Or do you think the ultimate purpose of this project is kind of impossible using php???  I'm definetly beginning to think that.. 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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