简体   繁体   中英

Inserting an array to mysql using php

i have this two text fields that ask the user to put in two numbers that are limited to 49 numbers, so that i can have an array of number 1 to 50, or 151 to 200, or 27551 to 27600 any number but a series of 49 consecutive numbers, my problem is i dont know how to put them inside the database, i have no clue i have been searching for everything about inserting arrays but they dont work on my case,

This is my form

 <form id="form3" name="form1" method="post" action="">
    <p>From:
    <input type="text" name="from" id="form_number" class="from" /> 
    - To:
    <input type="text" name="to" id="form_number" class="to" />
    </p>
    <p>Waybill Booklet:
    <select name="waybill_booklet[]" id="form_list">
        <?php
            do {  
        ?>
        <option value="<?php echo $row_Booklet['id_waybill_booklet']?>"><?php echo $row_Booklet['booklet_no']?></option>
        <?php
            } while ($row_Booklet = mysql_fetch_assoc($Booklet));
            $rows = mysql_num_rows($Booklet);
            if($rows > 0) {
            mysql_data_seek($Booklet, 0);
            $row_Booklet = mysql_fetch_assoc($Booklet);
            }
        ?>
    </select>
    </p>
    <p>
    <input type="hidden" name="status[]" value="4" />
    <input type="submit" name="button" id="form_button" value="OK!" />
    </p>
</form>

the 49 series of consecutive numbers will be inserted into the database with a foreign key what is chosen from the drop down menu, and a value of 4 that is in the hidden field, so basically there are 4 columns to my table 1 for primary key 1 for the series of numbers and 1 for the foreign key and the last will be the value of the numbers.

This is my php code to get the series of numbers

<?php
$booklet = $_POST['waybill_booklet'];
$status = $_POST['status'];
$from = $_POST['from'];
$to = $_POST['to'];
$number = range($from,$to);

$count = 0;
$myArray = range($from,$to);
while($count<=49){
if($count<49){
    echo $myArray[$count]. ", ";
}else{
    echo $myArray[$count];
}
$count++;
}
?>

i dont know how to insert the data's

Instead of storing this as an array (since you want to store this as bulk, I assume it will not involve any direct database level aggregation or computation), you can store it as a json string using the json_encode($myArray_series_of_numbers) . This gives you the flexibility to store them as a string column and when you retrieve it back, you can use json_decode($model->series_of_numbers_column,true) to get it back as an array for easy computation back in PHP.

Hope this helps

$waybill = mysql_real_escape_string($_POST['waybill_booklet'][0]);
$status = mysql_real_escape_string($_POST['status'][0]);
foreach (range($from, $to) as $number) {
    $sql = "INSERT INTO yourTable (id, waybill, status) VALUES($number, '$waybill', '$status')");
    mysql_query($sql) or die(mysql_error());
}

You should also switch to PDO or mysqli, so you can use parametrized queries instead of substituting strings into the query. Then you don't need to escape the values like that.

Here is a tutorial on using mysql in php http://www.w3schools.com/php/php_mysql_insert.asp specifically the INSERT command. just build your data into variables instead of echo'ing it and then follow the guide to interact with a database

here is the auto increment tutorial to generate primary ids for each array element http://www.w3schools.com/sql/sql_autoincrement.asp

you can greatly increase the speed of the inserts and do it in one submit by building a multiple insert sql string.. and then using the insert guide above to run it.

INSERT INTO Table ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM