Jump to content

Simple PHP/MySQL help


bettarap100

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210240
Share on other sites

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;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210272
Share on other sites

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. 

Link to comment
https://forums.phpfreaks.com/topic/43298-simple-phpmysql-help/#findComment-210295
Share on other sites

Archived

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

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