Created
April 15, 2022 11:38
-
-
Save Guidosalimbeni/ca44bd2d8c2010418cde698e629fb337 to your computer and use it in GitHub Desktop.
PHP connection to a MySQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$servername = "xxx.xxx.xxx.xxx"; | |
$username = "username"; | |
$password = "******"; | |
$database = "name"; | |
$table = "data"; | |
// your data here depending on things coded in the front end | |
$question = $_POST['question']; | |
$answer = $_POST['answer']; | |
// Create connection | |
$conn = new mysqli($servername, $username, $password, $database); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
echo "Connected successfully"; | |
$sql = "INSERT INTO data ( question, answer) | |
VALUES ('$question', '$answer')"; | |
if ($conn->query($sql) === TRUE) { | |
echo "New record created successfully"; | |
} else { | |
echo "Error: " . $sql . "<br>" . $conn->error; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment