0
3.5kviews
Write a PHP Program to insert a record into MYSQL database.
1 Answer
written 6.3 years ago by | • modified 6.3 years ago |
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // sql to create table $sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )"; if (mysqli_query($conn, $sql)) { echo "Table MyGuests created successfully"; } else { echo "Error creating table: " . mysqli_error($conn); } // add a record to the "MyGuests" table $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('ABC', 'XYZ', '[email protected]')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?>