PHP Basic Knowledge Summary (iv) Message Board example Knowledge Application

Source: Internet
Author: User
Tags sql error

1, Message Board Display page: note.php

<?PHP$host= "127.0.0.1";$user= "Root";$pwd= "[Email protected]";$dbname= "PhP10";$db=NewMysqli ($host,$user,$pwd,$dbname);if($db->errno <> 0){    Echo"Database connection Failed"; Exit;}$sql= "SELECT * from MSG where id>15 ORDER by id DESC";$mysqli _result=$db->query ($sql);if($mysqli _result==false){    Echo"Query statement execution failed"; Exit;}$rows= []; while($row=$mysqli _result-Fetch_array (MYSQLI_ASSOC)) {    $rows[] =$row;}//Var_dump ($rows);?>wrap{Width:800px; Margin:0px Auto; }            .add{Overflow: Hidden; <!--eliminate float-->            }            . Add.content{Width:798px; Margin:0px; Padding:0px; }            . Add.user{float:Left ; }            . Add.btn{float:Right ; }            .msg{margin:20px 0px; Background:#CCC;Padding5px; }            . Msg.info{Overflow:hidden; }            . Msg.user{float:Left ; Color:Blue; }            . Msg. Time{                float:Right ; Color: 999; }            . Msg.content{Width: 100%; }        </style> class= "Wrap" > <!--comments-<divclass= "Add" > <form action= "messageboard_class.php" method= "POST" > <textarea name= "Content"class= "Content" cols= "rows=" 5 "></textarea> <br/> <input name=" User "class= "User" type= "text"/> <inputclass= "BTN" type= "submit" value= "Leave a message"/> </form> </div> <!--view Message-- <?PHP//Set the date time zone, or it will be 7 hours lessDate_default_timezone_set ("Asia/shanghai"); foreach($rows  as $key=$record) {                                    ?> <divclass= "MSG" > <divclass= "Info" > <spanclass= "User" ><?phpEcho $record["User"];?></span> <spanclass= "Time" ><?phpEcho Date(' Y-m-d h:i:s ',$record["Intime"]); ?></span> </div> <divclass= "Content" > <?phpEcho $record["Content"];?> </div> </div> <?php}?> </div> </body>

2, submit the message background processing: messageboard_class.php

<?PHPinclude("messageboard_class_import.php"); Filter the name of the message classinclude("connect.php"); Database Connection Tool Classes//Var_dump ($_post);$content=$_post[' Content '];$user=$_post[' User '];//Initialize the input class object using the classes in the Include file$input=Newinput ();if($input-Valid ($content) ==false){     die("The input cannot be empty!") ");//Die Method: Stop the program and display the content on the page}if($input-Valid ($user) ==false){     die("The entered user name is empty or illegal!") ");}//Var_dump ($_post);/*INSERT Statement*///Set the date time zone, or it will be 7 hours lessDate_default_timezone_set ("Asia/shanghai");$time= Time();Echo Date("Y-m-d h:i:s",$time);$sql= "INSERT into MSG (content, user, Intime) values (' {$content}‘, ‘{$user}‘, ‘{$time}‘)";$connectClass=NewConclass ();$db=$connectClass-Getdb ();$is=$db->query ($sql);if($is==true){    Echo"Successful Execution";}Else{    Echo"Execution Failed";}//redirect browser accessHeader("Location:note.php");/*//Query statement $sql = "SELECT * from msg where ID > The ORDER by id desc"; $con = new Conclass (); $db = $con, Getdb (); $mys    Qli_result = $db->query ($sql); if ($mysqli _result = = False) {echo "SQL error"; Exit;}   Define an array $rows = [];while ($row = $mysqli _result->fetch_array (MYSQLI_ASSOC)) {$rows [] = $row;  Rows array does not write subscript, meaning starting from 0;}var_dump ($rows); Can be seen as a two-dimensional array, the key starts with 0, the value is the data of each row (one-dimensional array representation)*/?>

Filter the name of the message class messageboard_class_import.php

<?PHPclassinput{ Public functionValid$info){        if($info== ""){            return false; }        //prohibit use of a user name message        $note= [' Zhang San ', ' John Doe ', ' Wang Le ']; foreach($note  as $name=$value){            //echo $name;            if($info==$value){                return false; }        }        return true; }}?>

Database connection Tool Class connect.php

<?PHPclassconclass{//Write to Database     Public functionGetdb () {$host= "127.0.0.1"; $dbuser= "Root"; $passwd= "Zs[email protected]"; $dbname= "PhP10"; $db=NewMysqli ($host,$dbuser,$passwd,$dbname); if($db->errno <> 0){            return NULL; }        return $db; }    }?>

PHP Basic Knowledge Summary (iv) Message Board example Knowledge Application

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.