简体   繁体   English

使用 php pdo 更新行

[英]Update row with php pdo

I create a news section onn my website and want the ability to update it if needed.我在我的网站上创建了一个新闻部分,并希望能够在需要时更新它。 I had it working using the old mysql method, but want to change it by using PDO.我使用旧的 mysql 方法让它工作,但想使用 PDO 更改它。

Can someone point out my error.有人可以指出我的错误。 The form pulls in data for me to update, but its pulling from the wrong row / id.该表单为我提取数据以进行更新,但它是从错误的行/id 中提取的。

Here is my query:这是我的查询:

<?php
$post_title = "";
$description = "";
$id = $_GET['id'];

$query = $db->query("SELECT title, description FROM htp_news WHERE id='$id'");

$post_title     = $db->query('SELECT title FROM htp_news')->fetchColumn();
$description    = $db->query('SELECT description FROM htp_news')->fetchColumn();

?>

And Here is my form where I'm echoing in the data.这是我在数据中回显的表格。

<form method="post" action="update-news.php">
<input type="hidden" name="ud_id" style="width: 100%" value="<?php echo "$id"; ?>">

<div class="grid_12 botspacer60">


Title: <input type="text" name="ud_title" value="<?php echo "$post_title"; ?>">
<br /><br />

News Details:<br />
<textarea id="tiny_mce" name="ud_description" rows="8"><?php echo "$description"; ?></textarea>

I'm not asking to rewrite my code, just a tip or something would be helpful so I can figure out what I did wrong here.我不是要求重写我的代码,只是一个提示或其他有用的东西,这样我就可以弄清楚我在这里做错了什么。

UPDATE This works with some modification to AdRock answer.更新这适用于对 AdRock 答案的一些修改。

    <?php

    $id = isset($_GET['id']) ? $_GET['id'] : NULL;
    $sth = $db->prepare("SELECT `id`, `title`, `description` FROM `htp_news` WHERE `id` = :id");
    $sth->bindParam(':id', $id, PDO::PARAM_INT);
    $sth->setFetchMode(PDO::FETCH_OBJ);
    $sth->execute();

    $row = $sth->fetch();

    ?>
    <form method="post" action="update-news.php">
    <input type="hidden" name="ud_id" style="width: 100%" value="<?php echo $row->id; ?>">

    <div class="grid_12 botspacer60">


    Title: <input type="text" name="ud_title" value="<?php echo $row->title; ?>">
    <br /><br />

    News Details:<br />
    <textarea id="tiny_mce" name="ud_description" rows="8"><?php echo $row->description; ?></textarea>
    </div>


    <div class="grid_12">

        <input type="submit" value="Update">
        <input type="button" value="Cancel" onclick="window.location = '/admin'">
        </div>
    </form>
    </div>

Here is the action script (update-news.php) that I'm using to add the new data into my database.

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/includes/database.php");
// new data
$id             = $_POST['id'];
$title          = $_POST['title'];
$description    = $_POST['description'];
// query
$sql = "UPDATE `htp_news` SET `title`=?, `description`=? WHERE id=?";
$sth = $db->prepare($sql);
$sth->execute(array($title,$description,$id));
echo "The post has been updated.<br />
<a href='edit-delete-news.php'>Update another position.</a><br />";
?> 

Thanks for the help.谢谢您的帮助。

Try this尝试这个

<?php

$sth = $dbh->prepare('SELECT id, title, description FROM htp_news WHERE id = :id')
$sth->bindValue(':id', $_POST['ud_id'], PDO::PARAM_INT);
$sth->execute();

$row = $sth->fetch(PDO::FETCH_ASSOC);

?>
<form method="post" action="update-news.php">
<input type="hidden" name="ud_id" style="width: 100%" value="<?php echo $row[0]['id']; ?>">

<div class="grid_12 botspacer60">


Title: <input type="text" name="ud_title" value="<?php echo $row[0]['title']; ?>">
<br /><br />

News Details:<br />
<textarea id="tiny_mce" name="ud_description" rows="8"><?php echo $row[0]['description']; ?></textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM