I am trying to create a connection from a web form to MySQL database using PHP. My code is as follows:
connection.php:
<?php
$link = mysqli_connect("", "", "", "");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
phpinfo();
This part seems to work.. It then connects to thank you.php:
<?php
function connect()
{
$conn = mysqli_connect("", "", "", "");
}
ini_set('display_errors', 1); error_reporting(~0);
require 'connection.php';
$conn = Connect();
$query = mysqli_query($conn,"INSERT into Members (Username) VALUES('1111Username')");
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
echo "Thank You For Contacting Us <br>";
mysqli_close($conn);
?>
I am getting the following errors:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/sites/i/site/public_html/ContactForm/thankyou.php on line 15
Fatal error: Call to a member function query() on null in /var/sites/i/site/public_html/ContactForm/thankyou.php on line 16