Jump to content


Photo

$sql not returning results


  • Please log in to reply
39 replies to this topic

#1 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 04:34 PM

I need an update form on a page that will look to see if anything is entered in the users "welcome" field in their record and, if there is content, display it for editing / updating.

If there is nothing in the user's field enter "My Default Text" and use that to update the user's welcome field in their record.

Thus far I have the following.

In the login form
<input type="submit" name="kt_login1" id="kt_login1" value="Login" />
In the page that opens as a result of loggin in:
<? session_start();
        if (isset($_POST['kt_login1'])): 
         $_SESSION['kt_login_user'] = $_POST['kt_login_user'];
         $_SESSION['kt_name'] = $_POST['kt_name'];
        endif;
?>
<p>Hello <? echo  $_SESSION['kt_name'] ?> Choose a link to update a part of your records...</p> 
In the page that is to contain the update record form:
<? session_start();
        if (!isset($_SESSION['kt_login_user'])):
        $_SESSION['kt_login_user'] = "Anonymous";
        $_SESSION['kt_name'] = $_POST['kt_name'];
        $_SESSION['kt_welcome'] = $_POST['kt_welcome'];
endif;
?>
<p>Hello <? echo  $_SESSION['kt_name'] ?> Yada yadda...</p> 
<?php $sql = "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user"; 
$qry = mysql_query($sql);
if(@mysql_num_rows($qry) > 0) 
{
while($r = mysql_fetch_array($qry)) 
{
$kt_welcome = $r[1];
}
} 
else 
{
$kt_welcome = "My default text";
}
?>
<textarea name="welcome" cols="80" rows="20"><?php echo $_SESSION['kt_welcome']; ?></textarea>
<?php mysql_close(); ?>
I have created 2 users, one with content in the welcome field of their record and the other with a blank welcome field in the welcome field of their record.

What is happening at the moment is
1 - The "echo $_SESSION['kt_name" is returning the name of the logged in user (as expected)
2 - the only thing that is being returned in the textarea is My default text. The contents of the user with pre-existing data in his welcome field is not being returned in spite of being logged in.

I am coming to honestly believe that PHP was developed to drive me nuts.

I would appreciate some direction here. Many thanks in advance.




#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 May 2006 - 06:01 PM

Change:
$r[1];

To:
$r[0];

You're only selecting one field from the database and the fetch_array function starts the array at 0.

Info: PHP Manual


#3 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 06:08 PM

Thanks but that didn't make any difference.

