It's ugly but it works, maybe you can help fix it
Posted: Tue Dec 28, 2010 5:39 pm
I wrote a very basic blog for a lady and she wanted to ability to edit it so I made a page that lists all the blogs in the db and shows the titles with a radio button next to them, she selects the radio button and says edit then gets a page that has the title, blurb, and blog in <input>and<textarea> boxes for editing. My problem is running the update command I can't think of a way to condense it down to one quick UPDATE command so it's split up into 3 updating each one then recalling and displaying the new updated blog, any help on cleaning this up and making it more efficient would be great. oh ya, it also strips a single quote ' from any line and replaces it with ~... i think it works, haven't tested it yet. Thank you.
Code: Select all
<?php
$title = $_POST['title'];
$blurb = $_POST['blurb'];
$blog = $_POST['blog'];
$num = $_POST['number'];
$safetitle = str_replace("'", "~", $title);
$safeblurb = str_replace("'", "~", $blurb);
$safeblog = str_replace("'", "~", $blog);
$con = mysql_connect("we all know what goes here.");
if(!$con) {
die("Could not connect to database:" . mysql_error());
}
@mysql_select_db("blogdb") or die("Could not select database: " . mysql_error());
$result = mysql_query("UPDATE blogs SET title=$safetitle WHERE blogID=$num") or die("title UPDATE error: " . mysql_error());
$result2 = mysql_query("UPDATE blogs SET blurb=$safeblurb WHERE blogID=$num") or die("Blurb UPDATE error: " . mysql_error());
$result3 = mysql_query("UPDATE blogs SET content=$safeblog WHERE blogID=$num") or die("Content UPDATE error: " . mysql_error());
?>
<html>
<body>
<?
$newquery = mysql_query("SELECT * FROM blogs WHERE blogID = '$num'") or die("Could not re-SELECT stuff: " .mysql_error());
while($row = mysql_fetch_array($newquery)) {
$newtitle = $row['title'];
$newblurb = $row['blurb'];
$newblog = $row['content'];
}
$snewtitle = str_replace("~", "'", $newtitle);
$snewblurb = str_replace("~", "'", $newblurb);
$snewblog = str_replace("~", "'", $newblog);
?>
<p align="center">Your blog has been updated.<br /> The new title is: <?php echo $snewtitle ?><br />Your new blurb is: <?php echo $snewblurb ?><br />And your new blog is: <?php echo $snewblog ?></p>
<a href="test-edit.php">Click</a> to edit another blog   |   <a href="/">Click</a> to go back to your home page.
</body>
</html>