24 common PHP codes

web front end six thousand six hundred and forty-nine 13 years ago (2011-04-27)
1. Connect MYSQL data base code
<? php
$connec= mysql _Connect ("localhost", "root", "root") or die ("Cannot connect to the database The server : ". mysql _error());
Mysql_select_db ("liuyanben", $connect) or die ("Cannot select database:". mysql_error());            
mysql_query("set names 'gbk'");
?>
2. Read the database and realize cyclic output
<? php
$sql="select * from liuyan order by ly_id desc";
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){   
?>
<table width="476" border="1" cellspacing="0" cellpadding="0">
    <tr>
    <td width="135" height="30">< div Align="center">Leave a message theme </ div ></td>
     <td width="204" height="30"><div align="center">Message time</div></td>
    <td width="129" height="30"><div align="center">Message takers</div></td>
    </tr>
    <tr bgcolor="#CC9999">
    <td height="30"><?= $ rs["ly_title"]?>
        <div align="center"></div></td>
    <td height="30"><?= $ rs["ly_time"]?>
        <div align="center"></div></td>
    <td height="30"><?= $ rs["ly_author"]?>
        <div align="center"></div></td>
    </tr>
    <tr>
    <td height="30"><div align="center"><a href="huifu. php? Id=<? Php echo $rs [" ly_id "]?>">Reply to comments</a></div></td>
    <td height="30"><div align="center"><a href="viewhuifu. php? Id=<? Php echo $rs [" ly_id "]?>">View reply information</a></div></td>
    <td height="30"><div align="center">There are already replies</div></td>
    </tr>
</table>
<? php
}
?>

3. How to implement paging, including two functions and two calls
1) Two functions
<?
//Paging function

function genpage(&$sql,$page_size=2)
{
        global $prepage,$nextpage,$pages,$sums;     //out param
        $page = $_GET["page"];
        $eachpage = $page_size;
        $pagesql = strstr($sql," from ");
        $pagesql = "select count(*) as ids ".$ pagesql;
        $conn = mysql_query($pagesql) or die(mysql_error());
        if($rs = mysql_fetch_array($conn)) $sums = $rs[0];
        $pages = ceil(($sums-0.5)/$eachpage)-1;
        $pages = $pages>=0?$ pages:0;
        $prepage = ($page>0)?$ page-1:0;
        $nextpage = ($page<$pages)?$ page+1pages;    
        $startpos = $page*$eachpage;
    $sql .= " limit $startpos,$eachpage ";
}
//Show Pagination
function showpage()
{
    global $page,$pages,$prepage,$nextpage,$queryString; // param from genpage function
    $shownum =10/2;
    $startpage = ($page>=$shownum)?$ page-$shownum:0;
    $endpage = ($page+$shownum<=$pages)?$ page+$shownum pages;
   
    Echo "total" ($pages+1). "Page:";
    If ($page>0) echo "<a href=$PHP_SELF? Page=0 $queryString>Home</a>";
    if($startpage>0)
            echo " ... <b><a href=$PHP_SELF?page=". ($page-$shownum*2). "$queryString>«</a></b>";
    for($i=$startpage;$i<=$endpage;$i++)
    {
            if($i==$page)    echo " <b>[".($i+1)."]</b> ";
            else            echo " <a href=$PHP_SELF?page=$i$queryString>". ($i+1). "</a> ";
    }
    if($endpage<$pages)
            echo "<b><a href=$PHP_SELF?page=". ($page+$shownum*2). "$queryString>»</a></b> ... ";
    if($page<$pages)
            Echo "<a href=$PHP_SELF? Page=$pages $queryString>End Page</a>";

}

//Display Band Score class Pagination of
function showpage1()
{
            $fenlei=$_GET["fenleiid"];
    global $page,$pages,$prepage,$nextpage,$queryString; // param from genpage function
    $shownum =10/2;
    $startpage = ($page>=$shownum)?$ page-$shownum:0;
    $endpage = ($page+$shownum<=$pages)?$ page+$shownum pages;
   
    Echo "total" ($pages+1). "Page:";
    If ($page>0) echo "<a href=$PHP_SELF? Fenleiid=$fenlei&page=0 $queryString>Home</a>";
    if($startpage>0)
            echo " ... <b><a href=$PHP_SELF?fenleiid=$fenlei&page=". ($page-$shownum*2). "$queryString>«</a></b>";
    for($i=$startpage;$i<=$endpage;$i++)
    {
            if($i==$page)    echo " <b>[".($i+1)."]</b> ";
            else            echo " <a href=$PHP_SELF?fenleiid=$fenlei&page=$i$queryString>". ($i+1). "</a> ";
    }
    if($endpage<$pages)
            echo "<b><a href=$PHP_SELF?fenleiid=$fenlei&page=". ($page+$shownum*2). "$queryString>»</a></b> ... ";
    if($page<$pages)
            Echo "<a href=$PHP_SELF? Fenleiid=$fenlei&page=$pages $queryString>End Page</a>";

}
?>

