How to insert value into MySQL table step by step guide for beginner


In this article we learn how to insert value into mysql table.To insert value into mysql table we learn two methods.One is through mysql promt and othe is using php script.

Insert value into mysql table using mysql promt

To inset value into mysql table using mysql promt we use “insert into” command. First we need to login mysql server, then select mysql database and inset the value into the specific table using the below command.

[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use techrideradmin
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> INSERT INTO student (studentfirstname, studentlastname) VALUES ("Raktim", "Sharma");
Query OK, 1 row affected (0.00 sec)
We can not define student roll no because it will be incrementd automatically.Now data has been inserted into mysql table named student.
How to insert value into MySQL table step by step guide for beginner
How to insert value into MySQL table step by step guide for beginner


Insert value into mysql table using php script


To Insert value into mysql table using php script we need to use mysql_query function .
Lets take an example to insert value into mysql table using php script.
<html>
   <head>
      <title>Insert value into MySQL table on linux system</title>
   </head>
   <body>
      <?php
         $mysql_host = 'localhost';
         $mysql_user = 'root';
         $mysql_pass = 'passwordofroot';
         $dbname = "techrideradmin";
         $conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass,$dbname);
        
         if(! $conn ) {
            die('Could not connect the server: ' . mysql_error());
         }
         echo 'Connection successfully established';
         $sql = “INSERT INTO student (studentfirstname, studentlastname) VALUES       ("Raktim", "Sharma");”;
         $result = mysql_query( $sql, $conn );
        
         if(! $result ) {
            die('value not inserted: ' . mysql_error());
         }
         echo "Value inserted succcessfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>

That’s all.If this article is helpful please share it!!!!






Please Donate To Bitcoin Address: [[address]]

Donation of [[value]] BTC Received. Thank You.
[[error]]
SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment