Using PHP to add data to the database

Source: Internet
Author: User

Display page (user visible)

<body>
<form action= "chuli.php" method= "POST" >//Data received from this page to the Chuli page for processing
<div> Nationality Code: <input type= "text" name= "code"/></div>//Create a div and put in the receiver to receive the code value
<div> Nationality Name: <input type= "text" name= "name"/></div>//Create a div and put in the receiver to receive the name value
<input type= "Submit" value= "Submission"/>
</form>

</body>

Action page (user not visible)

<?php//Processing page just keep the PHP code

$code = $_post["code"]; Set a variable to receive user-submitted code values
$name = $_post["name"]; Set a variable to receive the user-submitted name value

Build connection
$conn = @mysql_connect ("localhost", "root", "123"); LocalHost is the native server address, root is the user name, and 123 is the database password
Select the Operational database
mysql_select_db ("< database name >", $conn);
Write SQL statements
$sql = "INSERT into Nation values ('{$code}','{$name}   ') "; Add data to the database, if it is an integer number or a Boolean type cannot be enclosed in single quotes, use only the Vachar type to enclose the single quotation mark

Perform
$result = mysql_query ($sql);

if ($result)//Create a conditional statement to remind the programmer whether the data was added successfully
{
Jump page
Header ("location:lizi.php"); Add success, then jump back to Lizi page, that is, user input page
}
Else
{
echo "Add failed!              "; Add failed to display prompt message, add failed
}

Using PHP to add data to the database

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.