The following describes how to implement mysql form submission in php development to prevent repeated refreshes. If you have encountered similar problems before, take a look.
The database is jokes and the table is jokes. Add a joke as an example.
Mysql form submission page:
- <HTML>
- <HEAD>
- <TITLE> Our List of Jokes </TITLE>
- </HEAD>
- <BODY>
-
- <? Php
- Session_start (); // enable session
- $ Timetime = time ();
- $ Key = "sess _". $ time; // generate a random session key based on the time
- $ _ SESSION [$ key] = $ time; // sets the session value.
- ?>
-
- <Form action = "input. php? Tjid = 1 "METHOD = POST>
- <P> Type your joke here: <BR>
- <Textarea name = "joketext" ROWS = 10 COLS = 40 WRAP> </TEXTAREA> <BR>
- // Pass the session key to the server for processing by hiding the form
- <Input type = "hidden" name = "session_key" value = "<? Php echo $ key;?> "/>
- <Input type = submit name = "submitjoke" VALUE = "SUBMIT">
- </FORM>
-
- </BODY>
- </HTML>
-
- Processing page:
- <HTML>
- <HEAD>
- <TITLE> Our List of Jokes </TITLE>
- </HEAD>
- <BODY>
- // Connect to the database:
- <? Php
- $ Dbcnx = mysql_connect ("localhost", "root ","******");
- If (! $ Dbcnx ){
- Echo ("<P> Unable to connect to ".
- "Database server at this time. </P> ");
- Exit ();
- }
- // Select the jokes database
- If (! @ Mysql_select_db ("jokes ")){
- Echo ("<P> Unable to locate the joke ".
- "Database at this time. </P> ");
- Exit ();
- }
- // Start the session:
- Session_start ();
- $ Key = $ _ POST ['session _ key'];
- If (! $ Key | $ _ SESSION [$ key]! = Substr ($ key, 5 )){
- // If the session_key parameter is not passed
- // Or the number after the session_key parameter value truncates sess _ does not match the session parameter value
- Unset ($ _ SESSION [$ key]); // deletes the session value.
- Echo "chu cuo la"; // then consider whether an error is prompted, or transfer to another page
- Exit (); // terminate page Code Execution
- }
-
- // Write the data below
- If ("SUBMIT" ==$ _ POST ["submitjoke"]) {
- $ SQL = "INSERT INTO Jokes (joketext)
- VALUES ('$ _ POST [joketext]')
- ";
- If (mysql_query ($ SQL )){
- Echo ("<P> Your joke has been added. </P> ");
- } Else {
- Echo ("<P> Error adding submitted joke :".
- Mysql_error (). "</P> ");
- }
- // After the data operation is complete, delete the session
- Unset ($ _ SESSION [$ key]);
- }
- ?>
-
- // Display joke content:
- <P> Here are all the jokes in our database: </P>
- <BLOCKQUOTE>
- <? Php
- $ Result = mysql_query (
- "SELECT * FROM Jokes ");
- If (! $ Result ){
- Echo ("<P> Error checking Ming query :".
- Mysql_error (). "</P> ");
- Exit ();
- }
- // Display the text of each joke in a paragraph
- While ($ row = mysql_fetch_array ($ result )){
- Echo ("<P>". $ row ["JokeText"]. "</P> ");
- }
- ?>
-
- </BODY>
- </HTML>
-
Simple MySQL large table backup method
Implementation of MySQL table sharding
MySQL authorization table usage example
Disadvantages of MySQL memory tables
Advantages and disadvantages of MySQL independent tablespace