PDA

View Full Version : Updating records in mysql database - images


Tjobbe
June 29th, 2007, 14:29
I have a script where I can add a simple article and I can also choose to upload an image o not.

I have an edit page that currently only edits the text of the article, but I would like to be able to edit the image as well.

Currently, I have a page with a simple form with a file fields that moves the image to a folder on my sitre, and tells the mysql database where it is.

Here is my edit page
<?php
include "dbconnect.php"; $id = $_GET['id'];
?>

<form action="edit_news_store.php?action=update&id=<?php echo $id ?>" method="POST">
<?php


if (!preg_match("#^\d+$#", $id)) {
// maybe die here with an error
}

$query= "SELECT * FROM news WHERE id = '$id'";

$r= mysql_query($query) OR die('Query error: ' .mysql_error());

if (mysql_num_rows($r) == 0) {
}

$row= mysql_fetch_assoc($r);

echo 'Title<br />
<input name="title" id="title" value="'.$row['title'].'" size="80" />';
echo "<br /><br />";
echo 'Short description<br />
<input name="source" id="source" value="'.$row['source'].'" size="80" />';
echo "<br /><br />";
echo 'Source<br />
<input name="shortdescr" id="shortdescr" value="'.$row['shortdescr'].'" size="80" />';
echo "<br /><br />";
echo 'Date<br />
<input name="date" id="date" value="'.$row['date'].'" size="80" />';
echo "<br /><br />";
echo 'Article<br />
<textarea name="content" cols="60" rows="20" id="content" style="margin:0; padding:0; border-top:2px solid #333333;">'.$row['content'].'</textarea>';

mysql_close();

?>
<br /><br />
<input type="submit" value="Update Article" />
</FORM>


and this is the page that stores the info: (edit_news_store.php)

<?php

include "dbconnect.php"; $id = $_GET['id'];
$title = $_POST['title'];
$content = $_POST['content'];

$query = "UPDATE news SET title='". $title ."', content = '". $content ."' WHERE id = '$id'";

$r= mysql_query($query) OR die('Query error: ' .mysql_error());

if (mysql_affected_rows() == 0) {
echo "<h1>update ok</h1>";
} else {

echo '<h1>update succesful1!</h1>';
}

mysql_close();
?>


How do I change the image?

I'd like to have a file field again, that if I want to change it, it just updates the database, if nothing is put into the file field, then nothing changes, is this possible?

Dan
June 30th, 2007, 00:04
Have you tried borrowing code from the other page?

If you stick that in there and still have a problem, it'll be easier to recommend a fix.

(Your best bet is to just copy/paste the HTML and PHP, then in the SQL portion, add in image='".$image.")