Page 1 of 2
method does not return values ...
Posted: Tue Aug 05, 2008 1:58 am
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);
}
Re: method does not return values ...
Posted: Tue Aug 05, 2008 2:28 am
by RobertGonzalez
So add_to_database is actually echoing out the SQL string?
Re: method does not return values ...
Posted: Tue Aug 05, 2008 2:31 am
by icebreaker
thats right ... it echos with the entered values in the form ... like "insert into table values ('john','smith', ...)"
Re: method does not return values ...
Posted: Tue Aug 05, 2008 2:38 am
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.
Re: method does not return values ...
Posted: Tue Aug 05, 2008 3:14 am
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 ,
Re: method does not return values ...
Posted: Tue Aug 05, 2008 6:10 am
by icebreaker
Hey comon people .... please help me .
im really lost without knowing the solution to this ....
Re: method does not return values ...
Posted: Tue Aug 05, 2008 6:59 am
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.
Re: method does not return values ...
Posted: Tue Aug 05, 2008 9:32 am
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.
Re: method does not return values ...
Posted: Tue Aug 05, 2008 11:23 pm
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 ?
Re: method does not return values ...
Posted: Tue Aug 05, 2008 11:42 pm
by icebreaker
'jsmith' is the primary key
(just in case someone needs this info ....)
Re: method does not return values ...
Posted: Wed Aug 06, 2008 12:09 am
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.
Re: method does not return values ...
Posted: Wed Aug 06, 2008 12:29 am
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' .....
Re: method does not return values ...
Posted: Wed Aug 06, 2008 12:44 am
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.
Re: method does not return values ...
Posted: Wed Aug 06, 2008 12:46 am
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.
Re: method does not return values ...
Posted: Wed Aug 06, 2008 1:26 am
by icebreaker
Hey thanx pal ... removing the
"or die($query);" part solved the problem .
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 ..