Skip to content

Instantly share code, notes, and snippets.

@Guidosalimbeni
Created April 15, 2022 11:38
Show Gist options
  • Save Guidosalimbeni/ca44bd2d8c2010418cde698e629fb337 to your computer and use it in GitHub Desktop.
Save Guidosalimbeni/ca44bd2d8c2010418cde698e629fb337 to your computer and use it in GitHub Desktop.
PHP connection to a MySQL database
<?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