简体   繁体   English

多个文本输入到MySQL数据库

[英]Multiple Text inputs into MySQL Database

In my HTML file I have the following to take input in the following format. 在我的HTML文件中,以下内容以以下格式进行输入。

Stuff1,Stuff2,Stuff3,Stuff4 

<form action="process_form.php" method="post">
    <form>
     Part 1: <input type="text" name="p1" /><br />
     Part 2: <input type="text" name="p2" /><br />
     Part 3: <input type="text" name="p3" /><br />
     Part 4: <input type="text" name="p4" /><br />
     Part 5: <input type="text" name="p5" /><br />
     Part 6: <input type="text" name="p6" /><br />
     Part 7: <input type="text" name="p7" /><br />
     Part 8: <input type="text" name="p8" /><br />
     Part 9: <input type="text" name="p9" /><br />
     Part 10: <input type="text" name="10" /><br />
    </form>

    <input type="submit" name="formSubmit" value="Submit" />

</form>

From there, I am using explode in my php file to separate at the comma and create an array from my string. 从那里开始,我在php文件中使用explode以逗号分隔并从字符串创建数组。

$create_table1 = "create table parts(qty int(5) NOT NULL,
partID int(5) NOT NULL PRIMARY KEY,
partname varchar(25) NOT NULL,
price int(5) NOT NULL
)";

     $p1_boom = explode(",",$p1); 
     $p2_boom = explode(",",$p2);
     $p3_boom = explode(",",$p3);
     $p4_boom = explode(",",$p4);
     $p5_boom = explode(",",$p5);
     $p6_boom = explode(",",$p6);
     $p7_boom = explode(",",$p7);
     $p8_boom = explode(",",$p8);
     $p9_boom = explode(",",$p9);
     $p10_boom = explode(",",$p10);

Now what I am trying to do is enter each set of data on its own line within the table. 现在,我要尝试的是在表中的每一行中输入每组数据。 Such as all the parts of P1 go on the first line in the table, all the parts of P2 go on the next line in the table, etc. Thanks ahead of time for your help and let me know if you need more information! 例如,P1的所有部分在表的第一行,P2的所有部分在表的下一行,等等。在此先感谢您的帮助,如果需要更多信息,请告诉我!

Your not going to be able to insert an array into a field. 您将无法将数组插入字段。 You will have to either leave it as a string OR serialize/json_encode the array before inserting. 您必须将其保留为字符串,或者在插入之前对数组进行序列化/ json_encode。 Inserting the rows is no different then inserting any pieces of data. 插入行与插入任何数据一样。

$db = new PDO(/*connection info*/);
$stmt = $db-prepare('insert into tableName(myColumn) values(?)');
// Just loop throw the data with...
$stmt->execute(array($stringToInsert));

Do you want the table create code? 您要表格创建代码吗? I am going to assume each part is a TEXT because you haven't specified.... otherwise change the types. 我将假设每个部分都是一个TEXT,因为您尚未指定...。否则更改类型。 Then there is some code to build queries too. 然后也有一些代码可以构建查询。

<?php

//find maximum length of $p_booms and the booms
$p_booms = array();
$pb_lengths = array();
for($i = 1; $i <= 10; $i++) {
    $p_booms[] = explode(",", $_POST["p$i"]); //not sanitized! (yet)
    $pb_lengths[] = count($pb_booms[$i]);
}
$pmax = max($pb_lengths);


//create the table with the maximum width
$create_table = "
    CREATE TABLE parts (
    partID INT(5) NOT NULL PRIMARY KEY";

for($i = 0; $i < $pmax; $i++) {
    $create_table .= ", p$i TEXT DEFAULT NULL";
}
$create_table .= ");";
$mysqli->query($create_table);


//then insert the values by building a query
//I am assuming partID is the p1, p2, p3 becomes 1, 2, 3 respectively
foreach($p_booms as $id => $boom) {
    $query = "INSERT INTO parts SET partID=$id";
    foreach($boom as $i => $part) {
        $part = $mysqli->real_escape_string($part); //yay sanitized!
        $query .= ", p$i = '$part'";
    }
    $mysqli->query($query);
}

Cheers 干杯

Use the serialize() function. 使用serialize()函数。

$p10_boom = explode(",",$p10);
$p10_boom_serial = serialize( $p10_boom );

That produces something like: 会产生类似:

a:4:{i:0;s:6:"Stuff1";i:1;s:6:"Stuff2";i:2;s:6:"Stuff3";i:3;s:6:"Stuff4";}

Just escape that string when you save it to your DB. 将字符串保存到数据库时,只需转义该字符串即可。

Now whichever field you are going to use for your data should probably be a text field, or a long VARCHAR field. 现在,要用于数据的任何字段都应该是文本字段或长VARCHAR字段。

If you have to retrieve the value and need to turn it back into an array, use unserialize() . 如果必须检索该值并将其转回一个数组,请使用unserialize() You can convert it back into a string using join() or implode() 您可以使用join()implode()将其转换回字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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