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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.