dongzongpeng6474 2016-06-10 15:14
浏览 68
已采纳

使用PHP和MySQL更新帖子

I'm building a sort of blog system, but I'm having trouble with editing an existing 'post' in the system. It is collecting the data from the database, and it displays it. But when I click the update button, I get this error. I've looked for it, checked my code couple of times, but I just couldn't find anything. This is the error.

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

And this is my code.

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

If you guys wanna know more, or see more code, I'd like to hear it. Thanks in advance.

EDIT. for update.

//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
        ));

Latest form for update

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

2条回答 默认 最新

  • drnf593779 2016-06-10 15:19
    关注

    :patient_id is missing from the $stmt->execute() function. Please add it and try again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)