1,105,399 Community Members

PHP - mysql query error

Member Avatar
shnips6
Newbie Poster
5 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);

?>
Member Avatar
simplypixie
Practically a Master Poster
673 posts since Oct 2010
Reputation Points: 123 [?]
Q&As Helped to Solve: 122 [?]
Skill Endorsements: 6 [?]
 
0
 

It would help to know exactly what error you are getting

Member Avatar
shnips6
Newbie Poster
5 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
simplypixie
Practically a Master Poster
673 posts since Oct 2010
Reputation Points: 123 [?]
Q&As Helped to Solve: 122 [?]
Skill Endorsements: 6 [?]
 
0
 

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

$result = mysqli_query($dbc, $query) or die(mysql_error());
Member Avatar
shnips6
Newbie Poster
5 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
shnips6
Newbie Poster
5 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
diafol
Where are my eyes?
12,986 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,849 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

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.

Member Avatar
shnips6
Newbie Poster
5 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
diafol
Where are my eyes?
12,986 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,849 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: