MySQL Insert data Insert Data via command Prompt window

Source: Internet
Author: User
Tags learn php mysql insert

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 Runoob_tbl

Instance

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

[Email protected]# mysql-u root-p password;EnterPassword:*******Mysql> UseRunoob;DatabaseChangedmysql>INSERT into Runoob_tbl(Runoob_title,Runoob_author,Submission_date) -VALUES("Learn PHP", "John Poul.",Now());QueryOk, 1Row affected(0.01Sec)Mysql>INSERT into Runoob_tbl(Runoob_title,Runoob_author,Submission_date) -VALUES("Learn MySQL", "Abdul S",Now());QueryOk, 1Row affected(0.01Sec)Mysql>INSERT into Runoob_tbl-> (runoob_title, Runoob_author, Submission_date)  ->< Span class= "PLN" >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 runoob_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:

<meta CharSet="Utf-8"> <title>Adding data to the 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() ){$runoob _title=Addslashes($_post[' Runoob_title ']);$runoob _author=Addslashes($_post[' Runoob_author ']);}Else{$runoob _title=$_post[' Runoob_title '];$runoob _author=$_post[' Runoob_author '];}$submission _date=$_post[' Submission_date '];$sql= "INSERT into Runoob_tbl". "(Runoob_title,runoob_author, Submission_date)". "VALUES". "(' $runoob _title ', ' $runoob _author ', ' $submission _date ')";mysql_select_db(' Runoob ');$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="Runoob_title" Type="Text" Id="Runoob_title"></td></tr><tr><td Width="250">Tutorial Author</td><td><input Name="Runoob_author" Type="Text" Id="Runoob_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. Www.0539g.com

MySQL Insert data Insert Data via command Prompt window

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.