RAFC_1990 Posted March 23, 2011 Share Posted March 23, 2011 Hey im quite new to php but im hoping you can help me sort the following code out please. while ($row = mysql_fetch_array($sql_result)) { $id1 = $row["id"]; $username1 = $row["username"]; $password1 = $row["password"]; $organisation1 = $row["organisation"]; $delete = echo '<a href="delete.php<a href="delete.php?id=<?php echo $row->id; ?>">Delete</a>">Delete</a>'; print "<tr><td>$organisation1</td><td></td><td>$username1</td><td>$delete</td></tr>"; Im trying to get a delete button at the end of every row, but i cant seem to get the right syntax for it to create the html button and loop it on every row Please could someone help, Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/ Share on other sites More sharing options...
kenrbnsn Posted March 23, 2011 Share Posted March 23, 2011 You don't want to use the "echo" in an assignment statement. Use <?php $delete = "<a href='delete.php?id=$id1'>Delete</a>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191347 Share on other sites More sharing options...
cs.punk Posted March 23, 2011 Share Posted March 23, 2011 You just echo out the variable, no need to add the <?php ?> tags. You only use those if perhaps you've had a closing tag before and your writing static HTML. while ($row = mysql_fetch_array($sql_result)) { $id1 = $row["id"]; $username1 = $row["username"]; $password1 = $row["password"]; $organisation1 = $row["organisation"]; $delete = echo '<a href="delete.php<a href="delete.php?id=' . $row->id . '>Delete</a>">Delete</a>'; print "<tr><td>$organisation1</td><td></td><td>$username1</td><td>$delete</td></tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191349 Share on other sites More sharing options...
cyberRobot Posted March 23, 2011 Share Posted March 23, 2011 You just echo out the variable, no need to add the <?php ?> tags. You only use those if perhaps you've had a closing tag before and your writing static HTML. while ($row = mysql_fetch_array($sql_result)) { $id1 = $row["id"]; $username1 = $row["username"]; $password1 = $row["password"]; $organisation1 = $row["organisation"]; $delete = echo '<a href="delete.php<a href="delete.php?id=' . $row->id . '>Delete</a>">Delete</a>'; print "<tr><td>$organisation1</td><td></td><td>$username1</td><td>$delete</td></tr>"; Note that the above code still has an extra echo that isn't needed. Also, I would imagine that $row->id is an invalid variable. Based on the code, kenrbnsn's solution should work though. Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191374 Share on other sites More sharing options...
cyberRobot Posted March 23, 2011 Share Posted March 23, 2011 Also, the while loop is missing the end curly bracket after the print statement. Maybe it was accidentally left off when pasting into the forum. But you'll want to look into it if the change provided by kenrbnsn doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191376 Share on other sites More sharing options...
RAFC_1990 Posted March 24, 2011 Author Share Posted March 24, 2011 Thanks guys, worked a treat. But now the problem is that the delete.php code doesnt seem to work for me if (isset ($_GET['id']) && !empty ($_GET['id'])) { mysql_query ('delete from $tbl_name where id='.intval ($_GET['id']).' limit 1'); header ('Location:adminadd.php'); } else { echo ("Error deleting account from database!"); } exit; Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191666 Share on other sites More sharing options...
cyberRobot Posted March 24, 2011 Share Posted March 24, 2011 Assuming that you switch to the solution that kenrbnsn mentioned: <?php ... $delete = "<a href='delete.php?id=$id1'>Delete</a>"; ... ?> Do the delete links contain the ID? If so, what happens when you try to display $_GET['id'] on delete.php; does an ID get displayed? Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191690 Share on other sites More sharing options...
cyberRobot Posted March 24, 2011 Share Posted March 24, 2011 Also I should ask, what kind of error are you getting? Does delete.php display "Error deleting account from database!"; are you getting a MySQL error; or something else? Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191691 Share on other sites More sharing options...
RAFC_1990 Posted March 24, 2011 Author Share Posted March 24, 2011 Yes i used the solution that kenrbnsn mentioned. The link does display the id, but i dont get any error message it just reloads the page as asked but the record is not deleted :/ Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191749 Share on other sites More sharing options...
kenrbnsn Posted March 24, 2011 Share Posted March 24, 2011 Then you have to post the code for delete.php Ken Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191758 Share on other sites More sharing options...
RAFC_1990 Posted March 24, 2011 Author Share Posted March 24, 2011 I did do above, its if (isset ($_GET['id']) && !empty ($_GET['id'])) { mysql_query ('delete from $tbl_name where id='.intval ($_GET['id']).' limit 1'); header ('Location:adminadd.php'); } else { echo ("Error deleting account from database!"); } exit; Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191759 Share on other sites More sharing options...
RAFC_1990 Posted March 24, 2011 Author Share Posted March 24, 2011 Sorted guys, thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/231501-html-in-while-loop/#findComment-1191763 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.