php Database mysql query issue.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4209 Views - Last Post: 12 June 2011 - 10:11 PM Rate Topic: -----

#1 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

php Database mysql query issue.

Posted 09 June 2011 - 11:20 PM

I am making a mysql query as follows.
The result i am getting is "didnt" (look code below)
i was wondering where the problem could be. it connects fine with the database but the mysql_query call is not working.

// Connecting to Database
$dbhost = 'localhost';
$dbuser = '****';
$dbpass = '****';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) 			
mysql_select_db('MemberUser');
				
if($conn){
  echo 'connected';
}
else{
die ('Error connecting to mysql');	
}

$result = mysql_query("INSERT INTO users (fullname, email, password) VALUES ('$name', '$email', '$password')");
if ($result) {
 echo 'worked';
} elseif(!$result) {
 echo 'didnt';
} else {
 echo '$_REQUEST is empty';
}
	

thanks for looking

This post has been edited by Dormilich: 09 June 2011 - 11:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: php Database mysql query issue.

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,318
  • Joined: 08-June 10

Re: php Database mysql query issue.

Posted 09 June 2011 - 11:41 PM

add on line #19: echo mysql_error();
Was This Post Helpful? 0
  • +
  • -

#3 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: php Database mysql query issue.

Posted 10 June 2011 - 12:26 AM

View PostDormilich, on 10 June 2011 - 12:41 AM, said:

add on line #19: echo mysql_error();


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bob, bob@gmail.com, 2222222222)' at line 2

This post has been edited by nacholibre: 10 June 2011 - 12:27 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,318
  • Joined: 08-June 10

Re: php Database mysql query issue.

Posted 10 June 2011 - 12:33 AM

can you print the query as it is used in the call, e.g. echo "INSERT INTO users (fullname, email, password) VALUES ('$name', '$email', '$password')";.

it might be something with the quotes or it might be with the database not getting the data it expects (i.e. an INT fields value must not be quoted).
Was This Post Helpful? 2
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: php Database mysql query issue.

Posted 10 June 2011 - 03:43 AM

For the 11 billionth time people...

If you're building a query from input -- that is, you have variables in the query -- and it's failing, doesn't it MAKE SENSE to LOG THE CONTENTS OF THE QUERY WHEN IT FAILS? This is SIMPLE COMMON SENSE!

$query = "INSERT INTO users (fullname, email, password) VALUES ('$name', '$email', '$password')";
$result = mysql_query($query);
if ($result) {
 echo 'worked';
} elseif(!$result) {
 echo "Query $query did work! " . mysql_error();
} else {
 echo '$_REQUEST is empty';
}


Also, you realize that the else part of that tree is pointless, right? The result of an insert query is TRUE or FALSE. There is no third state to consider.

I also have to ask where you're setting the values of the variables you're using in your query.
Was This Post Helpful? 2
  • +
  • -

#6 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: php Database mysql query issue.

Posted 11 June 2011 - 12:58 AM

i found the problem - it was a quotation and space issue. Thanks both for your suggestions. it helped.
Currently i am getting "Warning: Cannot modify header information - headers already sent by...." error.
This happens when i try to redirect user to a SUCCESS page as follows.

