multiple row inserts?

Discussion in 'MySQL' started by frobak, Nov 6, 2010.

  1. #1
    Hi, im having trouble with a mysql insert, please help!

    I want to insert multiple rows into a database table. There are only 2 columns in the table - 'id' and 'used'.

    I want the id field to auto increment

    I want the used column to all have 'NO' inserted.

    I want the amount of rows to be specified by user input to a form.

    Ive tried to use the limit function but im getting an error. Im thinking Limit only works on selects?

    Can anyone shed any light on this.

    This is what i tried:
    	
    if (isset($_POST['submit'])) {
    	
    			$code_qty = $_POST['code_qty'];
    	
    			include("includes/db_connect.inc.php");
    			
    			$sql = " INSERT INTO single_promo SET
    			used = 'NO' LIMIT=$code_qty";
    
    PHP:
     
    frobak, Nov 6, 2010 IP
  2. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #2
    LIMIT, SET and INSERT aren't meant to be used together - you've totally misunderstood something. What exactly are you trying to achieve ?
     
    ActiveFrost, Nov 6, 2010 IP
  3. ustas

    ustas Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    at first in mysql create AUTO_INCREMENT field (id)
    then, two way
    - in php create a cycle, in each iteration insert new row in table (insert into table (field_name) values ("NO"))
    - in mysql (not best way :) ), you can create script for multiple row inserts
    insert into table (field_name) values
    ("NO"),
    ("NO"),
    ...
    ("NO"),
    ("NO");
     
    ustas, Nov 6, 2010 IP
  4. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    i just want to insert multiple rows into a table, automatically dependant on how many rows the user wants to insert
     
    frobak, Nov 6, 2010 IP
  5. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #5
    thanks ustas, php cycle seems a good idea. Ill give that a go!
     
    frobak, Nov 6, 2010 IP
  6. amrox

    amrox Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The way I would do it is a standard insert statement within a php FOREACH loop
     
    amrox, Nov 6, 2010 IP
  7. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #7
    this worked a treat!

    
    		$code_qty = $_POST['code_qty'];
    		$i=0;
    
    		while ($i <= $code_qty) {
    		
    			$sql = "INSERT INTO single_promo SET
    			used = 'NO'";
    			
    			$result = @mysql_query($sql,$connection) or die(mysql_error());
    			
    			$i++;
    		}
    
    PHP:
    cheers for you input guys!
     
    frobak, Nov 6, 2010 IP