簡體   English   中英

MYSQL PHP通過html表單更新數據庫表信息

[英]MYSQL PHP update db table information through html form

提前致謝。

我正在嘗試更新數據庫中的信息,但是沒有運氣,也沒有錯誤消息。

這是頁面代碼(pid或post_id在URL中):

<?php
include('page with functions.php');


if (isset($_GET['pid'], $_POST['title'], $_POST['body'])){

if (edit_post($_GET['pid'], $_POST['title'], $_POST['body'])){
header("Location: blog_edit.php?pid={$_GET['pid']}");
}else{
header("Location: some location.php");
}

die();

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Blog Edit</title>
</head>

<body>

<div>
    <?php

if (isset($_GET['pid']) ===false || valid_pid($_GET['pid']) === false){
echo 'Invalid post ID';
}else{
$post = get_post($_GET['pid']);

    ?>

<h2><?php echo $post['title']; ?></h2>


<hr />

<p><?php echo $post['body']; ?></p>

<hr />



    <form action="" method="post">
         <p>
<label for="title">Title</label>
        <input type="text" name="title" id="title" value="<?php echo $post['title']; ?>"/>
        </p>
        <p>
            <textarea name="body" rows="20" cols="60"><?php echo $post['body']; ?></textarea>
        </p>
        <p>
            <input type="submit" value="Edit Post" />
        </p>
    </form>
<?php
}
?>
</div>
</body>
</html>

這是函數(URL中的pid或post_id):

// edits a blog entry
function edit_post($title, $body){

$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");

}

任何幫助將非常感謝! 僅供參考,我是PHP MYSQL的新手,所以請客氣。

if (edit_post($_GET['pid'], $_POST['title'], $_POST['body'])){
header("Location: blog_edit.php?pid={$_GET['pid']}");
}else{
header("Location: some location.php");
}

您在函數調用中傳遞了三個參數,但是函數定義只有兩個參數。

function edit_post($title, $body){

$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");

}

同樣,從函數返回一個布爾值。

function edit_post($pid, $title, $body){

$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

$result = mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");

echo $result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM