First Step: Database connection
<php // CREATE database connection error_reporting(e_all & ~e_ DEPRECATED); @mysql_connectdie ("Database connection error"); // Connect database address user name password @mysql_select_dbdie ("DB connection Failed"); // Connection Library mysql_query ("Set names ' GBK '"); // Code ?>
The second part interacts with the database
A, in the database interaction we need to be divided into front-end and back-end. The front end is used for the user's presentation. Back end for database invocation
First write the front-end display code as follows:
1 <formAction= "add.php"Method= "POST">2Title<inputtype= "text"name= "title"><BR>3Content<textareaRow= "5"cols= " the"name= "Con"></textarea><BR><!--TextArea is a multi-line input row for showing that you can display a few lines cols for setting width--4 <inputtype= "Submit"name= "Sub"value= "Submit"><!--Submit button --5 </form>
HTML code Explanation
<!--textarea is a multi-line input row for showing that you can display a few lines cols for setting width--
B, the front-end display completed the front-end input to make a judgment is not empty. And the database to do the backstage handover, like we insert the data
1<?PHP2 include(' conn.php ');3 if(!Empty($_post[Sub])) {//Empty to determine if NULL!empty indicates NOT NULL4 $title=$_post[' title '];5 $con=$_post[' Con '];6 $sql= "INSERT into ' news ' (' id ', ' title ', ' dates ', ' contents ') VALUES (null, '$title', now (), '$con‘)";//mysql statement, inserting data7 $ret=mysql_query($sql);8 Echo"Insert Succeeded"Ten } A?>
PHP Learning Connection Database Micro Blog