Inserting Many Rows At The Same Time

Discussion in 'PHP' started by coder 525, Jul 31, 2008.

  1. #1
    I want to insert multiple records to the database at the same time.
    First I need to enter the number of records to be inserted.
    Depending on the number of records to be inserted
    <input type="text">
    should come
    After entering all records , they need to be inserted in the database.
    I am looking for an option that can be done using single PHP page. Is it possible?
    Any help !
     
    coder 525, Jul 31, 2008 IP
  2. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you using MySQL or PG... MySQL, I guess.

    the mysql_query() function handles only one query at once, so either insert the records using one query or use multiple mysql_query();

    Link: http://us2.php.net/mysql_query
     
    selling vcc, Aug 1, 2008 IP
  3. coder 525

    coder 525 Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am thinking about using for loop in php with single querry. But I could not so far. I am posting the code here

    <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    <label>Number of rows to be inserted
    <input type="text" name="num_rec" />
    </label>
    <label>
    <input type="submit" name="display" value="Display" />
    </label>
    <p>&nbsp;</p>
    </form>
    <form name="form2" enctype="multipart/form-data" method="post" action="">

    <?php
    $num_rec = $_POST['num_rec'];

    for($x=0;$x<$num_rec;$x++){
    ?>
    <input type="text" name="check" />

    <?
    // end of for loop
    }
    ?>
    <p>
    <input type="submit" name="Insert" value="Insert">
    </p>
    </form>

    <?php
    $db = mysql_connect ( "" , "" , "" )or die('Cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("");
    foreach ($_POST['check'] AS $value) {
    $query=mysql_query("INSERT INTO tb_signup (userid) VALUES ('$value')");
    }
    mysql_close($db);
    ?>
     
    coder 525, Aug 1, 2008 IP
  4. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What do you get when running this script?

    note: the script has vulnerabilities...
     
    selling vcc, Aug 1, 2008 IP