method does not return values ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

icebreaker
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 1:50 am

method does not return values ...

Post by icebreaker »

Hi all, this is my first post in this forum .

im new to PHP .. just trying some examples ..

i want to return the value "true" after connecting to the database .. but after going into the method "add_to_database()" the value true is not returned ..instead this statement gets printed with the values "INSERT INTO table_users VALUES ('$first','$last','$user','$pass','$mobile','$email')" ...
(Note : the values r added into the database without any problems)
can u tell me what is wrong with my code ? ..thanx

Code: Select all

function is_all_valid(){
       if(isset($_POST['but_signin'])){
           $fn = check_fname();
           $ln = check_lname();
           $un = check_uname();
           $pas = check_password();
           $mob = check_mobile();
           $em = check_email();
           if($fn == "" & ""== $ln & "" == $un & "" == $pas &  "" == $mob & "" == $em){
            add_to_database(get_fname(),get_lname(),get_uname(),get_pass(),get_mobile(),get_email());
            return "true";
           }
        else {
            return "false";
        }
       }
    else {
    return "false";
    }
   }
  
   function add_to_database($first,$last,$user,$pass,$mobile,$email){
           include 'db_connect.php';
           $query = "INSERT INTO table_users VALUES ('$first','$last','$user','$pass','$mobile','$email')";
        mysql_query($query) or die($query);
    }
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: method does not return values ...

Post by RobertGonzalez »

So add_to_database is actually echoing out the SQL string?
icebreaker
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 1:50 am

Re: method does not return values ...

Post by icebreaker »

thats right ... it echos with the entered values in the form ... like "insert into table values ('john','smith', ...)"
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: method does not return values ...

Post by RobertGonzalez »

Can you crank up your error_reporting level to E_ALL and set display_errors to On then run it again. I am half suspicious that there is something in the db_connect file that is causing output of the script as opposed to variable setting.
icebreaker
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 1:50 am

Re: method does not return values ...

Post by icebreaker »

These are the only content of the db_connect file ...

Code: Select all

<?php 
 
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'admin';
 
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');  
    $dbname = 'test';
        mysql_select_db($dbname);
        
?>
Note : the data is added to the database ... so i think there cannot be any errors ,
icebreaker
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 1:50 am

Re: method does not return values ...

Post by icebreaker »

Hey comon people .... please help me .
im really lost without knowing the solution to this ....
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: method does not return values ...

Post by onion2k »

Change line 25 of the first file from:

Code: Select all

mysql_query($query) or die($query);
to

Code: Select all

mysql_query($query) or die("Query failed: ".mysql_error()." SQL: ".$query);
Then run it again and post whatever is printed.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: method does not return values ...

Post by RobertGonzalez »

icebreaker wrote:Hey comon people .... please help me .
im really lost without knowing the solution to this ....
Just a heads up... it is against our rules to bump your own thread within 24 hours of posting. 3 hours is not a very long time to wait. I know it is urgent to you but please respect the rules of our community.
icebreaker
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 1:50 am

Re: method does not return values ...

Post by icebreaker »

hey thanx dude ..this is the error message i got ..

Code: Select all

 
Query failed: Duplicate entry 'jsmith' for key 1 SQL: INSERT INTO table_users VALUES ('john','smith','jsmith','robocop','12345678900','z@g.com')
even though this error is shown ...all these values are entered into the database.
(surely my data does not have any duplicate values ... )

can someone help me solve this problem ?
icebreaker
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 1:50 am

Re: method does not return values ...

Post by icebreaker »

'jsmith' is the primary key

(just in case someone needs this info ....)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: method does not return values ...

Post by RobertGonzalez »

The data cannot be being entered more than once if the primary key is not being duplicated. The error you are getting is because the database is telling you that it cannot take a row with a duplicate primary key.
icebreaker
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 1:50 am

Re: method does not return values ...

Post by icebreaker »

So what u r saying is that ... i have another value named 'jsmith' in the primary key column ?

i checked it ... there is only one value called 'jsmith' .....
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: method does not return values ...

Post by RobertGonzalez »

Right. That value being there is what is preventing you from being able to add another row with a key of that value. That is what is leading to your error.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: method does not return values ...

Post by RobertGonzalez »

And for the record, to answer you original post, the reason it is showing the query is because you were actually die()ing with the query as the output of die.
icebreaker
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 1:50 am

Re: method does not return values ...

Post by icebreaker »

Hey thanx pal ... removing the "or die($query);" part solved the problem . :D
solved means .. the value i expected was returned .

but still that error msg comes when i add the

Code: Select all

or die("Query failed: ".mysql_error()." SQL: ".$query);
to the code .

i inserted different new values .. but still it gave the same error ...

so if u can pls find a solution for that..
another thing which i do not understand is : if there is an error related to primary key .. the database should not allow the data to be entered right ? but in this case every data i enter is added to the database .. can u pls explain y that is happening ..
Post Reply