使用(select)和(insert into)将数据从php表单插入mysql数据库

byqmnocz  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(252)

我有一个类,我需要创建一个与php和mysql数据库网站。问题是我不能在数据库中插入来自php表单的数据。在要求用户编写将要插入数据库的不同值之后,用户需要编写用户名和密码。使用用户名和密码,我需要找到用户的id。使用该id,我需要将网站用户输入的值插入到personnage表中,该行的iduser是来自用户用户名和密码的值。下面是一个更好地解释我自己的例子:

<form action="backend.php" method="POST">
        <fieldset>
            <legend>Creation Personnage</legend>

            <label> Username :
                <input type="text" name="nom" id="nom" />
            </label>

            <label> Password :
            <input type="text" name="mdp" id="mdp" />
            </label>

            <br><br><br><br>

            <label> Race :
            <input type="text" name="race" id="race" />
            </label>

            <label> Classe :
            <input type="text" name="classe" id="classe" />
            </label>

            <br><br>

            <label> Niveaux :
            <input type="text" name="niveaux" id="niveaux" />
            </label>

            <label> Experience :
            <input type="text" name="experience" id="experience" />
            </label>

            <br><br>

            <label> Sexe :
            <input type="text" name="sexe" id="sexe" />
            </label>

            <label> Age :
            <input type="text" name="age" id="age" />
            </label>

            <br><br>

            <label> Poids :
            <input type="text" name="poids" id="poids" />
            </label>

            <label> Peau :
            <input type="text" name="peau" id="peau" />
            </label>

            <br><br>

            <label> Cheveux :
            <input type="text" name="cheveux" id="cheveux" />
            </label>

            <label> Yeux :
            <input type="text" name="yeux" id="yeux" />
            </label>

            <br><br>

            <label> Religion :
            <input type="text" name="religion" id="religion" />
            </label>

            <label> Portrait :
            <input type="text" name="portrait" id="portrait" />
            </label>

            <br><br>

        </fieldset>

        <input type="submit" value="Ajouter">
    </div>

下面是backend.php:

<?php
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        include_once($_SERVER["DOCUMENT_ROOT"]."/cnxPDO.php");
        if($_SERVER['REQUEST_METHOD'] === 'POST')
        {
            $con = connecPDO("DD", "myparam");
            $nom = $_POST['nom'];
            $mdp = $_POST['mdp'];
            $race = $_POST['race'];
            $classe = $_POST['classe'];
            $niveaux = $_POST['niveaux'];
            $experience = $_POST['experience'];
            $sexe = $_POST['sexe'];
            $age = $_POST['age'];
            $poids = $_POST['poids'];
            $peau = $_POST['peau'];
            $cheveux = $_POST['cheveux'];
            $yeux = $_POST['yeux'];
            $religion = $_POST['religion'];
            $portrait = $_POST['portrait'];

            $idUser = 'SELECT idUser FROM USER WHERE nom = '$nom' AND mdp = '$mdp'';

            $sql='INSERT INTO PERSONNAGE(idUser,race,classe,niveaux,experience,sexe,age,poids,peau,cheveux,yeux,religion,portrait) 
                    VALUES(:idUser,:race,:classe,:niveaux,:experience,:sexe,:age,:poids,:peau,:cheveux,:yeux,:religion,:portrait)';

            $stmt = $con->prepare($sql);
            $stmt->BindParam(':idUser',$idUser);
            $stmt->BindParam(':race',$race);
            $stmt->BindParam(':classe',$classe);
            $stmt->BindParam(':niveaux',$niveaux);
            $stmt->BindParam(':experience',$experience);
            $stmt->BindParam(':sexe',$sexe);
            $stmt->BindParam(':age',$age);
            $stmt->BindParam(':poids',$poids);
            $stmt->BindParam(':peau',$peau);
            $stmt->BindParam(':cheveux',$cheveux);
            $stmt->BindParam(':yeux',$yeux);
            $stmt->BindParam(':religion',$religion);
            $stmt->BindParam(':portrait',$portrait);

            if (!$stmt->execute()) {
            echo "<br>PDO::errorCode():";
            print_r($stmt->errorCode());
            echo "<br>PDO::errorInfo():";
            print_r($stmt->errorInfo());
            }
            else { echo " <br> Sauvegarde effectuée";  }
        }
    ?>

我真的需要帮助来完成这个,否则我就不能通过我的课!!事实上,我们的老师没有解释任何关于这门课的事情,我们必须在网上找到一切!!我不懂3/4的php和mysql。所以我真的需要帮助!我还有很多事情要做,但我会从这个开始。

daolsyd0

daolsyd01#

这个错误与为sql语句参数提供的变量数量错误直接相关。或者,您尝试更新表中不存在的列。
检查列名是否与语句中提供的列名完全匹配,并确保仅为表中存在的列提供数据。
另外,如果希望php在sql语句中插入变量的值,则必须对sql语句使用双引号而不是单引号。如果使用双引号,php将只在变量中插入值。

相关问题