alexcrosson Posted October 2, 2006 Share Posted October 2, 2006 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&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 Quote Link to comment Share on other sites More sharing options...
alpine Posted October 2, 2006 Share Posted October 2, 2006 replace [color=red]$x = mysql_query("SELECT id FROM blog");[/color]with [color=blue]$x = $row["id"];[/color] Quote Link to comment Share on other sites More sharing options...
manmanman Posted October 2, 2006 Share Posted October 2, 2006 Or, this works: [code]$sql = mysql_query("SELECT * FROM blog");$result = mysql_fetch_array($sql);$x = $result['id'];[/code] Quote Link to comment Share on other sites More sharing options...
alpine Posted October 2, 2006 Share Posted October 2, 2006 [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 Link to comment Share on other sites More sharing options...
manmanman Posted October 2, 2006 Share Posted October 2, 2006 [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 Quote Link to comment Share on other sites More sharing options...
alexcrosson Posted October 2, 2006 Author Share Posted October 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
alexcrosson Posted October 2, 2006 Author Share Posted October 2, 2006 Now that, that part of the site works. Would you guys know how to do the [b]Edit Entry[/b] Link? Quote Link to comment Share on other sites More sharing options...
manmanman Posted October 2, 2006 Share Posted October 2, 2006 Try this:[code]<?phpif ($_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&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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.