MySQL Inserting data

Source: Internet
Author: User
Tags learn php php script

MySQL Inserting data

Use the INSERT INTO SQL statement in the MySQL table to insert the data.

You can insert data into a data table by mysql> the command Prompt window, or insert data through a PHP script.

Grammar

The following is the INSERT into SQL syntax for inserting data into a MySQL data table:

( field1, field2,... )( value1, value2,... );

If the data is a character type, you must use either single or double quotation marks, such as "value".

Inserting data from a command prompt window

Below we will use the SQL insert into statement to insert data into the MySQL data table Tutorials_tbl

Instance

In the following example we will want to insert three data into the TUTORIALS_TBL table:

[Email protected]# mysql-u root-p password;EnterPassword:*******Mysql> UseTutorials;DatabaseChangedmysql>INSERT into Tutorials_tbl(Tutorial_title,Tutorial_author,Submission_date) -VALUES("Learn PHP", "John Poul.",Now());QueryOk, 1Row affected(0.01Sec)Mysql>INSERT into Tutorials_tbl(Tutorial_title,Tutorial_author,Submission_date) -VALUES("Learn MySQL", "Abdul S",Now());QueryOk, 1Row affected(0.01Sec)Mysql>INSERT into Tutorials_tbl-> (tutorial_title, tutorial_author< Span class= "pun" >, Submission_date)  -> values -> ( "JAVA Tutorial"   "Sanjay" ,  ' 2007-05-06 ' query Ok,1 row affected  (0.01 sec mysql>        

Note: using the arrow markers (--) is not part of the SQL statement, it simply represents a new row, and if an SQL statement is too long, we can write the SQL statement by using the ENTER key to create a new line, and the command terminator for the SQL statement is a semicolon (;).

In the above example, we do not provide tutorial_id data because we have set it to the Auto_increment (auto-increment) property when we create the table. Therefore, the field is automatically incremented without the need for us to set it. The instance now () is a MySQL function that returns the date and time.

Inserting data using a PHP script

You can use the PHP mysql_query () function to execute the SQL insert into command to insert data.

The function has two parameters, returns TRUE on successful execution, or FALSE.

Grammar
BOOL mysql_query( sql,);    
Parameters Description
Sql Necessary. Specifies the SQL query to send. Note: The query string should not end with a semicolon.
Connection Optional. Specifies the SQL connection identifier. If not specified, the previous open connection is used.
Instance

In the following instance, the program receives three field data entered by the user and inserts it into the data table:

<title>Add New Record in MySQL Database</title><body><?PhpIf(Isset($_post[' Add '])){$dbhost= ' localhost:3036 ';$dbuser= ' Root ';$dbpass= ' Rootpassword ';$conn=Mysql_connect($dbhost,$dbuser,$dbpass);If(!$conn){ Die(' Could not connect: ' .Mysql_error());}If(!Get_magic_quotes_gpc() ){$tutorial _title=Addslashes($_post[' Tutorial_title ']);$tutorial _author=Addslashes($_post[' Tutorial_author ']);}Else{$tutorial _title=$_post[' Tutorial_title '];$tutorial _author=$_post[' Tutorial_author '];}$submission _date=$_post[' Submission_date '];$sql= "INSERT into Tutorials_tbl". "(Tutorial_title,tutorial_author, Submission_date)". "VALUES". "(' $tutorial _title ', ' $tutorial _author ', ' $submission _date ')";mysql_select_db(' Tutorials ');$retval=Mysql_query($sql,$conn);If(!$retval){ Die(' Could not enter data: ' .Mysql_error());}Echo"Entered Data successfully\n";Mysql_close($conn);}Else{?><form method= "POST" action= "<?PHP $_php_self?>"><table Width="600" Border="0" CellSpacing="1" cellpadding="2"><tr><td Width="250">Tutorial Title</td><td><input Name="Tutorial_title" Type="Text" Id="Tutorial_title"></td></tr><tr><td Width="250">Tutorial Author</td><td><input Name="Tutorial_author" Type="Text" Id="Tutorial_author"></td></tr><tr><td Width="250">Submission Date [YYYY-MM-DD]</td><td><input Name="Submission_date" Type="Text" Id="Submission_date"></td></tr><tr><td Width="250"> </td><td> </td></tr><tr><td Width="250"> </td><td><input name= "add"  type= "submit"  id=  "add"  value=></td></tr></table></form><?}?></body ></HTML>          

When we receive user-submitted data, we need to use the GET_MAGIC_QUOTES_GPC () function to determine whether the escape of special characters is turned on for the sake of data security. If this option is off (not turned on) and returns 0, then we must call the Addslashes function to add escape to the string.

Yi.

You can also add other methods of checking data, such as mailbox format verification, phone number verification, integer validation, and so on.

MySQL Inserting data

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.