Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    34
    Thanks
    22
    Thanked 0 Times in 0 Posts

    multiple rows inserted into table

    Hello, I posted a thread some time ago regarding the same issue. 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. I'm still very new to php. But, I've been learning a lot in the last couple weeks but unfortunetly still have no results.

    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. 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 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 as needed.

    I'd like to represent the idea like this:
    FORM
    INPUT 1 INPUT 2 INPUT 3 INPUT 4 INPUT 5
    INPUT 1 INPUT 2 INPUT 3 INPUT 4 INPUT 5
    INPUT 1 INPUT 2 INPUT 3 INPUT 4 INPUT 5
    INPUT 1 INPUT 2 INPUT 3 INPUT 4 INPUT 5
    /FORM
    All rows of html identical, only the values would differ.
    Here's my html so far:
    Code:
    <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[0]" >
    <option value="other1">other1</option>
    <option value="other2">other1</option>
    <option value="other3">other3</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>
        </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>
        </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>
        </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>
      <tr>
        <td><select name="vendor[4]" id="vendor">
          <option value="other1">other1</option>
          <option value="other2">other1</option>
          <option value="other3">other3</option>
        </select></td>
        <td><input name="description[4]" type="text" id="description" size="50" /></td>
        <td><input name="quantity[4]" type="text" id="quantity" size="50" /></td>
        <td><input name="price[4]" type="text" id="price" size="50" /></td>
      </tr>
      <tr>
        <td><select name="vendor[5]" id="vendor">
          <option value="other1">other1</option>
          <option value="other2">other1</option>
          <option value="other3">other3</option>
        </select></td>
        <td><input name="description[5]" type="text" id="description" size="50" /></td>
        <td><input name="quantity[5]" type="text" id="quantity" size="50" /></td>
        <td><input name="price[5]" type="text" id="price" size="50" /></td>
      </tr>
    </table>
    <input type="submit" name="Submit" value="Submit" />
    </FORM>
    And here's the PHP file "post_multiple.php" that the html form calls on:
    PHP Code:
    <?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']; 
    $Submit 'Submit'

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

    // Check if button name "Submit" is active, do this  
    if($Submit){ 
    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(); 
    ?>
    This is my most current error message:
    Code:
    Notice: Use of undefined constant i - assumed 'i' in /var/www/multiple/post_multiple.php on line 28 Notice: Undefined index: i in /var/www/multiple/post_multiple.php on line 28
    Can anybody lend me some suggestions or tell me if I'm totally on the wrong track?? I'm a total noob and barely have a clue what I'm doing; I'm just trying really hard to learn and really want to make some cool stuff for myself and whoever else may want to use.

    I'd also love to have some links to reference materials anybody may think could help for this or any similar kind of application..

    Thanks a mil to anybody who reads this, even if you don't reply!!! I really appreciate it!!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    Like this?
    http://www.drasticdata.nl/DDDrasticGrid.php

    add rows, delete rows, edit rows ... AJAX, MySQL, PHP.

  • Users who have thanked mlseim for this post:

    thelung (02-08-2011)

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    34
    Thanks
    22
    Thanked 0 Times in 0 Posts
    that looks very neat. Is the code easy to learn from? I know almost nothing about AJAX and only very basic Javascript. I'm definetly going to d/l that and play around with it, though.

    Thanks for sharing the link. It looks like something I'll have fun with.

    To answer your question; yes, like that. Only where the user could fill in several lines at one time, then submit it to the database.

    I'm at work right now. I'm going to finish a report, catch up on a couple phone calls, d/l the code for that and play with it for a while.

    Thanks a mil buddy!!!

  • #4
    New Coder
    Join Date
    Sep 2010
    Posts
    34
    Thanks
    22
    Thanked 0 Times in 0 Posts
    wow, DrasticTools is some pretty neat stuff.. Thanks for sharing this link to me. For some reason I don't know, I probably would've never found it otherwise..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •