如何修复“连接失败:SQLSTATE [42000]:”以发送表单数据?

时间:2019-04-28 15:33:24

标签: php html mysql

对于学校,我们必须建立一个简单的数据库和一个页面,人们可以在该页面上填写其详细信息,然后将其发送到数据库中,以便以后在其他页面上对其进行回忆。

为此,我创建了一个简单的html表单,人们可以在该表单中填写要发送的详细信息,当我按“提交”时,我会得到:

  

连接失败:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在``Begintime'',``Instructor'',``Players'',``Gamename'')附近使用VALUES('','','','')''''

我还从学校提供的另一个数据库中获取了一些数据(感谢上帝,它工作得很好),它是$ sql1

所以可以肯定的是,但是我似乎无法弄清楚这一点。希望有人看到我看不到的东西。

我对php和mysql比较陌生,现在还在上学,但是现在放假了,所以我已经关闭了,所以我在学校找不到帮助

<?php
$servername = "localhost";
$username = "root";
$password = "mysql";
$dbname = "Planningstool";

    $Begintime = $_POST["Begintime"];
    $Instructor = $_POST["Instructor"];
    $Players = $_POST["Players"];
    $Gamename = $_POST["Gamename"];
    $conn = new PDO("mysql:host=$servername;dbname=Planningstool", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    try {

        $stmt =  $conn->prepare("INSERT INTO Planning ('Begintime', 'Instructor', 'Players', 'Gamename') VALUES (:Begintime,:Instructor,:Players,:Gamename)");

        $stmt->bindParam(':Begintime', $Begintime);
        $stmt->bindParam(':Instructor', $Instructor);
        $stmt->bindParam(':Players', $Players);
        $stmt->bindParam(':Gamename', $Gamename);
        $stmt->execute();
        header("somewhere")
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
    }

    $sql1 = "SELECT id, name, image, description, expansions, skills, url, youtube, min_players, max_players, play_minutes, explain_minutes FROM games";
    $result = $conn->query($sql1);
    $row = $result->fetch();

    $dbresult = $conn->prepare($sql2);
<div>
<h1 class="planningstitel">Planning</h1>
<br>
<p class="errortext">De vakken met * zijn verplicht</p>
<form method="POST" action="planning.php">
<div class="form-group1">
Keuze spel:
<select name="Gamename" value="<?php $Gamename?>">
<option value="">Maak uw keuze</option>
    <?php
        foreach ($result as $row) {
    ?>
<option name="Gamename" value="<?php $Gamename?>"><?php echo $row["name"]?></option>
    <?php
        }
    ?>
</select>
<span class="error">* <?php echo $gamenameErr;?></span>
</div>
<div class="form-group2">
    Starttijd:
<input type="time" class="form-control" name="Begintime" value="<?php $Begintime ?>">
<span class="error">* <?php echo $StarttimeErr;?></span>
</div>
<div class="form-group3">
    Uitlegpersoon:
<input type="text" class="form-control" name="Instructor" value="<?php $Instructor ?>">
    <span class="error">* <?php echo $InstructorErr;?></span>
</div>
<div class="form-group4">
    Spelers:<br>
<input type="text" class="form-control-speler" name="Players" value="<?php $Players ?>">

<input type="text" class="form-control-speler" 
<p>Niet alle textboxen bij Spelers hoeven ingevuld te worden</p>
</div>
<input type="submit" name="Submit">
<input type="reset">
    </form>




</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

不要在列名周围使用引号

$stmt =  $conn->prepare("INSERT INTO Planning (Begintime, Instructor, Players, Gamename) 
VALUES (:Begintime,:Instructor,:Players,:Gamename)");