Record sequence between 2 fields
Results 1 to 4 of 4

Thread: Record sequence between 2 fields

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    7

    Record sequence between 2 fields

    Hello, I'm back and would like everyone's help, how do I write in mysql a sequence of numbers coming from a form, like this:
    *
    *
    in form they would have 3 fields being name, start and end
    *
    field name: test
    *******start: 0001
    *********end: 0010
    *
    when writing to BD would look like

    +------+------+
    | name |test|
    +------+------+
    | test |0001|
    | test |0002|
    | test |0003|
    | test |0004|
    | test |0005|
    | test |0006|
    | test |0007|
    | test |0008|
    | test |0009|
    | test |0010|
    +------+------+

    I did so

    for ($i = (int)$_POST['start'];
    $i <= (int)$_POST['end']; $i++)
    echo "$i\n","<br>";

    But I could not do the INSERT INTO, please help me

  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    please

  3. #3
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    I don't understand your question. It seems you might need to look at some basic tutorials about how to read POST data. And you don't show us your SQL at all. You need to describe what you want to do in more detail, explain what you already tried (and show us the code!), and what happened (vs. what you expected to happen).

  4. #4
    Senile Member
    Join Date
    Oct 2002
    Location
    Cheshire, UK
    Posts
    1,660
    try
    Code:
    <?php
    $db = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
    
    $db->query("DROP TABLE IF EXISTS test_1");
    $db->query("CREATE TABLE test_1 (
        name VARCHAR(10),
        test INT(4) ZEROFILL,
        PRIMARY KEY (name,test)
    )");
    
    if (isset($_POST['start'])) {
    $start = $_POST['start'];
    $finish = $_POST['end'];
    $data = array();
    
    for ($i=$start; $i<=$finish; $i++) {
        $data[] = sprintf("('%s', %d)", 'test', $i);
    }
    $sql = "INSERT INTO test_1 VALUES\n" . join(",\n", $data);
    $db->query($sql);
    $sql = "SELECT name, test FROM test_1";
    echo query2HTMLtable($db,$sql);
    }
    else die("values required");
    ?>
    RESULTS
    Code:
    +------+------+
    | name | test |
    +------+------+
    | test | 0001 |
    | test | 0002 |
    | test | 0003 |
    | test | 0004 |
    | test | 0005 |
    | test | 0006 |
    | test | 0007 |
    | test | 0008 |
    | test | 0009 |
    | test | 0010 |
    +------+------+
    Last edited by barand; 12-24-2013 at 07:11 AM. Reason: added info
    Barand

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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