I created a simple form in HTML to add the details of student. It contains text fields foe first name, last name, age, phone and email. And i created a database in wamp phpmyadmin and created a table named stud with fields fname, lname, age, phone, email and my PHP code to add the data from the form to table stud is given below
<?php
$host="localhost";
$username="";
$password="";
$db_name="student";
$tbl_name="stud";
$connect=mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db($connect,$db_name)or die("cannot select DB");
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$age=$_POST['age'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$sql="INSERT INTO $tbl_name(fname, lname, age, phone, email)VALUES('$fname', '$lname' ,'$age', '$phone', `'$email')";`
$result=mysqli_query($connect,$sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='student.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
mysqli_close($connect);
?>
when I run this code in a browser it shows "cannot connect DB". But when i created a table named stud in test database it worked. Please help me to find a way to connect to my stud table in student database in wamp.
You can also use below
$connect=mysqli_connect("localhost","username","password","db_name")or die("Error in database connection");
OR
Check your username beacause default username in wamp is
root
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With