2) Two calls
first
<? php
$sql="select * from liuyan order by ly_id desc";
genpage($sql);     //Just add this line to the normal code.
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){   
?>
the second
<? php
}
?>
<? php   
            showpage(); // Show Page
?>
            <? php
            mysql_close();
                                           
            ?>

4. Server side contains
<? php require_once('conn.php'); ?>

5. How to write a record to the database, and then prompt and jump to the page
<? php
$ly_title=$_POST["ly_title"];
$ly_content=$_POST["ly_content"];
$ly_time=$_POST["ly_time"];
$ly_author=$_POST["ly_author"];
$ly_ email =$_POST["ly_ email "];

$sql="insert into liuyan(ly_title,ly_content,ly_time,ly_author,ly_email) values('".$ly_title."','".$ly_content."','".$ly_time."','".$ly_author."','".$ly_email."')";
mysql_query($sql,$connec);
Echo ("<script type='text/javascript '>alert ('Add successfully!'); location. href='index. php ';</script>");
?>

6. A dialog box pops up and page jump occurs
<? php
Echo ("<script type='text/javascript '>alert ('Add successfully!'); location. href='index. php ';</script>");
?>

7. Information view page (conditionally read the database)
1) Read database conditionally
<? php
$sql="select * from liuyan where ly_id=$_GET[id]";
$conn=mysql_query($sql,$connec);
$rs=mysql_fetch_array($conn);
?>
2) Output a field
<?=$ rs[ly_title]?>
3) Close database
<? php
            mysql_close();                                
            ?>

8. Update a record in the database and give a prompt to jump
<? php
$ly_title=$_POST["ly_title"];
$ly_content=$_POST["ly_content"];
$ly_time=$_POST["ly_time"];
$ly_author=$_POST["ly_author"];
$ly_email=$_POST["ly_email"];

$sql="update liuyan set ly_title='$ly_title',ly_content='$ly_content',ly_time='$ly_time',ly_author='$ly_author',ly_email='$ly_email' where ly_id=$_GET[id]";
mysql_query($sql,$connec);
Echo ("<script type='text/javascript '>alert ('Updated successfully!'); location. href='../index. php';</script>");
?>

9. How to delete a record in the database
<? php

$sql="delete from liuyan where ly_id=$_GET[id]";
mysql_query($sql,$connec);
Echo ("<script type='text/javascript '>alert ('deleted successfully!'); location. href='../index. php';</script>");
?>

10. How to verify member login
<? php
session_start();
$username=$_POST["username"];
$password=$_POST["password"];

$sql="select * from admin where username='".$ username. "' && password='".$ password. "'";
$result=mysql_query($sql,$connec);
if($row=mysql_fetch_array($result)){
session_register("admin");
$admin=$username;
Echo ("<script type='text/javascript '>alert ('Login succeeded!'); location. href='admin. php ';</script>");}
else
{
Echo ("<script type='text/javascript '>alert ('The user name or password you entered is incorrect, please re-enter!'); location. href='login. php ';</script>");
}

mysql_close();

?>

11. How to check SESSION (production of background check page)
<? php
session_start();
if(!isset($_SESSION["admin"])){
header("location:login.php");
exit;
}
?>

12. Verify whether the user name and password are filled in (javascript)
<SCRIPT language=javascript>
<!--
function confirmlogin()
{
    if (document.frmmain.username.value.length<4 || document.frmmain.username.value=="")
    {
        document.frmmain.username.focus();
        document.frmmain.username.select;
        Window. alert ("Please enter your user name!");
        return false;
    }
    if (document.frmmain.password.value.length<4)
    {
        document.frmmain.password.focus();
        document.frmmain.password.select;
        Window.alert ("Please enter your password!");
        return false;    
    }
    return true;
}

//-->
</SCRIPT>
?>

13. Called in PHP editor Method of

    1) Place the editor folder in the background management folder.
    2) Use the following statement to import.

