PHP query operation to implement the voting function, PHP query polling function
This example for everyone to share the PHP query operation to implement the voting function code for your reference, the specific content as follows
Topic:
Summary of solving methods:
Method One:
1. Poll Main Page:
Vote <?php//calculated total number of people: $sqlzs = "Select sum (Numbers) from Diaoyanxuanxiang where timudaihao= ' {$TMDH} '"; $zrs = $db->query ($ SQLZS); foreach ($arrxx as $v) { $name = $v [1]; $number = $v [2]; if ($zrs [0][0]==0) { $bfb = 0; } else { $BFB = ($number/$ZRS [0][0]) *100; } $BFB =round ($BFB, 2); echo "
"; }? >
2. Processing the polling 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 represents the type of $sql}header ("location:TouPiao.php"); >
3. Establish a class to access the database, encapsulated for reference:
<?php//executes an SQL statement that returns the corresponding result class dbda{public $host = "localhost";//database server address public $uid = "root";//Database user name Public $password = "";//Database Password //Execute SQL statement method //Parameter inside: $sql represents the SQL statement to execute; $type is the type of SQL statement, 0 for query, 1 for other (add-on); $ DB represents the database function Query to be manipulated ($sql, $type =0, $db = "MyDB") { //Build Connection object $dbconnect =new mysqli ($this- Host, $this->uid, $this->password, $db); Determine if the connection is wrong !mysqli_connect_error () or Die ("Connection failed! "); Execute SQL statement $result = $dbconnect->query ($sql); Determine the SQL statement type if ($type ==0) { //If the query statement returns a two-dimensional array of result sets, return $result->fetch_all (); } else { //If it is a different statement, returns TRUE or false return $result; }}}
Method Two:
1. Poll Main Page:
Vote
2. Processing the polling 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 represents the type of $sql}header ("location:TouPiao.php"); >
3. View the poll results page:
view results <?phpinclude (".. /dbda.class.php "); $db =new Dbda ();//Find out the title code and name $sql=" SELECT * from Diaoyantimu limit 0,1 "in the research topics table; $arr = $db->query ($ SQL), $tmmc = $arr [0][1];echo "{$TMMC}:
";//Export the option contents from the Survey options table: $SQLXX = "SELECT * from Diaoyanxuanxiang where timudaihao= ' {$arr [0][0]} ' "; $arrxx = $db->query ($SQLXX); Total count: $sqlzs = "Select sum (Numbers) from Diaoyanxuanxiang where timudaihao= ' {$arr [0][0]} '"; $zrs = $db->query ($ SQLZS); foreach ($arrxx as $v) {$name = $v [1];//research project Name $number = $v [2];//Select the number of people//determine if the total number is 0 if ($zrs [0][0]==0) {$BFB = 0; } else {$BFB = ($number/$ZRS [0][0]) *100;//percent} $BFB =round ($BFB, 2); Two digits after the decimal point echo "{$name} {$number} {$bfb}%
";}?
Show Results:
The above is the whole content of this article, I hope that you learn PHP programming help.
http://www.bkjia.com/PHPjc/1126074.html www.bkjia.com true http://www.bkjia.com/PHPjc/1126074.html techarticle PHP query operation to implement the voting function, PHP query voting function This example for everyone to share the PHP query operation to achieve the voting function of the code, for your reference, the specific contents of the following title: Solution ...