dongyuying1507 2018-12-18 21:56
浏览 240
已采纳

使用(SELECT)和(INSERT INTO)从PHP表单将数据插入MySQL数据库

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">
        <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>

Here's the 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";  }
        }
    ?>

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.

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

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?