Php query voting exercises (0508), php0508

Source: Internet
Author: User

Php query voting exercises (0508), php0508

Exercise questions:

Solution:

Method 1:

1. Voting homepage:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

3. Create a database access class and encapsulate it for reference:

<? Php // execute an SQL statement and return the corresponding result class DBDA {public $ host = "localhost"; // database server address public $ uid = "root "; // database username public $ password = ""; // Database password // The method for executing the SQL statement // $ SQL indicates the SQL statement to be executed; $ type is the type of SQL statement. 0 indicates Query, and 1 indicates other statements (add, delete, modify, and delete). $ db indicates the function Query of the database to be operated ($ SQL, $ type = 0, $ db = "mydb") {// create a connection object $ dbconnect = new MySQLi ($ this-> host, $ this-> uid, $ this-> password, $ db); // determines whether the connection has an error! Mysqli_connect_error () or die ("connection failed! "); // Execute the SQL statement $ result = $ dbconnect-> query ($ SQL); // determine the SQL statement type if ($ type = 0) {// if it is a two-dimensional array return $ result-> fetch_all ();} else {// if it is another statement, returns true or falsereturn $ result ;}}}

  Method 2:

1. Voting homepage:

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

2. process the voting page:

<? Php $ arr = $ _ POST ["xx"]; include (".. /DBDA. class. php "); $ db = new DBDA (); foreach ($ arr as $ v) {$ SQL = "update diaoyanxuanxiang set numbers = numbers + 1 where ids = '{$ v}'"; $ db-> Query ($ SQL, 1 ); // 1 indicates the $ SQL type} header ("location: TouPiao. php ");?>

3. view the voting result page:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

  

 

Webpage display result:

 

 

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.