I create a news section on 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.
I’ve tried a couple methods, but just seem to be missing something.
My database connection is stored in my header, that’s why it is not shown.
On method #1 The form pulls in data for me to update, but its pulling from the wrong row / id.
Here is method #1
[php]<?php
$post_title = “”;
$description = “”;
$id = isset($_POST[‘ud_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();
?>
Title: <input type=“text” name=“ud_title” value="<?php echo "$post_title"; ?>">
News Details:
<?php echo "$description"; ?>
<input type="submit" value="Update">
<input type="button" value="Cancel" onclick="window.location = '/admin'">
</div>
[hr]
Here is method #2 which I like better because I’m using only on query but it is not pulling in any info into my form.
Method #2
[php]
<?php $sth = $db->prepare("SELECT id, title, description FROM htp_news WHERE id = :id"); $sth->bindValue(':id', isset($_POST['ud_id']), PDO::PARAM_INT); $sth->execute(); $row = $sth->fetch(PDO::FETCH_ASSOC); ?>Title:
News Details:
<?php echo $row[0]['description']; ?>
<input type="submit" value="Update">
<input type="button" value="Cancel" onclick="window.location = '/admin'">
</div>
With method #2 I also tried to bind the title and description, but still didn’t help bring in the data from my database row.
Any help would be appreciated. Thanks