[单击按钮时PHP操作不起作用

问题描述 投票:-1回答:1

提交表单时遇到麻烦,不确定为什么我的按钮不起作用。我正在尝试做的是允许用户编辑数据库上的记录。当我单击cms.php上的编辑链接时,将带我进入edit.php,它显示了单击的电影详细信息。当我更改详细信息并提交时,没有任何反应。另外,我没有被重定向回上一页。非常感谢您能得到的任何帮助,因为我已经坚持了将近一天,谢谢您:) edit.php,下面是处理电影剪辑的后端代码。

edit.php

<?php
include('../includes/conn.inc.php');
include('../includes/functions.php');

$movieID = $_GET['movieID'] ?? null;
$sMovieID = safeInt($movieID);
$sql ="SELECT * FROM sheffmovies WHERE movieID =:movieID";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':movieID', $sMovieID, PDO::PARAM_INT);
$stmt->execute();
$totalnoMovies = $stmt->rowCount();
$row = $stmt->fetchObject();
if($totalnoMovies === 0){
    header('Location: notFoundCMS.php');
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <meta name="description" content = "Movies in Sheffield at Sheff Cinema">
    <meta name= "keywords" content = "SheffCinema, movies, local movies in Sheffield, latest showings in Sheffield">
    <meta name = "author" content = "SheffCinema">
    <title>SheffCinema | Welcome</title>
    <link rel="stylesheet" href="../css/stylesheet.css">
</head>

<body>

 <h1> Edit Page <?php echo $row ->movieName; ?> </h1>

<div class="contact-form">
 <form name ="form1" method="post" action="../action/editRecord.php">

    <input name="movieID" type ="hidden" value ="<?php echo $row->movieID; ?>">

    <label for="movieName">Movie Name</label>
    <input type="text" name="movieName" id="movieName" value="<?php echo $row->movieName; ?>">

    <label for="moviePrice">Price of Movie</label>
    <input type="text" name="moviePrice" id="moviePrice" value="<?php echo $row->moviePrice; ?>">

    <label for="movieCertificate">Age rating</label>
    <input type="text" name="movieCertificate" id="movieCertificate" value="<?php echo $row->movieCertificate; ?>">

    <label for="movieGenre">Genre</label>
    <input type="text" name="movieGenre" id="movieGenre" value="<?php echo $row->movieGenre; ?>"> 

    <label for="movieRating">Rating (Out of 5)</label>
    <input type="text" name="movieRating" id="movieRating" value="<?php echo $row->movieRating; ?>">

    <button type="submit" name="button1" class="button_1" form="form1" value="Submit">Submit</button>
    </div>
</form>

editRecord.php

<?php

include('../includes/conn.inc.php');
include('../includes/functions.php');

 $sMovieName = safeString($_POST['movieName']);
 $sMoviePrice = safeFloat($_POST['moviePrice']);
 $sMovieCertificate = safeInt($_POST['movieCertificate']);
 $sMovieGenre = safeString($_POST['movieGenre']);
 $sMovieRating = safeInt($_POST['movieRating']);
 $sMovieID = safeInt($_POST['movieID']);

 $sql = "UPDATE sheffmovies SET
 movieName = :movieName,
 moviePrice = :moviePrice,
 movieCertificate = :movieCertificate,
 movieGenre = :movieGenre,
 movieRating = :movieRating,
 WHERE movieID = :movieID";
 $stmt = $pdo-> prepare ($sql);
 $stmt->bindParam(':movieName', $sMovieName, PDO::PARAM_STR);
 $stmt->bindParam(':moviePrice', $sMoviePrice, PDO::PARAM_STR);
 $stmt->bindParam(':movieCertificate', $sMovieCertificate, PDO::PARAM_STR);
 $stmt->bindParam(':movieGenre', $sMovieGenre, PDO::PARAM_STR);
 $stmt->bindParam(':movieRating', $sMovieRating, PDO::PARAM_INT);
 $stmt->bindParam(':movieID', $sMovieID, PDO::PARAM_INT);


 header("Location:../cms/cms.php");

exit;
?>
php mysql pdo
1个回答
0
投票

绑定参数后,需要执行将其保存到DB。https://www.php.net/manual/en/pdostatement.execute.php

 $sql = "UPDATE sheffmovies SET
 movieName = :movieName,
 moviePrice = :moviePrice,
 movieCertificate = :movieCertificate,
 movieGenre = :movieGenre,
 movieRating = :movieRating
 WHERE movieID = :movieID";

 $stmt = $pdo-> prepare ($sql);
 $stmt->bindParam(':movieName', $sMovieName, PDO::PARAM_STR);
 $stmt->bindParam(':moviePrice', $sMoviePrice, PDO::PARAM_STR);
 $stmt->bindParam(':movieCertificate', $sMovieCertificate, PDO::PARAM_STR);
 $stmt->bindParam(':movieGenre', $sMovieGenre, PDO::PARAM_STR);
 $stmt->bindParam(':movieRating', $sMovieRating, PDO::PARAM_INT);
 $stmt->bindParam(':movieID', $sMovieID, PDO::PARAM_INT);
 $stmt->execute(); // <- This needs to be added

此外,您的SQL中还有一个逗号结尾。已从此答案中删除。

对于您的HTML,使用按钮的输入界面提交。 (而且您的div标签未在正确的位置关闭)

<div class="contact-form">
 <form name ="form1" method="post" action="../action/editRecord.php">

    <input name="movieID" type ="hidden" value ="<?php echo $row->movieID; ?>">

    <label for="movieName">Movie Name</label>
    <input type="text" name="movieName" id="movieName" value="<?php echo $row->movieName; ?>">

    <label for="moviePrice">Price of Movie</label>
    <input type="text" name="moviePrice" id="moviePrice" value="<?php echo $row->moviePrice; ?>">

    <label for="movieCertificate">Age rating</label>
    <input type="text" name="movieCertificate" id="movieCertificate" value="<?php echo $row->movieCertificate; ?>">

    <label for="movieGenre">Genre</label>
    <input type="text" name="movieGenre" id="movieGenre" value="<?php echo $row->movieGenre; ?>"> 

    <label for="movieRating">Rating (Out of 5)</label>
    <input type="text" name="movieRating" id="movieRating" value="<?php echo $row->movieRating; ?>">

    <input type="submit" class="button_1" value="Submit">

 </form>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.