Select box and inserting into database

Discussion in 'PHP' started by baris22, Oct 19, 2009.

  1. #1
    I need some help. I have got 2 select boxes. First one is for amount and second one is displaying names from database. When i submit i want to enter into database chosen value times row.

    If chosen value is 2 at the select box I want to add two rows into database. How can i do this?

    This is my index.php
    
    
    <?
    include_once ("config/connect.php");
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form id="form" name="form" method="post" action="insert.php">
    <select name="select1" id="select1">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
    </select>
           of 
    <select name="select2" id="select2">
           <option value="Select">Select</option>
       <?	
        $query3 = "SELECT * FROM material";
    	$portfolio = mysql_query($query3);
        while($row3 = mysql_fetch_array($portfolio)) { 
       ?>     
        <option><?=$row3['material_name'];?></option>
        
       <? } ?>
    </select>
    <br />
    <br />
    <input type="submit" name="add" value="Add" onclick="return confirmPost()" />
    
    </form>
    </body>
    </html>
    
    
    PHP:
    and this is insert.php

    
    
    <?
    include_once ("config/connect.php");
    if (isset($_POST['add'])) 
    {
    	$select1 = $_POST['select1']; //amount
    	$select2 = $_POST['select2']; // name
    
    	$query = mysql_query("INSERT INTO item (item_id, item_amount, item_name) VALUES('', '".$select1."', '".$select2."')");
    	
    	$added="yes";
    		
    }		
    
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?=$select1?> of  <?=$select2?>
    
    <br />
    
    <? echo ($added=="yes")?"Added":""; ?>
    </body>
    </html>
    
    
    PHP:

     
    baris22, Oct 19, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    try following..

    
    for($i = 0; $i < $select1; $i++)
    {
    	$query = mysql_query("INSERT INTO item (item_id, item_amount, item_name) VALUES('', '".$select1."', '".$select2."')");
    }
    
    PHP:
    but i wonder why do you need to insert them twice? I mean, it will duplicate the rows without any unique identifier unless there is a purpose behind this..
     
    mastermunj, Oct 19, 2009 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thank you it worked perfect.

     
    baris22, Oct 19, 2009 IP