简体   繁体   English

php pdo插入表格中的数组

[英]Php pdo insert array from form

Through tutorial I created html form that inserts data into database. 通过教程,我创建了将数据插入数据库的html表单。 It works great, but in tutorial it is not explained why they use such code for array list. 它很好用,但是在教程中没有解释为什么他们将这样的代码用于数组列表。

$db = new PDO("mysql:host=localhost;dbname=test", "test_user", "test123");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if(isset($_POST['btn-add']))
{
    $fields= (is_array($_POST['fields'])) ? $_POST['fields'] : array();
    $insertStmt = $db->prepare("INSERT INTO test (test_field) VALUES (:field)");

    foreach ($fields as $field) {
        $insertStmt->execute(array('field' => $field));
    }
}

<form action="" method="POST">
    <label>field 1 <input type="text" name="fields[0]"></label>         
    <label>field 2 <input type="text" name="fields[1]"></label>
    <button type="submit" name="btn-add">add</button>
</form>

My question is about this line. 我的问题是关于这条线。 Maybe someone could explain it. 也许有人可以解释。

$products = (is_array($_POST['fields'])) ? $_POST['fields'] : array();

What this line do - ? 这行是做什么的? $_POST['fields'] : array(); $ _POST ['fields']:array();

Why it does not work just with $products = (is_array($_POST['fields'])) 为什么它不能仅与$ products =(is_array($ _ POST ['fields']))一起使用

Also is this a good way to create array insert from form or I should search for different tutorial? 这也是从表单创建数组插入的好方法,还是我应该搜索其他教程?

The line you mentioned sets the variable $products to an array, no matter if there are data is given via the form or not. 您提到的行将变量$products设置为数组,无论是否通过表单提供了数据。

$products = (is_array($_POST['fields'])) ? $_POST['fields'] : array(); 

says "If $_POST['fields'] is an array, then set $products to this given array, otherwise set $products as an empty array". 说:“如果$_POST['fields']是一个数组,则将$products设置为此给定的数组,否则将$products设置为一个空数组”。

For further information on the ternary operator you can have a look here: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary 有关三元运算符的更多信息,请参见此处: http : //php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

This ensures, that the following code 这样可以确保以下代码

foreach ($fields as $field) {
    $insertStmt->execute(array('field' => $field));
}

doesn't raise a Warning like 不会像

WARNING Invalid argument supplied for foreach() on line number ...

Hope this helps :) 希望这可以帮助 :)

$fields = (is_array($_POST['fields'])) ? $_POST['fields'] : array();

Explanation 说明

If $_POST['fields'] is an array, the condition evaluates to true . 如果$_POST['fields']是一个数组,则条件的计算结果为true If the condition is true, the expression $_POST['fields'] before : will be assigned to the variable $fields otherwise the expression array() after : will be assigned. 如果条件为真,则将:之前的表达式$_POST['fields']分配给变量$fields否则将分配:之后的表达式array()

The syntax goes like: 语法如下:

$var = condition ? expr1 : expr2;

To make you more clear, here it is: 为了使您更清楚,这里是:

$a = 10;
$b = 20;    
$c = $b > $a ? $b : $a;

Here, we check the condition if $b is greater than $a , if it is true, $b 's value is assigned to $c , if it is false, $a 's value is assigned. 在这里,我们检查条件,如果$b大于$a ,如果为true,则将$b的值分配给$c ;如果为false,则将分配$a的值。

This is how ternary expression works. 这就是三元表达式的工作方式。

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

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