PHP and MySQL dynamic website development tutorial (3) _ MySQL

Source: Internet
Author: User
PHP and MySQL dynamic website development tutorial (3) is simple, isn't it? Edit a record from the database: Let's assume that we want to modify the existing record in the database. We can see that there is an SQL command called set to set the values of fields in the database. We will use this command to modify the entire record in the database. Consider the following script:
--------------------------------------------------------------------------------

Reference content is as follows:
Edit. php:
  
  Editing an entry from the database
  
  
Edit an entry Mysql_connect () or die ("Problem connecting to DataBase ");
$ Query = "select * from tbl ";
$ Result = mysql_db_query ("example", $ query );
If ($ result)
{
Echo "Found these entries in the database:
";
Echo"




















               ";While ($ r = mysql_fetch_array ($ result )){$ Idx = $ r ["idx"];$ User = $ r ["UserName"];$ Last = $ r ["LastName"];$ Text = $ r ["FreeText"];Echo"                ";}Echo"
IdxUser NameLast NameFree Text

$ Idx
$ User$ Last$ Text
";
}
Else
{
Echo "No data .";
}
Mysql_free_result ($ result );
Include ('links. X ');
?>
  
  


--------------------------------------------------------------------------------
As you can see, the code here is somewhat familiar. The first part only prints the table content in the database. Note that one line is not the same:
$ Idx
This line creates a link to editing. php3 and passes some variables to the new script. Similar to the form, only links are used. We convert the information to: variable and value. Note: to print out the "symbol, we need to use/". Otherwise, the server will regard it as part of the PHP script and use it as printed information.
We want to convert all the records in the database, so that we can get the exact data in the table so that it is easier to modify it.
--------------------------------------------------------------------------------

Reference content is as follows:
Editing. php
  
  Editing an entry
  
  
Editing an entry
  
  
  
  


--------------------------------------------------------------------------------
  
Okay. this script is simple. When a form is printed, it records the data of the current record.Command. The data is transmitted from the previous page.
Now, if we do not change the record information, it will return the current value, that is, the default value. If we change the field value, the field value will become a new value. Next we can pass the new value to another script, which will change the value in the MySQL table.
--------------------------------------------------------------------------------
Editdb. php:

Reference content is as follows:
   Mysql_connect () or die ("Problem connecting to DataBase ");
$ Query = "update tbl set
Idx = '$ idx', UserName =' $ username', LastName = '$ Lastname', FreeText =' $ FreeText 'where
Idx = '$ idx '";
$ Result = mysql_db_query ("example", $ query );
$ Query = "SELECT * FROM tbl ";
$ Result = mysql_db_query ("example", $ query );
If ($ result)
{
Echo "Found these entries in the database:

";
Echo"


















               ";While ($ r = mysql_fetch_array ($ result )){$ Idx = $ r ["idx"];$ User = $ r ["UserName"];$ Last = $ r ["LastName"];$ Text = $ r ["FreeText"];Echo"                ";}Echo"
IdxUser NameLast NameFree Text
$ Idx$ User$ Last$ Text
";
}
Else
{
Echo "No data .";
}
Mysql_free_result ($ result );
  
Include ('links. X ');
?>

--------------------------------------------------------------------------------
Basically, one thing to care about is the following line:
$ Query = "update tbl set idx = '$ idx', UserName =' $ username', LastName = '$ Lastname ', freeText = '$ FreeText' where idx = '$ idx '";
Note that it is the same as the syntax we explained in the previous MySQL section. Another thing is that this script changes the idx = $ idx record. if there are multiple records with idx equal to $ idx in the table, these records will be changed. If we want to be stricter, we can change the where clause as follows:
$ Query = "update tbl set idx = '$ idx', UserName =' $ username', LastName = '$ Lastname ', freeText = '$ FreeText' where idx = '$ idx' and UserName =' $ username' and LastName = '$ Lastname' and FreeText =' $ FreeText '";
This syntax checks all fields, not just idx.
Delete a record from the database:
Yes, it is easy to delete. We still need two scripts: one for selecting the record to be deleted (basically the same as the record selected for editing) and the other for deleting and printing new tables.
--------------------------------------------------------------------------------

Reference content is as follows:
Del. php
  
  Deleting an entry from the database
  
  
Del an entry
   Mysql_connect () or die ("Problem connecting to DataBase ");
$ Query = "select * from tbl ";
$ Result = mysql_db_query ("example", $ query );
If ($ result)
{
Echo "Found these entries in the database:

";
Echo"


















               ";While ($ r = mysql_fetch_array ($ result )){$ Idx = $ r ["idx"];$ User = $ r ["UserName"];$ Last = $ r ["LastName"];$ Text = $ r ["FreeText"];Echo"                ";}Echo"
IdxUser NameLast NameFree Text

Idx = $ idx & UserName = $ user & LastName = $ last & FreeText = $ text/"> $ idx
$ User$ Last$ Dtext
";
}
Else
{
Echo "No data .";
}
Mysql_free_result ($ result );
Include ('links. X ');
?>
  
  


--------------------------------------------------------------------------------
This script is very similar to the edited script we have used, so let's take a look at the instructions.

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.