Horje Website
Mysqli Insert Query – PHP Code

First, Create Mysql Username and Password and Database

Second, Create a Table “sms” in Database

Third, Create a Row “id and title” in Table.

 

Then, Edit following codes to connect your database and username with Mysql.

 

 
 
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. ?>

 

Then, Create a file “insert.php” and add following codes inside of the file.

 
 
  1. <?php
  2. // Create connection
  3. $conn = new mysqli($servername, $username, $password, $dbname);
  4. // Check connection
  5. if ($conn->connect_error) {
  6. die("Connection failed: " . $conn->connect_error);
  7. }
  8. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  9. VALUES ('John', 'Doe', '[email protected]')";
  10. if ($conn->query($sql) === TRUE) {
  11. echo "New record created successfully";
  12. } else {
  13. echo "Error: " . $sql . "<br>" . $conn->error;
  14. }
  15. $conn->close();
  16. ?>

 

Full Codes

 
 
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10. die("Connection failed: " . $conn->connect_error);
  11. }
  12. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  13. VALUES ('John', 'Doe', '[email protected]')";
  14. if ($conn->query($sql) === TRUE) {
  15. echo "New record created successfully";
  16. } else {
  17. echo "Error: " . $sql . "<br>" . $conn->error;
  18. }
  19. $conn->close();
  20. ?>

 

Source: www.w3schools.com

Published:
December 24, 2022
Author:
admin
Category:
Mysql, PHP Source Code
Views:
31

This article was posted in Mysql, PHP Source Code. Bookmark the permalink. Follow comments with the RSS feed for this post.Post a Comment or leave a trackback: Trackback URL.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Horje © 2011 - 2023