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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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