Click here to Skip to main content
16,016,180 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Warning: mysqli::__construct() expects parameter 5 to be integer, string given in C:\xampp\htdocs\project\contactdemo.php on line 35

Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\project\contactdemo.php on line 44

Warning: main(): Couldn't fetch mysqli in C:\xampp\htdocs\project\contactdemo.php on line 50
Error:INSERT INTO contactphpdem(username,email,mobile,subject) values('Rahul','ragavi@gmail.com','8105189061','nvjmlik')

Warning: mysqli::close(): Couldn't fetch mysqli in C:\xampp\htdocs\project\contactdemo.php on line 52

What I have tried:

<?php
$username= filter_input(INPUT_POST,'username');
$email= filter_input(INPUT_POST,'email');
$mobile= filter_input(INPUT_POST,'mobile');
$subject= filter_input(INPUT_POST,'subject');
if(!empty($username))
{
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
echo "Only letters and white space allowed";
}
if(!empty($email))
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format";
}
if(!empty($mobile))
{
if (!preg_match("/^[0-9]{10}$/",$mobile)) {
echo "Invalid mobile number";
}
if(!empty($subject))
{
if (preg_match("/^[0-9]{10}$/",$subject)) {
echo "Invalid data";
}
$host="localhost";
$dbusername="root";
$dbemail="email";
$dbmobile="mobile";
$dbsubject="subject";
$dbname="contactphpdem";

//Create connection
$conn = new mysqli($host,$dbusername,$dbemail,$dbmobile,$dbsubject,$dbname);

if(mysqli_connect_error()){
die('Connect Error('. mysqli_connect_errno().') '
. mysqli_connect_error());
}
else{
$sql = "INSERT INTO contactphpdem(username,email,mobile,subject)
values('$username','$email','$mobile','$subject')";
if($conn->query($sql))
{
echo"alert('Your are Successfully entered')";
echo"window.open('Home.html','_self')";
}
else{
echo"Error:". $sql ."
". $conn->error;
}
$conn->close();
}
}
else
{
echo"Please share your thoughts or query for us to improve our service";
die();
}
}
else{
echo"Please enter your mobile number";
die();
}

}
else{
echo"Please enter your email id";
die();
}
}
else{
echo"Name should not be empty";
die();
}

?>
Posted
Updated 27-Nov-21 19:23pm

 
Share this answer
 
mysql_connect generally given error by these config file declaration method so you use these kind of methods like;-
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'demo');
define('APP_NAME', 'demo1');
error_reporting(1);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900