Wrong Code?

Ask about a PHP problem here.
Post Reply
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Wrong Code?

Post by techm3 »

I'm using this code in the same page. The last query works. Does anyone sees something wrong in the code?
The table name and the fields are ok.
	
       $b_name		= mysql_real_escape_string(htmlentities($_POST['b_name']));

        // Updates Offers
	$of1		= mysql_real_escape_string(htmlentities($_POST['of1']));
	$of2		= mysql_real_escape_string(htmlentities($_POST['of2']));
	$of3		= mysql_real_escape_string(htmlentities($_POST['of3']));
	$of4		= mysql_real_escape_string(htmlentities($_POST['of4']));
	$of5		= mysql_real_escape_string(htmlentities($_POST['of5']));

	$sql	= "INSERT INTO `business_offers` (of1, of2, of3, of4, of5, username, b_name) VALUES ('{$of1}', '{$of2}', '{$of3}', '{$of4}', '{$of5}','{$_SESSION['username']}', '{$b_name}')";
	
	mysql_query($sql);
	// WORKS
	$ev1		= mysql_real_escape_string(htmlentities($_POST['ev1']));
	$ev2		= mysql_real_escape_string(htmlentities($_POST['ev2']));
	$ev3		= mysql_real_escape_string(htmlentities($_POST['ev3']));
	$ev4		= mysql_real_escape_string(htmlentities($_POST['ev4']));
	$ev5		= mysql_real_escape_string(htmlentities($_POST['ev5']));

	$sql	= "INSERT INTO `business_events` (ev1, ev2, ev3, ev4, ev5, username, b_name) VALUES ('{$ev1}', '{$ev2}', '{$ev3}', '{$ev4}', '{$ev5}','{$_SESSION['username']}', '{$b_name}')";
	
	mysql_query($sql);
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Wrong Code?

Post by jacek »

I thunk username is a keyword, try adding ` around your column names, also if you add
echo mysql_error();
after the failing query, it will tell you what the problem is :)
Image
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Re: Wrong Code?

Post by techm3 »

I cover every column name with ` but it doesn't work. And by using
echo mysql_error();
I get no error :?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Wrong Code?

Post by jacek »

techm3 wrote:I cover every column name with ` but it doesn't work. And by using
echo mysql_error();
I get no error :?
Hmm, can you post the full code then perhaps somethign is prevent the queries from ever running.
Image
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Re: Wrong Code?

Post by techm3 »

Ok. Here is the complete code. Hope you can help me. :)
<?php
	include('init.inc.php');

	$b_name		= mysql_real_escape_string(htmlentities($_POST['b_name']));
	$b_phone_number	= mysql_real_escape_string(htmlentities($_POST['b_phone_number']));
	$b_address	= mysql_real_escape_string(htmlentities($_POST['b_address']));
	$b_category	= mysql_real_escape_string(htmlentities($_POST['b_category']));
	$b_city		= mysql_real_escape_string(htmlentities($_POST['b_city']));
	$b_state	= mysql_real_escape_string(htmlentities($_POST['b_state']));
	$b_country	= mysql_real_escape_string(htmlentities($_POST['b_country']));
	$b_zipcode	= mysql_real_escape_string(htmlentities($_POST['b_zipcode']));
	$b_description	= mysql_real_escape_string(htmlentities($_POST['b_description']));

	$sql	= "INSERT INTO `business_information` (
			b_name, 
			b_phone_number, 
			b_address, 
			b_category, 
			b_city, 
			b_state, 
			b_country, 
			b_zipcode, 
			b_description, 
			username) 
		VALUES (
			'{$b_name}', 
			'{$b_phone_number}', 
			'{$b_address}', 
			'{$b_category}', 
			'{$b_city}', 
			'{$b_state}', 
			'{$b_country}', 
			'{$b_zipcode}', 
			'{$b_description}', 
			'{$_SESSION['username']}')";
			
	mysql_query($sql);
	
	// Updates Business Offers
	$of1		= mysql_real_escape_string(htmlentities($_POST['of1']));
	$of2		= mysql_real_escape_string(htmlentities($_POST['of2']));
	$of3		= mysql_real_escape_string(htmlentities($_POST['of3']));
	$of4		= mysql_real_escape_string(htmlentities($_POST['of4']));
	$of5		= mysql_real_escape_string(htmlentities($_POST['of5']));

	$sql	= "INSERT INTO `business_offers` (of1, of2, of3, of4, of5, username, b_name) VALUES ('{$of1}', '{$of2}', '{$of3}', '{$of4}', '{$of5}', '{$_SESSION['username']}', '{$b_name}')";
	
	mysql_query($sql);
	
	// Updates Business Events
	$ev1		= mysql_real_escape_string(htmlentities($_POST['ev1']));
	$ev2		= mysql_real_escape_string(htmlentities($_POST['ev2']));
	$ev3		= mysql_real_escape_string(htmlentities($_POST['ev3']));
	$ev4		= mysql_real_escape_string(htmlentities($_POST['ev4']));
	$ev5		= mysql_real_escape_string(htmlentities($_POST['ev5']));

	$sql	= "INSERT INTO `business_events` (ev1, ev2, ev3, ev4, ev5, username, b_name) VALUES ('{$ev1}', '{$ev2}', '{$ev3}', '{$ev4}', '{$ev5}', '{$_SESSION['username']}', '{$b_name}')";
	
	mysql_query($sql);
	echo mysql_error();
	//header('Location: http://prosion.ws/m/profile.php');
	
?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Wrong Code?

Post by jacek »

Well that still looks okay :s

It could be that if the second query works it removes the value that would normally be returned by mysql_error() so try putting the mysql_error() call under the first query too :D
Image
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Re: Wrong Code?

Post by techm3 »

So, I did what you said and I still get no error.

But I noticed that when I write something on the fields called 'of' (Second Query) it does work and create the row on the database. If not, it won't create it.

This creates a doubt on me. Isn't PHP supposed to create the row with the values that are posted like the username, although the others are not?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Wrong Code?

Post by jacek »

Yeah it should just use the values form the form. Your SQL looks fine to me, which suggests that the problem is with your table. A common one is getting the name of a column wrong. mysql_error() should have told you about that though :?

Can you put a
echo mysql_error();
directly under ever mysql_query(), just to make absolutely sure there are no errors that are being hidden.
Image
Post Reply