Page 1 of 1

Wrong Code?

Posted: Mon Jan 16, 2012 6:52 pm
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);

Re: Wrong Code?

Posted: Tue Jan 17, 2012 10:31 pm
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 :)

Re: Wrong Code?

Posted: Sat Jan 21, 2012 2:05 pm
by techm3
I cover every column name with ` but it doesn't work. And by using
echo mysql_error();
I get no error :?

Re: Wrong Code?

Posted: Sun Jan 22, 2012 2:45 pm
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.

Re: Wrong Code?

Posted: Mon Jan 23, 2012 1:36 am
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');
	
?>

Re: Wrong Code?

Posted: Mon Jan 23, 2012 8:05 pm
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

Re: Wrong Code?

Posted: Tue Jan 24, 2012 1:02 am
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?

Re: Wrong Code?

Posted: Tue Jan 24, 2012 5:56 pm
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.