Php/mysql 3rd-The next day (iii)

Source: Internet
Author: User
Tags mysql printf
V. Modification of data

In a tutorial, I put the SQL statement to execute into a variable ($sql) and then use mysql_query () to execute the database query. This is useful when debugging. If something is wrong with your program, you can always display the contents of the SQL statement and check for grammatical errors.

We've learned how to insert data into a database. Now let's learn how to modify the records that are already in the database. The editing of the data consists of two parts: the data display and the data returned to the database through the form input, both of which we have already mentioned. However, data editing is a little bit different, and we have to show the relevant data in the table first.

First, we'll look back at the first lesson's program code, and display the employee's name in the Web page. But this time, we're going to show the data in the table. The program looks something like the following:

 

  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;? Php

$db = mysql_connect ("localhost", "root");

mysql_select_db ("MyDB", $db);

if ($id) {


Querying the database

$sql = "SELECT * FROM Employees WHERE id= $id";

$result = mysql_query ($sql);

$myrow = Mysql_fetch_array ($result);

? $#@62;

$#@60;form method= "POST" action= "$#@60;? PHP echo $PATH _info?$#@62; " $#@62;

$#@60;input type=hidden name= "id" value= "$#@60;? PHP echo $myrow ["id"]? $#@62; " $#@62;

Name: $#@60;input type= "Text" Name= "the" "value=" $#@60;? PHP Echo
$myrow ["$#@62"]? $#@62;$#@60;br$#@62;

Surname: $#@60;input type= "Text" Name= "Last" value= "$#@60;? PHP Echo
$myrow ["Last"]? $#@62; " $#@62;$#@60;br$#@62;

Address: $#@60;input type= "Text" name= "Addresses" value= "$#@60;? PHP Echo
$myrow ["Address"]? $#@62; " $#@62;$#@60;br$#@62;

Position: $#@60;input type= "Text" name= "position" value= "$#@60"? PHP Echo
$myrow ["position"]? $#@62; " $#@62;$#@60;br$#@62;

$#@60;input type= "Submit" name= bmit value= "input information" $#@62;

$#@60;/form$#@62;

$#@60;? Php

} else {


Show list of employees

$result = mysql_query ("SELECT * FROM Employees", $DB);

while ($myrow = Mysql_fetch_array ($result)) {

printf ("$#@60;a href=\"%s?id=%s\ "$#@62;%s%s$#@60;/a$#@62;$#@60;br$#@62;\n", $PATH _info,
$myrow ["id"], $myrow ["a"], $myrow ["last"]);

}

}

? $#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

We have just written the contents of the field into the value attribute of the corresponding table element, which is correspondingly simple. Let's go further and make it possible for the program to write the user's modified content back to the database. Again, we use the Submit button to determine if the table entry is processed. Also note that the SQL statements we use are slightly different.

$#@60;html$#@62;

$#@60;body$#@62;

$#@60;? Php

$db = mysql_connect ("localhost", "root");

mysql_select_db ("MyDB", $db);

if ($id) {

if ($submit) {

$sql = "UPDATE employees SET first= $first, last= $last,
address= $address, position= $position where id= $id ";

$result = mysql_query ($sql);


echo "Thank you!" Data change complete \ n ";

} else {


Querying the database

$sql = "SELECT * FROM Employees WHERE id= $id";

$result = mysql_query ($sql);

$myrow = Mysql_fetch_array ($result);

? $#@62;

$#@60;form method= "POST" action= "$#@60;? PHP echo $PATH _info?$#@62; " $#@62;

$#@60;input type=hidden name= "id" value= "$#@60;? PHP echo $myrow ["id"]? $#@62; " $#@62;


Name: $#@60;input type= "Text" Name= "the" "value=" $#@60;? Php
echo $myrow ["a"] $#@62; $#@62;$#@60;br$#@62;


Surname: $#@60;input type= "Text" Name= "Last" value= "$#@60;? PHP Echo
$myrow ["Last"]? $#@62; " $#@62;$#@60;br$#@62;


Address: $#@60;input type= "Text" name= "Addresses" value= "$#@60;? PHP Echo
$myrow ["Address"]? $#@62; " $#@62;$#@60;br$#@62;


Position: $#@60;input type= "Text" name= "position" value= "$#@60"? PHP Echo
$myrow ["position"]? $#@62; " $#@62;$#@60;br$#@62;


$#@60;input type= "Submit" name= "submit" value= "input information" $#@62;

$#@60;/form$#@62;

$#@60;? Php

}

} else {


Show list of employees

$result = mysql_query ("SELECT * FROM Employees", $DB);

while ($myrow = Mysql_fetch_array ($result)) {

printf ("$#@60;a href=\"%s?id=%s\ "$#@62;%s%s$#@60;/a$#@62;$#@60;br$#@62;\n", $PATH _info,
$myrow ["id"], $myrow ["a"], $myrow ["last"]);

}

}

? $#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

That's it. This program already contains most of the features we've learned. You have also seen that we have added an if () statement to check for multiple conditions in an if () conditional discriminant statement.

Next, we're going to add everything together and write a good program.



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.