When the table is created, this statement ‘INSERT INTO table_name’ is used to insert records into it.
To insert records in a table,The columns are not written under double (“”) or single (”) quotes.When numeric data type values are written then they are not written in double or single quotes.String values are given double or single quotes.NULL value is not written inside double or single quotes.
The insertion of records is done in this way.
INSERT INTO table_name (column 1, column 2, column 2, ..., column n) VALUES('value 1', 'value 2', 'value 2', ..., 'value n')
Example for Inserting Record in Table using MySQLi
<?php $server = "localhost"; $user = "root"; $password = ""; $db = "expertstutorials"; $conn = mysqli_connect($server, $user, $password, $db); if($conn){ echo "Connected successfully."; } else{ echo mysqli_connect_error(); } $table_insert = "INSERT INTO course(course_name, course_year) VALUES ('b.tech', '4 year')"; if (mysqli_query($conn, $table_insert)) { echo "Record inserted successfully."; } else { echo "Error inserting record : " . mysqli_error($conn); } mysqli_close($conn); ?>
Example for Inserting Record in Table using PDO
<?php $server = "localhost"; $user = "root"; $password = ""; $db = "expertstutorials"; try{ $conn = new PDO("mysql:host=$server;dbname=$db", $user, $password); echo "Connected successfully."; $table_insert = "INSERT INTO course(course_name, course_year) VALUES ('b.tech', '4 year')"; $conn->exec($table_insert); echo "Record inserted successfully."; } catch(PDOException $e){ echo "Error inserting record : " . $e->getMessage(); } $conn = null; ?>