dongwei3151 2019-04-28 15:33
浏览 416
已采纳

如何修复'连接失败:SQLSTATE [42000]:'用于发送表单数据? [重复]

This question already has an answer here:

For school we have to make a simple database and a page from where people can fill in their details and to send them to a database to later recall them on a different page.

For this i created a simple html form where people can fill in the details to be send yet when i press submit i get:

Connection failed: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Begintime', 'Instructor', 'Players', 'Gamename') VALUES ('','','','')'"

i'm also getting some data from a different database provided by school(so it works perfectly thank god) which is $sql1

so its for sure somewhere there but i can't seem to figure out what at this point. Hopefully somebody sees what i can't see.

i'm relatively new to php and mysql and still on school but it's vacation now so i am unable to find help on school since it's closed

<?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>
</div>
  • 写回答

1条回答 默认 最新

  • dth981485742 2019-04-28 15:35
    关注

    Don't use quote around column name

    $stmt =  $conn->prepare("INSERT INTO Planning (Begintime, Instructor, Players, Gamename) 
    VALUES (:Begintime,:Instructor,:Players,:Gamename)");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?