gpt4 book ai didi

php - 我是否继续在 php/mysql、phpMyAdmin 中正确编码编辑和删除功能

转载 作者:行者123 更新时间:2023-11-29 14:56:55 25 4
gpt4 key购买 nike

我正在努力向我的基本博客应用程序添加编辑和删除功能。我正在努力让 edit.php 代码和 delete.php 代码正确处理。

当用户单击删除或编辑按钮时,相关 php 文件中的代码不会被处理。

主要 PHP 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="container">

<h1>Lay Down Your Thoughts</h1>

<div id="boxtop"></div>
<div id="content">

<!-- form to leave a message -->
<form action="<?php $self ?>" method="post">
<h2>Post your thought!</h2>

<div class="fname"><label for="name"><p>Name:</p></label><input name="name" type="text" cols="20" /></div>
<div class="femail"><label for="email"><p>Email:</p></label><input name="email" type="text" cols="20" /></div>
<label for="message"><p>Message:</p></label>
<textarea name="post" rows="5" cols="40"></textarea>
<input name="send" type="hidden" />
<p><input type="submit" value="send" /></p>
</form>

<?php
$self = $_SERVER['PHP_SELF']; //the $self variable equals this file
$ipaddress = ("$_SERVER[REMOTE_ADDR]"); //the $ipaddress var equals users IP
include ('db.php');
// checks the POST to see if something has been submitted
if(isset($_POST['send']))
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['post'])) {
echo('<p class="error">You did not fill in a required field.</p>');
} else {

// if there are no empty fields, insert into the database:

//validate through htmlspecialchars()
// eliminates the user from submitting harmful html
// also runs through mysql_real_escape_string()
// stops users sending SQL code to infiltrate the db
$name = htmlspecialchars(mysql_real_escape_string($_POST['name']));
$email = htmlspecialchars(mysql_real_escape_string($_POST['email']));
$post = htmlspecialchars(mysql_real_escape_string($_POST['post']));

// this is our SQL string to insert shouts into db
$sql = "INSERT INTO messages SET name='$name', email='$email', post='$post', ipaddress='$ipaddress';";

// run the SQL string
// if it succeeds, display message
if (@mysql_query($sql)) {
echo('<p class="success">message has been posted</p>');
} else {
// if error, send message
echo('<p class="error">There was an unexpected error when posting your message.</p>');
}
}

// display 8 latest messages
$query = "SELECT * FROM messages ORDER BY `id` DESC LIMIT 8;";

// run query if it fails display fail
$result = @mysql_query("$query") or die('<p class="error">There was an unexpected error collecting messages.</p>');

?><ul><?
// display the rows from the post
while ($row = mysql_fetch_array($result)) {

$ename = stripslashes($row['name']);
$eemail = stripslashes($row['email']);
$epost = stripslashes($row['post']);

// gravatar image
$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5(strtolower($eemail))."&size=70";

echo('<li><div class="meta"><img src="'.$grav_url.'" alt="Gravatar" /><p>'.$ename.'</p></div><div class="message"><p>'.$epost.'</p></div></li>');

echo ('<form action="messageME_final_delete.php" method="post"><input name="delete" type="hidden" /> <p><input type="submit" value="delete" /></p></form>');

echo('<form action="messageME_final_update.php" method="post"><input name="edit" type="hidden" /> <p><input type="submit" value="edit" /></p></form>');


}
?></ul><?
?>

</div><!--/content-->
<div id="boxbot"></div>

</div><!--/container-->

</body>
</html>

这是编辑 php 文件:

<form action="messageME_final_update.php" method="post">
<h2>Edit this Thought!</h2>

<div class="fname"><label for="name"><p>Name:</p></label><input name="name" type="text" cols="20" /></div>
<div class="femail"><label for="email"><p>Email:</p></label><input name="email" type="text" cols="20" /></div>
<label for="message"><p>Message:</p></label>
<textarea name="post" rows="5" cols="40"></textarea>
<input name="send" type="hidden" />
<p><input type="submit" value="send" /></p>
</form>

<?
include ('db.php');

$query="UPDATE messages SET name='name', email='email', post='post' WHERE id='ID'";
mysql_query($query);
echo "Record Updated";
mysql_close();
?>

最后是删除 php 代码:

<?php

include ('db.php');

$sql = "DELETE FROM `messages` WHERE `ID` =" ." mysql_real_escape_string ( $_GET['ID'] )";

mysql_select_db ( $database, $connect );

if ( @mysql_query ( $sql ) )
{
echo 'Article ID = ' . $_POST['ID'];
echo ' was deleted successfully';
}
else {
die ( mysql_error () );
}
?>

最佳答案

您的更新页面根本没有与识别用户想要编辑的帖子相关的代码。它只是呈现一个新表单并尝试更新 ID 为字符串“ID”的行。

您的删除页面尝试访问 $_GET['ID'] 和 $_POST['ID'],这两者永远不会被设置,因为 HTTP 请求始终是单一方法(GET 或 POST,或头等)。您也无法正确连接字符串与函数,而是将文字文​​本“mysql_real_escape_string(...”作为查询的一部分发送,这将不会运行。

$sql = "DELETE FROM messages WHERE ID = " . (int)$_POST['ID'];

...更接近您想要的,只是帖子列表上的表单不包含名为 ID 的元素。您应该创建一个,并用该行对应的帖子 ID 填充它。

<input type="hidden" name="ID" value="<?php echo $row['ID']; ?>" />

对指向编辑页面的表单执行相同的操作,并使用 $_POST['ID'] 查找帖子并填充表单字段以进行编辑。

建议阅读,它将引导您完成在 PHP/MySQL 中构建 CMS 的各个方面:

http://www.amazon.com/Build-Database-Driven-Using-MySQL/dp/0980576814/ref=dp_ob_title_bk

关于php - 我是否继续在 php/mysql、phpMyAdmin 中正确编码编辑和删除功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4371447/

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