Jump to content


Photo

MySQL & PHP Question


  • Please log in to reply
7 replies to this topic

#1 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 02 October 2006 - 09:17 PM

First of All here is my code:
  	$result = mysql_query("SELECT * FROM blog ORDER BY date DESC") or die(mysql_error());
				while($row=mysql_fetch_array($result)) {
				$x = mysql_query("SELECT id FROM blog");
				echo "<div id=\"blog\">\n"
			      ."<h1>{$row['title']}</h1>\n"
			      ."<p>{$row['content']}</p><br/>\n"
			      ."<h2>Edit Entry | <a href=\"?a=delete&amp;d=$x\">Delete Entry</a></h2>\n"
			      ."</div>\n";
				}
I basicly trying to create a way for user to delete blog posts on my site. There is a loop which pulls all of the blog entries and puts them into html. I want to asign value to the "Delete Entry" link which will give the id of the query being selected. To do this is use $x to store the id of the resulting blog. My code is wrong, i know this but I'm hoping someone can tell me how to re-write:
$x = mysql_query("SELECT id FROM blog");
Into something that will give the value of the id field.

It may be hard to explain so feel free to ask questioins if you don't understand


#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 02 October 2006 - 09:20 PM

replace $x = mysql_query("SELECT id FROM blog");

with $x = $row["id"];

#3 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 October 2006 - 09:25 PM

Or, this works:

$sql = mysql_query("SELECT * FROM blog");
$result = mysql_fetch_array($sql);
$x = $result['id'];


#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 02 October 2006 - 09:32 PM

Or, this works:

$sql = mysql_query("SELECT * FROM blog");
$result = mysql_fetch_array($sql);
$x = $result['id'];


No - it won't work to list all entries, you will only get the first row on that one!

#5 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 October 2006 - 09:39 PM

Or, this works:

$sql = mysql_query("SELECT * FROM blog");
$result = mysql_fetch_array($sql);
$x = $result['id'];


No - it won't work to list all entries, you will only get the first row on that one!


Wouldn't you just stick it into the while statement with a delete link, e.g.:
<?php
		  	$result = mysql_query("SELECT * FROM blog ORDER BY date DESC") or die(mysql_error());
				while($row=mysql_fetch_array($result)) {
					$sql = mysql_query("SELECT * FROM blog");
					$result = mysql_fetch_array($sql);
					$x = $result['id'];
					echo "<div id=\"blog\">\n"
				      ."<h1>{$row['title']}</h1>\n"
				      ."<p>{$row['content']}</p><br/>\n"
				      ."<h2>Edit Entry | <a href='?a=delete&d=$x'>Delete Entry</a></h2>\n"
				      ."</div>\n";
				}
?>

Works for me  ;D

#6 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 02 October 2006 - 09:41 PM

The way alpine did it work for me. Thanks guys i really get a lot help around here.. thats why i love this site soo much.  ;D

#7 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 02 October 2006 - 09:44 PM

Now that, that part of the site works. Would you guys know how to do the Edit Entry Link?

#8 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 October 2006 - 09:57 PM

Try this:
<?php
if ($_GET['a']="edit") {
$result = mysql_query("SELECT * FROM blog WHERE id = '$x'") or die(mysql_error());
$row = mysql_fetch_array($sql);
?>
<form method="post" action="?a=edit2&amp;d=<?php echo $x;?>">
<input type="text" value="<?php echo $row['title'];?>" name="title" />
<textarea name="content" rows="20" cols="60"><?php echo $row['content'];?></textarea>
<input type="submit" name="submit" value="Submit" />
<?php
} elseif ($_GET['a']="edit2") {
$x=$_GET['x'];
$title=$_POST['title'];
$content=$_POST['content'];
mysql_query("UPDATE blog SET title='$title' WHERE id = $x");
mysql_query("UPDATE blog SET content='$content' WHERE id = $x");
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users