I tried everything to add the data to database but it fails to connect
Apache phpMyAdmin is being used. Am unable to connect to the data base
connect.php
<?php
include('dbcon.php');
$uname= $_POST['username'];
$cname= $_POST['name'];
$email= $_POST['email'];
$pass= $_POST['password'];
$number= $_POST['number'];
$query= "INSERT INTO `userlogindata`(`username`, `name`, `email`, `password`, `number`) VALUES ('$uname','$cname','$email','$pass','$number')";
$run=mysqli_query($con,$query);
if($run==true)
{
?>
<script>
window.open('redirect.php','_self');
</script>
<?php
}
else {
?>
<script> alert('Login-Id is already !');
window.open('index.php','_self');
</script>
<?php
}
?>
dbcon.php
<?php
$con = mysqli_connect('127.0.0.1','root','','onlinetest');
if ($con->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
It shows an alert Log-Id already registered.
As others have mentioned you should look into prepared statements, but to get your errors from the connection you need to ensure you call of your $con
variable. Atm you are using
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
It should be
echo "Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.