Jump to content

MySQL & PHP Question


alexcrosson

Recommended Posts

First of All here is my code:
[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";
}[/code]
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 "[b]Delete Entry[/b]" link which will give the id of the query being selected. To do this is use [b]$x[/b] 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:[code] $x = mysql_query("SELECT id FROM blog");[/code]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
Link to comment
https://forums.phpfreaks.com/topic/22797-mysql-php-question/
Share on other sites

[quote author=manmanman link=topic=110291.msg445607#msg445607 date=1159824321]
Or, this works:

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

No - it won't work to list all entries, you will only get the first row on that one!
Link to comment
https://forums.phpfreaks.com/topic/22797-mysql-php-question/#findComment-102690
Share on other sites

[quote author=alpine link=topic=110291.msg445609#msg445609 date=1159824761]
[quote author=manmanman link=topic=110291.msg445607#msg445607 date=1159824321]
Or, this works:

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

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

Wouldn't you just stick it into the while statement with a delete link, e.g.:
[code]
<?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";
}
?>
[/code]

Works for me  ;D
Link to comment
https://forums.phpfreaks.com/topic/22797-mysql-php-question/#findComment-102693
Share on other sites

Try this:
[code]
<?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");
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/22797-mysql-php-question/#findComment-102704
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.