I have created an online form and am trying to post the info into a mysql database and I keep getting back a query error.

Below is my PHP code. If anybody can help me, that would be great.

Thanks a lot.

<?php
	$property_type = $_POST['property_type'];
	$number_of_bedrooms = $_POST['#_of_bedrooms'];
	$number_of_bathrooms = $_POST['#_of_bathrooms'];
	$state_province = $_POST['state_province'];
	$city = $_POST['city'];
	$address = $_POST['address'];
	$name = $_POST['name'];
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	$additional_info = $_POST['additional_info'];

$dbc = mysqli_connect('localhost', 'circlema_shnips6', 'rhiannon', 'circlema_propertydatabase')or die('Error connecting to MySql server');

$query = "INSERT INTO circlema_propertydatabase (property_type, number_of_bedrooms, number_of_bathrooms, state_province, city, address, " . 
		"name, email, phone, additional_info) " .
		"VALUES ('$property_type', '$number_of_bedrooms', '$number_of_bathrooms', '$state_province', '$city', '$address', " . 
		"'$name', '$email', '$phone', '$additional_info')";

$result = mysqli_query($dbc, $query) or die('Error querying database.');

mysqli_close($dbc);

?>

It would help to know exactly what error you are getting

Thank you for looking at my code. The exact error that displays is "Error querying database" - which is written in my code as an "or die" response.

Hopefully this helps.

Thanks again for your help. If there is any other info you need from me, let me know.

Change that line to this so that you can see exactly what the error is:

$result = mysqli_query($dbc, $query) or die(mysql_error());

Thanks a lot. I will make this adjustent to my code and see what the error reads, and post it back here if I do not understand.

Thanks again for your help.

I have made some changes to my code based on suggestions on forums. I am quite certain that my connection to the database is fine, however, the data on the form is not posting into my database.

I was told to change:

$result = mysqli_query($dbc, $query) or die('Error querying database.');

to this:

$result = mysqli_query($dbc, $query) or die(mysql_error());

in order to identify the error. However, when I run the script with:

$result = mysqli_query($dbc, $query) or die('Error querying database.');

it goes to an error page that reads:
Error querying database.

When I use:
$result = mysqli_query($dbc, $query) or die(mysql_error());

it goes to what looks like an identical error page with no message at all.

Below is the code I have now:

<?php

	$dbc = mysqli_connect('localhost', 'circlema_shnips6', 'rhiannon', 'circlema_propertydatabase')or trigger_error(mysql_error(),E_USER_ERROR);
	
	$property_type = $_POST['property_type'];
	$number_of_bedrooms = $_POST['#_of_bedrooms'];
	$number_of_bathrooms = $_POST['#_of_bathrooms'];
	$state_province = $_POST['state_province'];
	$city = $_POST['city'];
	$address = $_POST['address'];
	$name = $_POST['name'];
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	$additional_info = $_POST['additional_info'];

	$query = "INSERT INTO circlema_propertydatabase (property_type,number_of_bedrooms,number_of_bathrooms,state_province,city,address,name,				email,phone,additional_info) VALUES('$property_type','$number_of_bedrooms','$number_of_bathrooms','$state_province','$city','$address','$name','$email', '$phone','$additional_info')";

	$result = mysqli_query($dbc, $query) or die(mysql_error());	

	mysqli_close($dbc);

?>

I would really appreciate any help. As well, if possible, I would like to direct the user to thankyou.html if the data successfully posts in my database.

Any help with the code required for that would be greatly appreciated.

Thanks again for all the previous suggestions.

Member Avatar for diafol

Every $_POST variable should be cleaned, e.g.

$property_type = mysqli_real_escape_string($dbc, $_POST['property_type']);

Also, whenever I have a long list of fields and values I tend to use the SET syntax - in order to avoid missing a pair:

$query = "INSERT INTO circlema_propertydatabase SET `property_type` = '$property_type',`number_of_bedrooms`= '$number_of_bedrooms'...";

Also enclose the fieldnames in backticks (`...`). I can't see anything obvious there that would cause a problem though.

I have made another adustment to my script and am now getting a specific error.

Thanks to everybody for your help. Hopefully this helps identify the error.

Here is my code and below is the error that comes back:

<?php

	$dbc = mysqli_connect('localhost', 'username', 'pwd', 'database')or trigger_error(mysqli_error(),E_USER_ERROR);
	
	$property_type = mysqli_real_escape_string($dbc, $_POST['property_type']);
	$number_of_bedrooms = mysqli_real_escape_string($dbc, $_POST['#_of_bedrooms']);
	$number_of_bathrooms = mysqli_real_escape_string($dbc, $_POST['#_of_bathrooms']);
	$state_province = mysqli_real_escape_string($dbc, $_POST['state_province']);
	$city = mysqli_real_escape_string($dbc, $_POST['city']);
	$address = mysqli_real_escape_string($dbc, $_POST['address']);
	$name = mysqli_real_escape_string($dbc, $_POST['name']);
	$email = mysqli_real_escape_string($dbc, $_POST['email']);
	$phone = mysqli_real_escape_string($dbc, $_POST['phone']);
	$additional_info = mysqli_real_escape_string($dbc, $_POST['additional_info']);
	
	$query = "INSERT INTO circlema_propertydatabase ('property_type','number_of_bedrooms','number_of_bathrooms','state_province','city','address','name','email','phone','additional_info') VALUES('$property_type','$number_of_bedrooms','$number_of_bathrooms','$state_province','$city','$address','$name','$email', '$phone','$additional_info')";
			
	$result = mysqli_query($dbc, $query) or die(mysqli_error());	
	
	mysqli_close($dbc);
	
	/*echo 'Thanks for submitting the form.'; */
	
?>

The error that is returning is:

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/circlema/public_html/price-your-home/form_processor.php on line 18

If this helps anybody identify an error in my code, that would greatly appreciated.

Member Avatar for diafol

The mysqli_* uses a different way of doing things than mysql_*

You can't use mysqli_error() as you must place the link identifier inside it:

mysqli_error($dbc)

Please check the php.net manual - all the answers are there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.