Warning: mysqli_error () expects exactly 1 parameter, 0 given in my register.php file

I followed the code exactly the same as in w3schools.com .. but still I get the above error, and the database table is also not updated.

My html and php code is as follows:

<form action="register.php" method="post"> <select name="title" size="1"> <option>Mr.</option> <option>Ms.</option> <option>Prof.</option> <option>Dr.</option> </select> <label class="label">Given Name: <input name="givenname" type="text" value="Enter Your First Name" maxlength="30" /></label> <label class="label">Surname: <input name="surname" type="text" /></label> <label class="label">Address: <input name="address" type="text" maxlength="80" /></label> <label class="label">Phone No: <input name="phoneno" type="text" /></label> <label class="label">Email ID: <input name="emailid" type="text" /></label> <label class="label">Fax: <input name="fax" type="text" /></label> <label class="label">Pincode: <input name="pincode" type="text" /></label> <label class="label">Country: <input name="country" type="text" /></label> <input name="submit" type="submit" /> </form> <?php $con=mysqli_connect("my_Ipaddress","abcd","abcd"); //database connection if (mysqli_connect_errno()) { echo "Failed to connect to Database...There must be some error! We are working on it!!" . mysqli_connect_error(); } $sql="INSERT INTO Registrations (Title, Given Name, Surname, Address, Phone No, Email ID, Fax, Pincode, Country) VALUES ('$_POST[title]','$_POST[givenname]','$_POST[surname]','$_POST[address]','$_POST[phoneno]','$_POST[emailid]','$_POST[fax]','$_POST[pincode]','$_POST[country]')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error()); } echo "1 record added"; mysqli_close($con); ?> 
+4
source share
5 answers

I got your mistake, here it is

Syntax

mysqli_error () is invalid.

Change

 die('Error: ' . mysqli_error()); 

to

 die('Error: ' . mysqli_error($con)); 
+2
source

The problem is that some of your fields contain spaces between them. To avoid a syntax error, these fields should be wrapped with reverse windows,

 INSERT INTO Registrations (Title, `Given Name`, Surname, Address, `Phone No`, `Email ID`, Fax, Pincode, Country) VALUES(...) 

As a side element, a query is vulnerable to SQL Injection if the value (s) of the variables were received externally. Please see the article below to learn how to prevent it. Using PreparedStatements , you can get rid of the use of single quotes around values.

+6
source

You have spaces between column names, and $_POST needs a quote in the column name.

 $sql="INSERT INTO Registrations (Title, {Given Name}, Surname, Address, {Phone No}, {Email ID}, Fax, Pincode, Country) VALUES ('".$_POST['title']."','".$_POST['givenname']."','".$_POST['surname']."','".$_POST['address']."','".$_POST['phoneno']."','".$_POST['emailid']."','".$_POST['fax']."','".$_POST['pincode']."','".$_POST['country']."')"; 
0
source

Use this type of query and do not allow space in the mysql fieldname field.

 $sql="INSERT INTO Registrations (Title, Given_Name, Surname, Address, Phone_No, Email_ID, Fax, Pincode, Country) VALUES ('".$_POST[title]."','".$_POST[givenname]."','".$_POST[surname]."','".$_POST[address]."','".$_POST[phoneno]."','".$_POST[emailid]."','".$_POST[fax]."','".$_POST[pincode]."','".$_POST[country]."')"; 
0
source

Try the following:

 die(mysqli_error($conn)); 

Example:

 $sql= "delete FROM purchage_book WHERE id='$id'"; $rs=mysqli_query($conn,$sql)or die(mysqli_error($conn)); 
-1
source

All Articles