Insert date in database using PHP (MySQLi Procedural)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php $servername = "localhost"; $username = "root"; $password = ""; $Database = "T4TutorialsDotCom"; // Create connection $connection = mysqli_connect($servername, $username, $password, $Database); // Check connection if (!$connection) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO t4table (Name, Father_name, email) VALUES ('shamil', 'Asad', '[email protected]')"; if (mysqli_query($connection, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($connection); } mysqli_close($connection); ?> |
Insert date in database using PHP (MySQLi Object oriented)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php $servername = "localhost"; $username = "root"; $password = ""; $Database = "T4TutorialsDotCom"; // Create connection $connection = new mysqli($servername, $username, $password, $Database); // Check connection if ($connection->connect_error) { die("Connection failed: " . $connection->connect_error); } $sql = "INSERT INTO t4table (Name, Father_name, email) VALUES ('shamil', 'Asad', '[email protected]')"; if ($connection->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $connection->error; } $connection->close(); ?> |
Insert date in database using PHP (PDO)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $servername = "localhost"; $username = "root"; $password = ""; $Database = "T4TutorialsDotCom"; try { $connection = new PDO("mysql:host=$servername;dbname=$Database", $username, $password); // set the PDO error mode to exception $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO T4Table (Name, Father_name, email) VALUES ('shamil', 'Asad', '[email protected]')"; // use exec() because no results are returned $connection->exec($sql); echo "New record created successfully"; } catch(PAsadxception $e) { echo $sql . "<br>" . $e->getMessage(); } $connection = null; ?> |