PHP simple implementation of MySQL data search, add data functions

Source: Internet
Author: User

Database TestDB, table user column email and nikename

HTML code:

<form method= "POST" action= "search1.php" name= "search" >  <input name= "Search" type= "text" value= "" size= " >   <input type= "Submit" value= "Search" > </form> the  following adds:...</p> to the database  <div> <form action= "insert.php" method= "POST" >nikename: <input type= "text" name= "Nikename"/>email: <input Type= "text" name= "email"/><input type= "Submit"/></form></div>

search1.php Code:

<?php$con = mysql_connect ("localhost", "root", "root"), if (! $con)  {die  (' Could not connect: '. Mysql_error () );  } mysql_select_db ("TestDB", $con); $searchs = $_post[' search '];  The value passed in the search box $searchs = Trim ($searchs); the IF (! $searchs) {echo ' search box cannot be empty. '; Exit;} $result = mysql_query ("SELECT * from user where nikename like '% $searchs% '"); if (Mysql_num_rows ($result) < 1) echo ' No number According to '; while ($row = Mysql_fetch_array ($result)) {  echo $row [' Nikename ']. " " . $row [' email '];  echo "<br/>";  } Mysql_close ($con);? >

The insert.php code is as follows:

<?php$con = mysql_connect ("localhost", "root", "root"), if (! $con)  {die  (' Could not connect: '. Mysql_error () );  } mysql_select_db ("TestDB", $con); $nikename =$_post[' nikename '];  Create short variable names$email=$_post[' email ']; $nikename = Trim ($nikename); if (! $nikename | |! $email)//determine if NULL {echo ' cannot be empty '; exit;} $sql = "INSERT into user (nikename, email) VALUES (' $nikename ', ' $emai L ') ";  if (!mysql_query ($sql, $con))  {die  (' Error: '. mysql_error ());  } echo "1 record added"; Mysql_close ($con)?>

PHP simple implementation of MySQL data search, add data functions

Related Article

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.