【问题标题】:Adding array into sql database将数组添加到sql数据库中
【发布时间】:2014-07-21 15:55:58
【问题描述】:

我正在尝试将此数组正确添加到数据库中。我有 3 个输入框,当我回显 $key 时,我的所有信息都在那里,就像 Jonesbass23 一样,但是当我运行代码时,每个字段都会在数据库中获得一个新行,它不会全部进入 1 行,并且值会进入错误列也是如此。还将根据用户想要的输入字段数量动态添加我的 $_POST 数组,因此我的数据库条目可以是多个。以为我掌握了它,但猜不到。任何帮助将不胜感激,谢谢。

    <?php
session_start();
$errmsg_arr = array();
$errflag = false;
require_once 'dbconfig.php';        // Database Connection

//Retrieve data input from addfish.php

$username = $_SESSION['username'];

print_r($_POST);
}

//Returns error message text on screen


if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: addfish.php");
exit();
}

//Insert data into database


$statement = $dbc->prepare("INSERT INTO entries(username, species, length, weight)
    VALUES(:username, :species, :length, :weight)");

foreach($_POST as $key => $data) {

echo $key;
echo $data['species']; // The data of species1, species2, or species3, etc.
echo $data['length'];
echo $data['weight'];


try {
    $statement->execute(array(
        "username" => "$username",
        "species" => $data['species'],
        "length" => $data['length'],
        "weight" => $data['weight']
    ));
}

catch(PDOException $e) {
    echo "Exception caught: $e";
}
} 
?>  

【问题讨论】:

    标签: php mysql arrays foreach


    【解决方案1】:

    问题是您正在为$_POST每个键 执行 1 次插入:

    foreach($_POST as $key => $value)
    

    因此,如果您的字段有 3 个输入,您的帖子将如下所示:

    $_POST => array (
       ['key1'] => 'value1'
       ['key2'] => 'value3'
       ['key3'] => 'value2'
    )
    

    您的 foreach 将为每个键执行一次插入,这就是为什么您最终会为每个键创建一个新行,更不用说您只插入 $key 字符串的一个字母:$key[1] 将返回例如$key 字符串的第二个字母。

    另外,您不需要循环 $_POST 的原因仅仅是因为每个键包含不同的值,并且您可以通过 $_POST['species'] 正常访问它们,例如 FuzzyTree 建议的那样。

    更新

    您可以通过如下命名输入的信息对它们进行分组:

    <input type="text" name="species1[species]" />
    <input type="text" name="species1[length]" />
    <input type="text" name="species1[weight]" />
    <br />
    <input type="text" name="species2[species]" />
    <input type="text" name="species2[length]" />
    <input type="text" name="species2[weight]" />
    

    等等,所以你可以像这样在你的 php 上循环它们:

    foreach($_POST as $key => $data) {
        echo $key; // This will print species1, species2, species3, etc.
    
        echo $data['species']; // The data of species1, species2, or species3, etc.
        echo $data['length'];
        echo $data['weight'];
    }
    

    另外,建议您在循环之前而不是在循环内准备语句。这样做没有问题,但这就是使用准备好的语句的意义所在。结合两种建议的示例:

    $statement = $dbc->prepare("INSERT INTO entries(username, species, length, weight)
        VALUES(:username, :species, :length, :weight)");
    
    foreach($_POST as $key => $data) {
        try {
            $statement->execute(array(
                "username" => $username,
                "species" => $data['species'],
                "length" => $data['length'],
                "weight" => $data['weight']));
    
        } catch(PDOException $e) {
            echo "Exception caught: $e";
        }
    }
    

    【讨论】:

    • 我的 $key 包含多行输入,这些输入会根据用户自己添加的行数而变化,因此 $_POST['species'] 然后下一个将是 $_POST['species2'] 分机。 ..
    • 然后你应该将所有物种或物种二的相关信息分组,将你的输入命名为species1['field']species2['field']等,这样你就可以检索他们的信息,如$_POST['species1']['data'] .
    • 好的,我已经在顶部更新了我的代码。当我使用 print_r($_POST);它打印我的数组,所有名称和值都是正确的。当它运行时 echo $key;我得到了正确的名称species1、species2 等...但是我的 echo $data['species'] $data['length'] 等.. 是空白的,它会将空值放入我的 sql 数据库中。
    • 尝试删除 HTML 表单中键周围的引号。例如将name="species1['species']" 更改为name="species1[species]"
    • 太棒了!效果很棒。感谢您的帮助!
    【解决方案2】:

    你必须序列化你的数组。

    这是文档中的示例

    $sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
    

    基本上,它将您的数组放在一行中,将其分配给一个变量并存储它,以便以后可以“反序列化”(基本上它通过字符进行解析)。

    【讨论】:

    • 不好的做法,检查1NF
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2021-02-20
    相关资源
    最近更新 更多