<input name="content" type="hidden" value=''>
<IFRAME ID="eWebEditor1" src="eWebEditorPHP38/ ewebeditor .htm? id=content&style=coolblue" frameborder="0" scrolling="no" width="550" height="350"></IFRAME>
Note: The name of the eWebEditorPHP38 editor folder.
    In id=content, content is the name of the hidden field above

14. Cyclic output (capable of splitting)
    1) Insert a table with one row and one column first
<? php
$i=1;
?>    
<table>
    <tr>
    <? php
    while($rs=mysql_fetch_array($conn)){   
    ?>        
    <td>
    Other tables and outputs to be cycled
    </td>
    <? php
        if ($i % 2==0) {
            echo "</tr><tr>";
            }
            $i++;
                }
                    ?>            
    </tr>
    </table>

15. Bind data to the drop-down list box (selected by default when modifying)
<select name="fenleiid">
<? php
$sql="select * from fenleibiao";
$conn=mysql_query($sql,$connec);
while($rs1=mysql_fetch_array($conn)){   
?>
                               
<option value="<?=$rs1["fenleiid"]?>"
<?
if ($rs["fenleiid"]==$rs1["fenleiid"]){
            echo "selected" ;
            }
            ?>>
            <?=$ rs1["flname"]?>
            </option>
            <? php>
}
    ?>
            </select>

16. Get character length function
strlen($c)>12

17. Define a character interception function
Usage:<= substrgb($rs["title"],10)?>
function substrgb($in,$num){
                    $pos=0;
                    $out="";
                    while($c=substr($in,$pos,1)){
                                if($c=="n") break;
                                if(ord($c)>128){
                                            $out.=$ c;
                                            $pos++;
                                            $c=substr($in,$pos,1);
                   
                    $out.=$ c;
                                }else{
                                            $out.=$ c;
                                }
                                $pos++;
                                if($pos>=$num) break;
                    }
                    if($out!=$in) $out = $out . "...";
                    return $out;
            }

18. Judge whether it is a number
! is_numeric(qq)

19. Get the current date in PHP technology
$ptime=date("y-m-d");

20. The PHP validator used for user registration
if ($admin==""    or (strlen($admin)>16) or (strlen($admin)<2)) {
        Echo "<SCRIPT language=JavaScript>alert ('Please enter the user name (no more than 16 and less than 2) ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
}

if ($password=="" or strlen($password)>16 or strlen($password)<6) {
        Echo "<SCRIPT language=JavaScript>alert ('password length is 6-16 characters');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
}

if ($password=="") {
        Echo "<SCRIPT language=JavaScript>alert ('confirmation password cannot be empty ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
}else{
            if ($password!=$password1) {
        Echo "<SCRIPT language=JavaScript>alert ('Password and confirmation password are inconsistent ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
            }
}

if ($wt="") {
        Echo "<SCRIPT language=JavaScript>alert ('Password question cannot be empty ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
}

if ($da="") {
        Echo "<SCRIPT language=JavaScript>alert ('The answer to the question cannot be empty ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
}

if ($qq!="") {
    if (!is_numeric($qq)) {
        Echo "<SCRIPT language=JavaScript>alert ('QQ number must be a number ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
    }
}

if ($youbian==""    or strlen($youbian)!= 6)    {
        Echo "<SCRIPT language=JavaScript>alert ('Please enter the zip code correctly ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
}

if ($youbian!="") {
    if (!is_numeric($youbian)) {
        Echo "<SCRIPT language=JavaScript>alert ('Postal code must be a number ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
    }
}

if ($dizhi="") {
        Echo "<SCRIPT language=JavaScript>alert ('Address cannot be empty ');";
        echo"this.location.href='vbscript:history.back()';</SCRIPT>";
       
}

