简体   繁体   English

如何将 javascript 数组发送到我的数据库?

[英]How to send an javascript array to my database?

I'm stuck because I don't understand what's wrong with my query, can someone look into it?我被卡住了,因为我不明白我的查询有什么问题,有人可以调查一下吗?

Basically, I'm using ajax to send my array to php, and then using php to send my data to my table, but it doesn't send anything基本上,我使用 ajax 将我的数组发送到 php,然后使用 php 将我的数据发送到我的表,但它没有发送任何内容

Here my javascript这是我的 javascript

// ENVOI DE LA GRILLE VERS LE SERVEUR
            var col_tab = document.getElementById('grille').rows[0].cells;
            var type_tab = document.getElementById('grille').rows[1].cells;
            var nb_cols = document.getElementById('grille').rows[0].cells.length;
            var titre_col=[];
            var type_col;

            var lignes_tab = document.getElementById('grille').rows;
            var nb_lignes = document.getElementById('grille').rows.length;
            var contenu_cell=[];
            var nb_options;
            var options;

            for (var ind=1; ind<nb_cols; ind++)
            {
                titre_col[ind-1] = col_tab[ind].children[4].value;
                type_col = type_tab[ind].children[0].type;
                options = "";

                if (type_col == "text")
                {
                    for (var indlg=1; indlg<nb_lignes; indlg++)
                    {
                        contenu_cell[ind-1] = lignes_tab[indlg].cells[ind].children[0].value;
                    }
                }
                else if (type_col == "number")
                {
                    for (var indlg=1; indlg<nb_lignes; indlg++)
                    {
                        contenu_cell[ind-1] = "Min=" + lignes_tab[indlg].cells[ind].children[0].attributes["min"].value + " Max=" + lignes_tab[indlg].cells[ind].children[0].attributes["max"].value;
                    }
                }
                else
                {
                    for (var indlg=1; indlg<nb_lignes; indlg++)
                    {
                        nb_options = lignes_tab[indlg].cells[ind].children[0].children.length;
                        for (var indop = 0; indop<nb_options; indop++)
                        {
                            options += lignes_tab[indlg].cells[ind].children[0].children[indop].text + "|";
                        }
                        contenu_cell[ind-1] = options;
                    }
                }

                $.post('ajout.php',{titre_col:titre_col,contenu_cell:contenu_cell,ind:ind});
            }

Here my php这是我的 php

<?php

$connect = new PDO("mysql:host=localhost;dbname=innovatech", "root", "12345");

if(isset($_POST["titre_col"]))
{
    $titre_col = $_POST['titre_col'];
    $contenu_cell = $_POST['contenu_cell'];
    $ind = $_POST['ind'];
    sleep(5);
    $query = "
    INSERT INTO colonne_grille 
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) VALUES 
    (:titre_col[0], :titre_col[1], :titre_col[2], :titre_col[3], :titre_col[4], :titre_col[5], :titre_col[6], :titre_col[7], :titre_col[8], :titre_col[9],)
    ";
        $user_data = array(
            ':titre_col' => $_POST["titre_col"],
        );
        $statement = $connect->prepare($query);
        $statement->execute($user_data);

        $query = "
    INSERT INTO ligne_grille 
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, num_ligne) VALUES 
    (:contenu_cell[0], :contenu_cell[1], :contenu_cell[2], :contenu_cell[3], :contenu_cell[4], :contenu_cell[5], :contenu_cell[6], :contenu_cell[7], :contenu_cell[8], :contenu_cell[9], :ind)
    ";
        $user_data = array(
            ':contenu_cell' => $_POST["contenu_cell"],
            ':ind' => $_POST["ind"],
        );
        $statement = $connect->prepare($query);
        $statement->execute($user_data);
}

?>

Here a picture of my tables tables image这是我的桌子图片的图片

Here a picture of an exemple of what's in my arrays exemple of variables这是我的 arrays变量示例中的示例图片

As you can see in my javascript, I'm creating 2 arrays ( titre_col[] and contenu_cell[] ).正如您在我的 javascript 中看到的,我正在创建 2 个 arrays( titre_col[]contenu_cell[] )。 And then, I'm sending those 2 arrays into my php with ajax ( $.post('ajout.php',{titre_col:titre_col,contenu_cell:contenu_cell,ind:ind}; ). And finally, I get those arrays ( $titre_col = $_POST['titre_col']; $contenu_cell = $_POST['contenu_cell']; ) and I send them to my tables And then, I'm sending those 2 arrays into my php with ajax ( $.post('ajout.php',{titre_col:titre_col,contenu_cell:contenu_cell,ind:ind}; ). And finally, I get those arrays ( $titre_col = $_POST['titre_col']; $contenu_cell = $_POST['contenu_cell']; )然后我将它们发送到我的桌子

Can someone help me fix this issue?有人可以帮我解决这个问题吗?

Thanks guys, stay safe:!谢谢大家,注意安全:! :) :)

Just use JSON.stringify on the 2nd parameter of $.post只需在 $.post 的第二个参数上使用 JSON.stringify

// ENVOI DE LA GRILLE VERS LE SERVEUR
            var col_tab = document.getElementById('grille').rows[0].cells;
            var type_tab = document.getElementById('grille').rows[1].cells;
            var nb_cols = document.getElementById('grille').rows[0].cells.length;
            var titre_col=[];
            var type_col;

            var lignes_tab = document.getElementById('grille').rows;
            var nb_lignes = document.getElementById('grille').rows.length;
            var contenu_cell=[];
            var nb_options;
            var options;

            for (var ind=1; ind<nb_cols; ind++)
            {
                titre_col[ind-1] = col_tab[ind].children[4].value;
                type_col = type_tab[ind].children[0].type;
                options = "";

                if (type_col == "text")
                {
                    for (var indlg=1; indlg<nb_lignes; indlg++)
                    {
                        contenu_cell[ind-1] = lignes_tab[indlg].cells[ind].children[0].value;
                    }
                }
                else if (type_col == "number")
                {
                    for (var indlg=1; indlg<nb_lignes; indlg++)
                    {
                        contenu_cell[ind-1] = "Min=" + lignes_tab[indlg].cells[ind].children[0].attributes["min"].value + " Max=" + lignes_tab[indlg].cells[ind].children[0].attributes["max"].value;
                    }
                }
                else
                {
                    for (var indlg=1; indlg<nb_lignes; indlg++)
                    {
                        nb_options = lignes_tab[indlg].cells[ind].children[0].children.length;
                        for (var indop = 0; indop<nb_options; indop++)
                        {
                            options += lignes_tab[indlg].cells[ind].children[0].children[indop].text + "|";
                        }
                        contenu_cell[ind-1] = options;
                    }
                }

                $.post('ajout.php',JSON.stringify({titre_col:titre_col,contenu_cell:contenu_cell,ind:ind})");
            }

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

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