dongyuying1507 2018-12-18 21:56
浏览 240


I have a class where I need to create a website with PHP and MySQL databases. The problem is that I am not able to insert data from PHP form in my database. The user is asked to write their username and their password, after he is asked to write different values that are going to be inserted in the database. With the username and the password, I need to find the Id of the user. With that Id, I need to insert the values entered by the user of the website into the table PERSONNAGE with the IdUser of the row being the one from the user's username and password. Here's an exemple to explain myself better :

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

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

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


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

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


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

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


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

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


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

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


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

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


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

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



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

Here's the backend.php :

        ini_set('display_errors', 1);
        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) 

            $stmt = $con->prepare($sql);

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

I really need help to finish this or I won't pass my class !! The fact is that our teacher doesn't explain anything about the class and we have to find everything on the web !! I don't understand 3/4 of PHP and MySQL. So I really need some help ! There's also a lot more I need to do, but I will begin with this.

  • 写回答

1条回答 默认 最新

  • dotdx80642 2018-12-18 23:40

    The error directly relates to the fact that there are the wrong amount of variables supplied for the SQL statement parameters. Alternatively, you are trying to update a column which does not exist in the table.

    Check the column names match exactly with the supplied column names in the statement, and make sure you supply data only for columns which exist in the table.

    Also, if you want PHP to interpolate values iinto variables within your SQL statement, you MUST use double quotes instead of single quotes for your SQL statement. PHP will only interpolate values into variables if you use double quotes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00