if ($mail=="") {
            Echo "<SCRIPT language=JavaScript>alert ('E-mail cannot be empty! ');";
            echo "this.location.href='vbscript:history.back()';</SCRIPT>";
           
         

if ($textarea=="") {
            Echo "<SCRIPT language=JavaScript>alert ('Personal description cannot be empty! ');";
            echo "this.location.href='vbscript:history.back()';</SCRIPT>";
           
}

if ($textarea==""    or strlen(textarea)>150) {
    Echo "<SCRIPT language=JavaScript>alert ('Personal description is 150 characters');";
    echo"this.location.href='vbscript:history.back()';</SCRIPT>";
   
}


21. Online survey front page reference
<table width="100%"    border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td background="images/bj_r1_c1.jpg">
                        <? php
$sql1="select * from voteclass where active=1";
$conn1=mysql_query($sql1,$connec);
$rs1=mysql_fetch_array($conn1);
?>
                        <form name="form3" method="post" action="vote_save.php">
                       
                <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
                    <tr>
                    <td><div align="center"><?=$ rs1[voteclass]?></ div></td>
                    </tr>
                                                            <? php
$sql2="select * from votexiang where vc_id=".$ rs1[vc_id];
$conn2=mysql_query($sql2,$connec);
?>
                                    <tr>
                    <td>
                                           
                                                <? php
                                                    If ($rs1 ["class"]=="single choice"){
                                                    while($rs2=mysql_fetch_array($conn2)){    ?>
                                                    <input name="vote" type="radio" value="<?=$rs2[votexiang]?>">
                                               
                                                <?=$ rs2[votexiang]?>
                                                <br>
                                                <? php
                                                    }
                                                    }else{
                                                    ?>
                                                   
                                                        <? php
                                                        $i=1;
                                                        while($rs2=mysql_fetch_array($conn2)){   
                                                        ?>
                                                    <input name="vote<?=$i?>" type="checkbox" value="<?=$rs2[votexiang]?>">
                                               
                                                <?=$ rs2[votexiang]?>
                                                    <input name="shu" type="hidden" value="<?=$i?>">
                                                <br>
                                                <? php
                                                    $i++;
                                                    }
                                                    }
                                                    $id=$rs1["vc_id"]
                                                    ?>
                                        </td>
                    </tr>
                    <tr>
                    <td><input name="imageField2" type="image" src="images/votetj.jpg" width="59" height="22" border="0"> 
                        <a href="voteview. php? Id=<?=$id?>" target="_blank">View the results</a></td>
                    </tr>
                </table>
                            </form>
            </td>
            </tr>
        </table>

22. Reference code of online survey processing page
<? php
$shu=$_POST["shu"];
$vote=$_POST["vote"];
if ($shu!=""){
for ($i=1;$i<=$shu;$i++){
$votexiang.=$_ POST["vote$i"];
}
if ($votexiang!=""){
                    for ($i=1;$i<=$shu;$i++){
                    if ($_POST["vote$i"]!=""){
                    $sql="update votexiang set voteshu=voteshu+1 where votexiang='".$_ POST["vote$i"]. "'";
                    mysql_query($sql,$connec);
                    Echo ("<script type='text/javascript '>alert ('Add successfully!'); location. href='index. php ';</script>");
                    }
                    }
}else{
Echo ("<script type='text/javascript '>alert ('no voting item selection!'); location. href='index. php ';</script>");
}
}
if ($vote!="") {
$sql="update votexiang set voteshu=voteshu+1 where votexiang='".$_ POST["vote"]. "'";
mysql_query($sql,$connec);
Echo ("<script type='text/javascript '>alert ('Add successfully!'); location. href='index. php ';</script>");
}else{
Echo ("<script type='text/javascript '>alert ('no voting item selection!'); location. href='index. php ';</script>");
}
?>

23. Reference code of online survey results page
<p align="center">Welcome to view the voting results</p>
<? php
$sql="select * from votexiang where vc_id=".$_ GET["id"];
$conn=mysql_query($sql,$connec);
?>
<table width="600" border="1" bordercolordark="#0000CC" bordercolorlight="#FFFFFF" cellspacing="0" cellpadding="0">
    <tr>
    <td width="103" height="25"><div align="center"><span class="STYLE1">Project Name</span></div></td>
    <td width="89" height="25"><div align="center"><span class="STYLE1">Number of votes</span></div></td>
    <td width="408" height="25"><div align="center"><span class="STYLE1">Scale</span></div></td>
    </tr>
    <? php
    $sum=0;
    while($rs=mysql_fetch_array($conn)){
    $sum+=$rs["voteshu"];
    }
   
    $conn=mysql_query($sql,$connec);
    while($rs1=mysql_fetch_array($conn)){
    $voteshu=(int)($rs1["voteshu"]/$sum*100);
    $pic_width=$voteshu*778/100;
?>
    <tr>
    <td height="25"><?=$ rs1["votexiang"]?></ td>
    <td height="25"><?=$ rs1["voteshu"]?></ td>
    <td height="25"><img src="images/left1.gif" height="20" width="<?=$pic_width?>">
            <?=$ voteshu?>%
            </td>
    </tr>
    <? php }?>
</table>

24. Judge the output content to output other results
<? php
            if ($rs["active"]==1) {
            Echo "<font color='# ff0000'>Activate</font>";
            }else{
            Echo "Disable";
            }
              
?>