PHP and MySQL (13)

Source: Internet
Author: User
PHP and MySQL getting started (13)

As a homework task, you can see if you can solve this problem: Put a hyperjoin called "Delete this Joke" after each Joke displayed on the page, when you click the connection, the joke is deleted from the database and a list of modified jokes is displayed. Below are some tips for you:

You can also complete all the functions on a multi-function page.

You need to use the SQL DELETE command, which we learned in chapter 2.

This is a key issue. To delete a specified database, you must be uniquely identified. The ID in the Jokes table can complete this function. You must pass the ID of the joke to be deleted to the request to delete the joke. It is appropriate to put this value in the query string connected by "Delete this Joke.

If you think you have an answer, or you only want to know the solution, go to the next page. Good luck!

Conclusion

In this chapter, we learned some new PHP functions used to implement interfaces with MySQL database services. Using these functions, we created our first database-driven website, which can publish jokes in our database online and allow visitors to add their own jokes to it.

In chapter 5, we will go back to the MySQL command line to learn how to use relational databases and other more advanced SQL queries to describe more complex information, and give the visitor special permissions for the jokes they have added!

Solution to challenges

This is the solution for "homework" proposed above. To add a "Delete this Joke" connection after each Joke, you must make the following changes:

Previously, we used to "Add a Joke!" at the bottom of our page !" A $ addjoke variable is passed in the connection. this variable is used to notify our script not to display the common joke list, but to display a form for inputting jokes. Similarly, in our "Delete
This Joke "is used to pass a $ deletejoke variable in the connection to indicate that we want to delete a Joke.

While getting the JokeText column for each joke, we also get the value of the ID column, so we get the ID associated with each joke in the database.

The ID value of the joke we want to delete is assigned to the $ deletejoke variable. This is achieved by inserting the ID value obtained from the database into the "Delete this Joke" connection of every Joke.

An if statement is used. if $ deletejoke is assigned a value (using the isset function) when loading this page ), we will use this value (the ID of the joke to be deleted) in a SQLDELETE statement to delete the specified joke.

Here is all the source code:


...

// If the user wants to add a joke
If (isset ($ addjoke )):
?>

Else:
// Connect to the database server
$ Dbcnx = @ mysql_connect (
"Localhost", "root", "mypasswd ");
If (! $ Dbcnx ){
Echo ("

Unable to connect to ".
"Database server at this time.

");
Exit ();
}
// Select the jokes database
If (! @ Mysql_select_db ("jokes ")){
Echo ("

Unable to locate the joke ".
"Database at this time.

");
Exit ();
}
// If a joke has been submitted,
// Add it to the database.
If ("SUBMIT" = $ submitjoke ){
$ SQL = "INSERT INTO Jokes SET ".
"JokeText = '$ joketext ',".
"JokeDate = CURDATE ()";
If (mysql_query ($ SQL )){
Echo ("

Your joke has been added.

");
} Else {
Echo ("

Error adding submitted joke :".
Mysql_error ()."

");
}
}
// If a joke has been deleted,
// Remove it from the database.
If (isset ($ deletejoke )){
$ SQL = "DELETE FROM Jokes ".
"Where id = $ deletejoke ";
If (mysql_query ($ SQL )){
Echo ("

The joke has been deleted.

");
} Else {
Echo ("

Error deleting joke :".
Mysql_error ()."

");
}
}
Echo ("

Here are all the jokes ".
"In our database:

");
// Request the ID and text of all the jokes
$ Result = mysql_query (
"Select id, JokeText FROM Jokes ");
If (! $ Result ){
Echo ("

Error memory Ming query :".
Mysql_error ()."

");
Exit ();
}
// Display the text of each joke in a paragraph
// With a "Delete this Joke" link next to each.
While ($ row = mysql_fetch_array ($ result )){
$ Jokeid = $ row ["ID"];
$ Joketext = $ row ["JokeText"];
Echo ("

$ Joketext ".
"".
"Delete this Joke

");
}
// When clicked, this link will load this page
// With the joke submission form displayed.
Echo ("

".
"Add a Joke!

");
Endif;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.