[!--quoteo(post=371621:date=May 5 2006, 02:01 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 5 2006, 02:01 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Change:
$r[1];

To:
$r[0];

You're only selecting one field from the database and the fetch_array function starts the array at 0.
[/quote]


#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 May 2006 - 06:11 PM

I would also suggest putting quotes around $kt_login_user in this statement:
SELECT 'welcome' FROM `users` WHERE `id` = '$kt_login_user'

Failing those changes, I'd suggest verifying that the query is executing correctly and you have something other than the Default text in the database.

Info: PHP Manual


#5 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 07:01 PM

In the database fields one user's record has "This the text in the database" and the other user's record is blank

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 May 2006 - 07:04 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
would also suggest putting quotes around $kt_login_user in this statement:
SELECT 'welcome' FROM `users` WHERE `id` = '$kt_login_user'
[/quote]
so this didn't work either? you still need them so leave them there. try echoing $kt_login_user see if it's containing the value you want it to contain. maybe it's somehow not being passed right, so nothing is being selected from the db.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 07:17 PM

[!--quoteo(post=371650:date=May 5 2006, 03:04 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 5 2006, 03:04 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
so this didn't work either? you still need them so leave them there. try echoing $kt_login_user see if it's containing the value you want it to contain. maybe it's somehow not being passed right, so nothing is being selected from the db.
[/quote]


Yes, it returns the username (this is a positive thing)

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 May 2006 - 07:25 PM

so the column in your database that you use to hold the user name is called 'id'? just making sure, because most people use 'id' for an id number not the user name.

in other words, are you sure it's not supposed to be:
SELECT 'welcome' FROM `users` WHERE `[!--coloro:red--][span style=\"color:red\"][!--/coloro--]username[!--colorc--][/span][!--/colorc--]` = '$kt_login_user'

or some other such column name?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 May 2006 - 07:36 PM

also, if your 'id' column does indeed hold the user's name, then try changing this (assuming that you changed the 1 to a 0 earlier):

$kt_welcome = $r[0];

to this:

$kt_welcome = $r['welcome'];

also can you please update what your code looks like now, in a new post, just to make sure we're all on the same page.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#10 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 07:41 PM

[!--quoteo(post=371655:date=May 5 2006, 03:25 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 5 2006, 03:25 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
so the column in your database that you use to hold the user name is called 'id'? just making sure, because most people use 'id' for an id number not the user name.

in other words, are you sure it's not supposed to be:
SELECT 'welcome' FROM `users` WHERE `[!--coloro:red--][span style=\"color:red\"][!--/coloro--]username[!--colorc--][/span][!--/colorc--]` = '$kt_login_user'

or some other such column name?
[/quote]


Doh...

however, I changed it to
<?php $sql = "SELECT 'welcome' FROM 'users' WHERE 'username' = '$kt_login_user'";
but the results are the same

here is the structure of the table
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Field Type Null Default
id int(11) No
username varchar(32) No
password varchar(32) No
name varchar(50) No
lastname varchar(50) No
email varchar(50) No
designation varchar(50) Yes NULL
firm varchar(50) No
address varchar(50) No
box varchar(50) Yes NULL
suite varchar(20) Yes NULL
city varchar(20) No
province varchar(20) No
postalcode varchar(20) No
phone varchar(20) No
fax varchar(20) Yes NULL
level varchar(10) No
active varchar(50) No user
random varchar(50) No
regtime datetime Yes 0000-00-00 00:00:00
welcome text No
[/quote]

The code re-posted

<? session_start();
        if (!isset($_SESSION['kt_login_user'])):
        $_SESSION['kt_login_user'] = "Anonymous";
        $_SESSION['kt_name'] = $_POST['kt_name'];
        $_SESSION['kt_welcome'] = $_POST['kt_welcome'];
endif;
error_reporting(E_ALL);
?>
<?php echo $kt_login_user ?>
  
<p> </p>
<?php $sql = "SELECT 'welcome' FROM 'users' WHERE 'username' = '$kt_login_user'"; 
$qry = mysql_query($sql);
if(@mysql_num_rows($qry) > 0) 
{
// you have a result
while($r = mysql_fetch_array($qry)) 
{
$kt_welcome = $r['welcome'];
}
} 
else 
{
$kt_welcome = "My default text";
}
?>
<textarea name="welcome" cols="80" rows="20"><?php echo $_SESSION['kt_welcome']; ?></textarea>
<?php mysql_close(); ?>


#11 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 May 2006 - 07:45 PM

Ok... a few more things I noticed.

1) You're setting this: $kt_welcome;
You're echoing this: echo $_SESSION['kt_welcome'];

Those are not the same.

2) You don't need a while loop:
while($r = mysql_fetch_array($qry))
{
$kt_welcome = $r['welcome'];
}

becomes:
$r = mysql_fetch_array($qry);
$kt_welcome = $r['welcome'];

Info: PHP Manual


#12 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 May 2006 - 07:46 PM

so let me get this straight:

if the user puts something in the textbox that stores it in the 'welcome' column, when you check to see if there is an entry in 'welcome', if it exists, it's supposed to make $kt_welcome = $r['welcome'] but if not then it simply makes $kt_welcome = 'My default text."

so as of right now, you are saying that your script does indeed print out "My default text" when it is supposed to, but it is blank when there is something in the 'welcome' column?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#13 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 07:56 PM

[!--quoteo(post=371667:date=May 5 2006, 03:46 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 5 2006, 03:46 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
so let me get this straight:

if the user puts something in the textbox that stores it in the 'welcome' column, when you check to see if there is an entry in 'welcome', if it exists, it's supposed to make $kt_welcome = $r['welcome'] but if not then it simply makes $kt_welcome = 'My default text."
[/quote]

Boy, am I new...
I haven't got to creating the update form yet although I would think that to be the least of my worries. If I get the correct data in the text area when the query is run then I figure that taht is working (maybe I'm wrong here)

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]so as of right now, you are saying that your script does indeed print out "My default text" when it is supposed to, but it is blank when there is something in the 'welcome' column?[/quote]
No, right now it only prints My default text. It should print "Bill's data" when user #2 is logged in and My default text when #1 is logged in.



Could you please explain
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
1) You're setting this: $kt_welcome;
You're echoing this: echo $_SESSION['kt_welcome'];

Those are not the same.
[/quote]


Changed but result the same
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
<?php echo $kt_login_user ?>

<p>&nbsp;</p>
<?php $sql = "SELECT 'welcome' FROM 'users' WHERE 'username' = '$kt_login_user'";
$qry = mysql_query($sql);
if(@mysql_num_rows($qry) > 0)
{
$r = mysql_fetch_array($qry);
$kt_welcome = $r['welcome'];
}
else
{
$kt_welcome = "My default text";
}
?>
<textarea name="welcome" cols="80" rows="20"><?php echo $_SESSION['kt_welcome']; ?></textarea>
<?php mysql_close(); ?>
[/quote]


#14 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 May 2006 - 07:58 PM

You're setting a normal variable and echoing a session variable. Replace it with the following.

<?php echo $kt_login_user ?>

<p>&nbsp;</p>
<?php $sql = "SELECT 'welcome' FROM 'users' WHERE 'username' = '$kt_login_user'"; 
$qry = mysql_query($sql);
if(@mysql_num_rows($qry) > 0) 
{
$r = mysql_fetch_array($qry);
$kt_welcome = $r['welcome'];
}
else 
{
$kt_welcome = "My default text";
}
?>
<textarea name="welcome" cols="80" rows="20"><?php echo $kt_welcome; ?></textarea>
<?php mysql_close(); ?>

Info: PHP Manual


#15 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 08:03 PM

[!--quoteo(post=371675:date=May 5 2006, 03:58 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 5 2006, 03:58 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You're setting a normal variable and echoing a session variable. Replace it with the following.
[/quote]

Same I'm afraid


#16 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 May 2006 - 08:05 PM

okay then repost your updated code let's see what it looks like now.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#17 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 05 May 2006 - 08:06 PM

unless i misunderstood this problem, i suggest:
$qry = mysql_query("SELECT welcome FROM users WHERE username = '$kt_login_user'");

if(mysql_num_rows($qry) == 1) // user found
{
$r = mysql_fetch_array($qry);
$kt_welcome = $r['welcome'];

if(!$kt_welcome OR $kt_welcome =="") // if empty message
{
$kt_welcome = "My default text";
}

}

** EDITED a query cut'n paste error *lol*

#18 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 08:08 PM

Reposting code
<? session_start();
        if (!isset($_SESSION['kt_login_user'])):
        $_SESSION['kt_login_user'] = "Anonymous";
        $_SESSION['kt_name'] = $_POST['kt_name'];
        $_SESSION['kt_welcome'] = $_POST['kt_welcome'];
endif;
error_reporting(E_ALL);
?>
<?php echo $kt_login_user ?>

<p>&nbsp;</p>
<?php $sql = "SELECT 'welcome' FROM 'users' WHERE 'username' = '$kt_login_user'";
$qry = mysql_query($sql);
if(@mysql_num_rows($qry) > 0)
{
$r = mysql_fetch_array($qry);
$kt_welcome = $r['welcome'];
}
else
{
$kt_welcome = "My default text";
}
?>
<textarea name="welcome" cols="80" rows="20"><?php echo $kt_welcome; ?></textarea>
<?php mysql_close(); ?>


#19 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 08:32 PM

[!--quoteo(post=371682:date=May 5 2006, 04:06 PM:name=alpine)--][div class=\'quotetop\']QUOTE(alpine @ May 5 2006, 04:06 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
unless i misunderstood this problem, i suggest:
$qry = mysql_query("SELECT welcome FROM users WHERE username = '$kt_login_user'");
if(mysql_num_rows($qry) == 1)
{
$r = mysql_fetch_array($qry);
$kt_welcome = $r['welcome'];

if(!$kt_welcome OR $kt_welcome =="")
{
$kt_welcome = "My default text";
}
}

** EDITED a query cut'n paste error *lol*
[/quote]

I changed the code to reflect your edit
I received [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/pencils/public_html/interview/home_page.php on line 47
[/quote]
so I added @ to
if(@mysql_num_rows($qry) == 1)

With the change to your suggested the problem is reversed. Now the textarea contains the correct data for the user with data in the field and no entry for the user with no data in the field (which I assume is returning what it sees (empty) in the field. It should return "My default text" if there is no data in the users field.

Code update:
<?php echo $kt_login_user ?>

<p>&nbsp;</p>
<?php $qry = mysql_query("SELECT welcome FROM users WHERE username = '$kt_login_user'");
if(@mysql_num_rows($qry) == 1)
{
$r = mysql_fetch_array($qry);
$kt_welcome = $r['welcome'];

if(!$kt_welcome OR $kt_welcome =="")
{
$kt_welcome = "My default text";
}
}
?>
<textarea name="welcome" cols="80" rows="20"><?php echo $kt_welcome; ?></textarea>
<?php mysql_close(); ?>



Alpine:
I don't understand this line
if(!$kt_welcome OR $kt_welcome =="")


#20 cspgsl

cspgsl
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationMalagash Nova Scotia

Posted 05 May 2006 - 08:46 PM

Hope I haven't upset anyone...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users