bettarap100 Posted March 18, 2007 Share Posted March 18, 2007 Hello, I am new to using PHP and MySql and have a simple problem that I can't seem to figure out. I have table name 'joke' which has 3 fields [id(primary key), joketext, and jokedate]. In my php file, I am getting all the joketext's from the table and displaying them. My problem is: Next to each joketext that is displayed, I need a link that when clicked will delete that joketext from the table and display the remaining joketext's. // Request the text of all the jokes $result = @mysql_query('SELECT id, joketext FROM joke'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } // Display the text of each joke in a paragraph while ($row = mysql_fetch_array($result)) { $getID = $row['id']; $query = "DELETE FROM joke WHERE id=$getID"; echo '<p>' . $row['joketext'] . '<a href ="' . @mysql_query($query) . '"> Delete This Not Funny Joke</a></p>'; } This code will display the link next to each joketext, but when clicked, the link does not do anything. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/ Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Share Posted March 19, 2007 I think you are going about things a little wrong. First make a page to display all of the jokes. (jokes.php) then make a page to delete a joke (delete.php). In joke.php you want a link from each record you output with the id in a link. (<a href="delete.php?id=$id">Delete Joke</a>) When you select that link it will now go to delete.php and delete that joke from the table, after it is deleted redirect the user back to jokes.php jokes.php <?php // Request the text of all the jokes $result = mysql_query("SELECT id, joketext FROM joke"); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } // Display the text of each joke in a paragraph while ($row = mysql_fetch_array($result)) { echo "<p>". $row['joketext'] . "<a href=\"delete.php?id=".$row[id]."\"> Delete This Not Funny Joke</a>)</p>"; } ?> delete.php <?php $id = $_GET['id']; $result = mysql_query("DELETE FROM joke WHERE id = $id"); echo "Joke Deleted!<br /><br />"; echo "<br />Please wait...<br /><br />"; echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=jokes.php\">"; ?> obviously you will need a little more then just that, but this is just the basics that you need. hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210240 Share on other sites More sharing options...
bettarap100 Posted March 19, 2007 Author Share Posted March 19, 2007 I am actually only supposed to have one php file. Is it possible to do everything in one file? Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210256 Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Share Posted March 19, 2007 You can set it up as a switch if you want. That would get it into one file. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210257 Share on other sites More sharing options...
bettarap100 Posted March 19, 2007 Author Share Posted March 19, 2007 Could you please explain using a switch a little more in this case. I am unsure as to how it could be used. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210265 Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Share Posted March 19, 2007 Here, try this: index.php <?php function list_jokes(){ // Request the text of all the jokes $result = mysql_query("SELECT id, joketext FROM joke"); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } // Display the text of each joke in a paragraph while ($row = mysql_fetch_array($result)) { echo "<p>". $row['joketext'] . "<a href=\"index.php?action=delete_joke&id=".$row[id]."\"> Delete This Not Funny Joke</a>)</p>"; } } function delete_joke($id){ $id = $_GET['id']; $result = mysql_query("DELETE FROM joke WHERE id = $id"); echo "Joke Deleted!<br /><br />"; echo "<br />Please wait...<br /><br />"; echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=index.php\">"; } switch ($action){ case "list_jokes": list_jokes(); break; case "delete_joke": delete_joke($id) break; default: list_jokes(); Break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210272 Share on other sites More sharing options...
bettarap100 Posted March 19, 2007 Author Share Posted March 19, 2007 After trying the code, when I click the link, it still does not get rid of the joketext. I am not sure what could be wrong. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210283 Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Share Posted March 19, 2007 did you make sure to make the connection to the database in both functions? Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210285 Share on other sites More sharing options...
bettarap100 Posted March 19, 2007 Author Share Posted March 19, 2007 I already had a connection to the database, but not within the two functions. // Connect to the database server $dbcnx = @mysql_connect('localhost', 'root', 'mysql'); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } However, I tried putting that same code in both functions, and the links are still not doing anything when clicked. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210295 Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Share Posted March 19, 2007 did you see the "Please wait..." when you clicked on delete or no? try echoing the $id before you delete it to make sure it is getting passed through to the function. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210308 Share on other sites More sharing options...
bettarap100 Posted March 19, 2007 Author Share Posted March 19, 2007 I do not see the Please wait... when i click the Delete link. I have not really worked with functions in php yet so I'm not sure if I am doing things correctly, even though I followed your code. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210322 Share on other sites More sharing options...
bettarap100 Posted March 19, 2007 Author Share Posted March 19, 2007 Looking at the previous code you wrote: <a href =\"jokes.php?action=delete_joke&id=".$row[id]."\"> Is that passing the id to the function delete_joke? Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210339 Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Share Posted March 19, 2007 yes, that is passing it. Quote Link to comment https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210344 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.