Jump to content

Archived

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

alexcrosson

MySQL & PHP Question

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

Share this post


Link to post
Share on other sites
replace [color=red]$x = mysql_query("SELECT id FROM blog");[/color]

with [color=blue]$x = $row["id"];[/color]

Share this post


Link to post
Share on other sites
Or, this works:

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

Share this post


Link to post
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!

Share this post


Link to post
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

Share this post


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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.