Database connection undefined variable

goodday house, I need an assistance with this connection. i am having an issue. i created a database called squared in mysql and under it a table called register with 3 field. so i am trying to insert information through coding using php editor. but i am having undefined variable error. i have already created the forms in html.

<?php 
$servername = "localhost";
$db_name="squared";
$username="root";
$password="";

//create connection
$conn = mysqli_connect($servername, $username, $password, $database);

//check connection
if (!$conn){
    die("connection failed: " . mysqli_connect_error());
}
echo "connected successfully";
//use $query or sql
// name, email and address are fields of your fields; test your table. $name, $email and $address are values collected from the form
$sql = "INSERT INTO register (name, email, address) VALUES ('$name','$email' , '$address')";
if (mysqli_query($conn, $sql)){
    echo "Newrecord created successfully";
}else{
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);


?>

edit your post and add around your code to format it correctly

done. Please can anyone recommend for me good sites i can learn file handling, validation and database connection in php

Everything that could possibly be wrong with that code is there. Go to this link and learn how to use PDO.

https://phpdelusions.net/pdo

Try sitepoint.com for learning: they have a lot of books. You will have to pay, about $100 a year, but I think that will help you.

Sponsor our Newsletter | Privacy Policy | Terms of Service