if ($result) {
echo 'worked';
header("Location: http://www.successpage.com/");
} else() {
echo "Query $query did work! " . mysql_error();


after googling forums and reading i know now that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. So header() wont work for me. Is there any other way to redirect user? sorry i dont know php that well.

thanks
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: php Database mysql query issue.

Posted 11 June 2011 - 08:18 AM

You're not suppose to output anything to the browser before trying to send a header.

You can still redirect the user with header - location by commenting out your echo statements. The die statement shouldn't be used because it will kill the entire script. Check out error handling.

To be positively sure your script won't output anything to the screen use output control\buffering.

Example
<?php
ob_start();
//all output is captured
//code here
echo "Hi";
header("location: page.php"); //valid since we're using output control
ob_end_flush(); 


Was This Post Helpful? 1
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,318
  • Joined: 08-June 10

Re: php Database mysql query issue.

Posted 11 June 2011 - 07:16 PM

View Postnacholibre, on 11 June 2011 - 09:58 AM, said:

if ($result) {
echo 'worked';
header("Location: http://www.successpage.com/");
} else() {
echo "Query $query did work! " . mysql_error();

Id just delete line 2, why should I echo out "worked" if I want to redirect, the outcome will be clear anyways.
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: php Database mysql query issue.

Posted 11 June 2011 - 07:19 PM

View PostJackOfAllTrades, on 10 June 2011 - 06:43 AM, said:

For the 11 billionth time people...

doesn't it MAKE SENSE to LOG THE CONTENTS OF THE QUERY WHEN IT FAILS? This is SIMPLE COMMON SENSE!


I blame dot Net languages for the complete lack of traditional troubleshooting & diagnosis standards.

Every time I see someone set a result variable & then not use it, I want to stab myself in the face.
Was This Post Helpful? 0
  • +
  • -

#10 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: php Database mysql query issue.

Posted 12 June 2011 - 08:59 PM

View Postcodeprada, on 11 June 2011 - 09:18 AM, said:

You're not suppose to output anything to the browser before trying to send a header.

You can still redirect the user with header - location by commenting out your echo statements. The die statement shouldn't be used because it will kill the entire script. Check out error handling.

To be positively sure your script won't output anything to the screen use output control\buffering.

Example
<?php
ob_start();
//all output is captured
//code here
echo "Hi";
header("location: page.php"); //valid since we're using output control
ob_end_flush(); 



I commented out all the echo's and set my code as follows.

<?php
ob_start();
..code..
$result = mysql_query($query);
if ($result) {
header('Location: http://www.google.com/');
} elseif(!$result) {
//echo "Query $query did work! " . mysql_error();
} 
ob_end_flush();
flush();
?>


i am still getting the error that reads
"
Warning: Cannot modify header information - headers already sent by (output started at /home/test.php:1) in /home/test.php on line 72"
Line 72 is this line "header('Location: http://www.google.com/');"

am i doing this wrong? Thanks again for any suggestions
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: php Database mysql query issue.

Posted 12 June 2011 - 09:02 PM

You can not have any output before you call header. You only get to output headers once, any echo, print statement, output of any kind will create headers & when you call the header function php will puke on your code because the headers have already been sent.
Was This Post Helpful? 0
  • +
  • -

#12 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: php Database mysql query issue.

Posted 12 June 2011 - 09:19 PM

View Postno2pencil, on 12 June 2011 - 10:02 PM, said:

You can not have any output before you call header.


I get that but the only output i had was echos and i commented them out.
Here is the entire code.
<?php
ob_start();

define('FACEBOOK_APP_ID', 'your_app_id'); 
define('FACEBOOK_SECRET', 'your_app_secret'); 

function parse_signed_request($signed_request, $secret) 
{
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256')
{
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}

// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) 
{
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) 
{
return base64_decode(strtr($input, '-_', '+/'));
}
if ($_REQUEST) 
{
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);

$name = $response["registration"]["name"];
$email = $response["registration"]["email"];
$password = $response["registration"]["password"];
$gender = $response["registration"]["gender"];
$dob = $response["registration"]["birthday"];
$workemail = $response["registration"]["workEmail"];

/*echo $name;
echo $email;
echo $password;
echo $gender;
echo $dob;
echo $workemail;*/

// Connecting to database
mysql_connect('DATABASE_HOST', 'YOUR_USERNAME', 'YOUR_PASSWORD');
mysql_select_db('YOUR_DATABASE');

$query = "INSERT INTO users (fullname, email, password, gender, dob, 
                                        workemail) VALUES ('$name', 
                                        '$email','$password','$gender','$dob','$workemail')";
$result = mysql_query($query);					
if ($result) {
header('Location: http://www.google.com/');
} elseif(!$result) {
//echo "Query $query did work! " . mysql_error();
} else {
//echo '$_REQUEST is empty';
}
}
ob_end_flush();
flush();
?>

Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: php Database mysql query issue.

Posted 12 June 2011 - 09:23 PM

Right away the first thing that I see is that this is a facebook app. Is this posting on the facebook canvas? Certainly the headers from facebook have already been posted...

I would suggest you test this outside of the scope of facebook with values that you control until you can assure the code is working correctly & then worry about implementing with your facebook app & appID.
Was This Post Helpful? 0
  • +
  • -

#14 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: php Database mysql query issue.

Posted 12 June 2011 - 09:48 PM

I am trying this with working FB AppID and my own site that is live.
The FB plugin i am working on is this one http://developers.fa...s/registration/
Once the user clicks the register button. The i have so far adds users to my database and remains on a white page.(just doing the mysql_query(query) and not doing anything with the result)
Clearly there must be someway to take users to a success/fail page. The error i was getting is when i try to redirect users based on the query result

Quote

I would suggest you test this outside of the scope of facebook with values that you control until you can assure the code is working correctly & then worry about implementing with your facebook app & appID.

i know something like the following works on my server for redirecting - i just dont know much about php to speculate why its not working for the FB code.
<?php
ob_start();
header("location: 1.html"); 
echo "send data"; 
header("location: 2.html"); //replaces 1.html
ob_end_flush(); //now the headers are sent
?>

Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: php Database mysql query issue.

Posted 12 June 2011 - 09:53 PM

Why not just use Ajax so you don't have to deal with sloppy header redirection?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2