繁体   English   中英

使用PHP和MySQL更新帖子

[英]Updating post with PHP & MySQL

我正在构建一种博客系统,但是在编辑系统中现有的“帖子”时遇到了麻烦。 它正在从数据库中收集数据,并显示它。 但是,当我单击更新按钮时,出现此错误。 我一直在寻找它,检查了几次我的代码,但是我什么都找不到。 这是错误。

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

这是我的代码。

<?php //include config
require_once('../../includes/config.php');

//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bewerk</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<link rel="stylesheet" href="../style/normalize.css">
<link rel="stylesheet" href="../style/main.css">
<link rel="stylesheet" href="../style/login.css">
</head>
<body>




<?php
//check for any errors
if(isset($error)){
foreach($error as $error){
    echo $error.'<br />';
}
}

try {

$stmt = $db->prepare('SELECT patient_id, voornaam, achternaam, leeftijd, lengte, gewicht, foto_url FROM patienten WHERE patient_id = :patient_id') ;
$stmt->execute(array(':patient_id' => $_GET['id']));
$row = $stmt->fetch();

} catch(PDOException $e) {
echo $e->getMessage();
}

?>

<?php

//if form has been submitted process it
if(isset($_POST['submit'])){

$_POST = array_map( 'stripslashes', $_POST );

//collect form data
extract($_POST);



    if(!isset($error)){

    try {

        //insert into database
        $stmt = $db->prepare('UPDATE patiënten SET patient_id, voornaam = :voornaam, achternaam = :achternaam, leeftijd = :leeftijd, lengte = :lengte, gewicht = :gewicht, foto_url = :foto_url WHERE patient_id = :patient_id') ;
        $stmt->execute(array(
            ':patient_id' => $patient_id,
            ':voornaam' => $voornaam,
            ':achternaam' => $achternaam,
            ':leeftijd' => $leeftijd,
            ':lengte' => $lengte,
            ':gewicht' => $gewicht,
            ':foto_url' => $foto_url
        ));

        //redirect to index page
        header('Location: index.php?action=updated');
        exit;

    } catch(PDOException $e) {
        echo $e->getMessage();
    }

}

}

?>


<div class="row">
<div class="col-md-2 col-md-offset-5">
    <img src="../style/images/logo.png">
</div>
</div>

<div class="keuze_link row">
<p><b><?php echo $row['voornaam'];?> <?php echo $row['achternaam'];?></b> bewerken</p>
</div>

<div class="container">



<form class="toevoegen" action="" method="post">
    <input type="text" name="voornaam" value="<?php echo $row['voornaam'];?>">
    <br>
    <input type="text" name="achternaam" value="<?php echo   $row['achternaam'];?>">
    <br>
    <input type="text" name="leeftijd" value="<?php echo   $row['leeftijd'];?>">
    <br>
    <input type="text" name="lengte" value="<?php echo $row['lengte'];? >">
    <br>
    <input type="text" name="gewicht" value="<?php echo $row['gewicht'];?>">
    <br>
    <input type="text" name="foto_url" value="<?php echo   $row['foto_url'];?>">
    <br>
    <input class="button" type="submit" name="submit" value="Updaten!">
</form>
</div>




</body>
</html>

如果你们想了解更多或看到更多代码,我想听听。 提前致谢。

编辑。 进行更新。

//insert into database
        $stmt = $db->prepare('UPDATE patienten SET patient_id = :patient_id, voornaam = :voornaam, achternaam = :achternaam, leeftijd = :leeftijd, lengte = :lengte, gewicht = :gewicht, foto_url = :foto_url WHERE patient_id = :patient_id') ;
        $stmt->execute(array(
            'patient_id' => $patient_id,
            ':voornaam' => $voornaam,
            ':achternaam' => $achternaam,
            ':leeftijd' => $leeftijd,
            ':lengte' => $lengte,
            ':gewicht' => $gewicht,
            ':foto_url' => $foto_url
        ));

最新表格更新

<form class="toevoegen" action="" method="post">
    <input type="hidden" name="voornaam" value="<?php echo $row['patient_id'];?>">
    <input type="text" name="voornaam" value="<?php echo $row['voornaam'];?>">
    <br>
    <input type="text" name="achternaam" value="<?php echo $row['achternaam'];?>">
    <br>
    <input type="text" name="leeftijd" value="<?php echo $row['leeftijd'];?>">
    <br>
    <input type="text" name="lengte" value="<?php echo $row['lengte'];?>">
    <br>
    <input type="text" name="gewicht" value="<?php echo $row['gewicht'];?>">
    <br>
    <input type="text" name="foto_url" value="<?php echo $row['foto_url'];?>">
    <br>
    <input class="button" type="submit" name="submit" value="Updaten!">
</form>

在更新查询中,您忘记了:patient_id进行绑定

是的,在列名前删除$。

$stmt->execute()函数中缺少:Patient_id。 请添加它,然后重试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM