gpt4 book ai didi

php - 当用户点击提交按钮时,index.php 不会捕获该操作并更新数据库

转载 作者:行者123 更新时间:2023-11-30 00:27:57 26 4
gpt4 key购买 nike

我这里有3个文件,index.php、authors.html.php和form.html.php,index.php是我的 Controller 脚本,然后调用authors.html.php来显示作者,最后调用form.html。当用户想要编辑作者或在 MySQL 数据库中添加作者时,可以使用 php 。

我遇到的问题是,当用户点击更新按钮时,数据库不会更新作者详细信息...看来我的 Controller 脚本没有捕获“editform”操作?我不完全确定它为什么会下滑。以下是文件摘录:

index.php( Controller ):

    <?php
include $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';

if ((isset($_POST['action'])) and ($_POST['action'] == 'Edit'))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php'; //connect to mysql
try
{
$sql = 'SELECT id, name, email FROM author WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching author details...';
include 'error.html.php';
exit();
}

$row = $s->fetch();

$pageTitle = 'Edit Author';
$action = 'editform';
$name = $row['name'];
$email = $row['email'];
$id = $row['id'];
$button = 'Update Author';

include 'form.html.php';
header('Location: .');
exit();
}

if (isset($_GET['editform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php'; //connect to mysql

try
{
$sql = 'UPDATE author SET name = :name, email = :email WHERE id = :id';
$s->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->bindValue(':name', $_POST['name']);
$s->bindValue(':email', $_POST['email']);
$s->execute();
}
catch (PDOException $e)
{
$error = "Error updating selected author.";
include 'error.html.php';
exit();
}

header('Location: .');
exit();
}

include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

try
{
$result = $pdo->query('SELECT id, name FROM author');
}
catch (PDOException $e)
{
$error = 'Error fetching authors from the database: ';
include 'error.html.php';
exit();
}

foreach($result as $row)
{
$authors[] = array('id' => $row['id'], 'name' => $row['name']);
}
include 'authors.html.php';
?>

authors.html.php

    <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php' ?>
// When I call "htmlout()" is the same as "echo htmlspecialchars()"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Authors</title>
</head>
<body>
<p>
<h1>Manage Authors</h1>
<p>
<a href="?add">Add New Author</a>
</p>
<ul>
<?php foreach ($authors as $author): ?>
<li>
<form action="?<?php $action ?>" method="post">
<div>
<?php htmlout($author['name']); ?>
<input type="hidden" name="id" value="<?php echo $author['id']; ?>">
<input type="submit" name="action" value="Edit">
<input type="submit" name="action" value="Delete">
</div>
</form>
</li>
<?php endforeach; ?>
</ul>
<p>
<a href="..">Return to JMS Home</a>
</p>
</p>
</body>
</html>

form.html.php

   <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php' ?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php htmlout($pageTitle); ?></title>
</head>
<body>
<h1><?php htmlout($pageTitle); ?></h1>

<form action="?<?php $action ?>" method="post">
<div>
<label for="name">Name:
<input type="text" name="name" id="name" value="<?php htmlout($name); ?>">
</label>
</div>
<div>
<label for="email">Email:
<input type="text" name="email" id="email" value="<?php htmlout($email); ?>">
</label>
</div>
<div>
<input type="hidden" name="id" value="<?php htmlout($id); ?>">
<input type="submit" name="action" value="<?php htmlout($button) ?>">
</div>
</form>
</body>
</html>

最佳答案

我发现我做错了什么了!唷...

我搞砸了这条线

$s->prepare($sql);

应该是

$s = $pdo->prepare($sql);

正如 @MamaWalter 指出的那样,我正在查看 $_GET 中的 $_POST 变量,因此我对其进行了更改,现在它工作得很好!

@linus72982 您使用 var_dump() 的建议是一个巨大的帮助,我是 PHP 新手,因此不知道它......再次感谢您所做的一切!

关于php - 当用户点击提交按钮时,index.php 不会捕获该操作并更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22732617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com