Author |
Message |
TheShniz
Regular


Joined: Jan 27, 2004
Posts: 65
|
Posted:
Wed Jun 09, 2004 1:08 pm |
|
I'm Xperienced in VB, VB.Net, Oracle (although been a lil bit) & am now muddling my way through PHP using basic trail & error. I've read a coup diff books on the language basics. But I'm just confuzed now
A lil background:
I run a Clan (club) 4 Online Gaming called Mystic Heroes... & I ran across Dick Snel's ClanMember module, which I thought was pretty good. But it had quite a few bugs & severe limitations 4 what I was looking 4. So I started rewriting & adapting it, using it as a foundation 4 learning PHP & PHP-Nuke.
My prob could very likely B a stupid (see also: Operator Error) syntax error or something a lil more elaborate, I'm guessing the former. My puzzlement comes from figuring out how 2 retreive data from mySQL in2 fields, allowing user modification, and updating the database.
Anyways, I'm getting this error:
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /nfs/home/mhclan2/public_html/modules/Members/index.php on line 446
Here's line 446:
Code:
$sql = "SELECT * FROM ".$prefix."_users_extended WHERE username = $uname";
$resultaat = mysql_query($sql, $dbi);
while ($record = mysql_fetch_object($resultaat))
{
|
I then have a bunch of HTML/Form/Post info, & then my SQL 2 post updates:
Code:
$sql1 = "UPDATE ".$prefix."_users_extended SET comment1 = '$comment1', comment2 = '$comment2', comment3 = '$comment3', comment4 = '$comment4' WHERE username='$uname'";
mysql_query($sql1, $dbi) or die("An occured an error. Please try again later");
|
I understand this is only a snapshot of what all is going on, so I've hosted everything I've done so far @:
http://www.mhclan.com/members.zip
U can C my work in progress @:
http://www.mhclan.com/modules.php?name=Members
It should all B self Xplanatory as I've documented everything I've done w/ the intentions of making it available 2 every1 once completed. Please keep in mind this is a work in progress. I'd appreciate any & all advice/help/assistance I could get.
Like I said, could B a 2sec Xplanation or a "what's going on here" analysis.
:: edited ::
1.) My profile (TheShniz) is the only Clan Member w/ an Xtended Profile. I still haven't created a way 2 add records in Users_Extended when an Administrator adds them 2 the Clan.
2.) I've registered a User for any1 willing 2 test:
Username: Test
Password: testing
3.) I've added the Test User as a Clan Member, so they R able 2 view the Xtended Profile & Edit their own... whereas non-Clan Members R Not.
An oversight on my part, but U'll need this info 2 B able 2 view the Member Only info stuff, & more specifically the prob I'm trying 2 figure out.
Thanx!!!!!!!!!!!!!!!!!
> TheShniz |
|
|
|
 |
sixonetonoffun
Spouse Contemplates Divorce

Joined: Jan 02, 2003
Posts: 2496
|
Posted:
Wed Jun 09, 2004 2:17 pm |
|
Try this maybe?
while ($record = mysql_fetch_object($resultaat, $dbi))
In the newer sqllayer its more like this but it looks like your using the older way.
while ($record = $db->sql_fetch_object($resultaat)) { |
|
|
|
 |
TheShniz

|
Posted:
Wed Jun 09, 2004 10:55 pm |
|
I tried your 1st suggestion, but same prob
Your 2nd Xample produced this:
Fatal error: Call to undefined function: sql_fetch_object() in /nfs/home/mhclan2/public_html/modules/Members/index.php on line 447
I can only assume U're correct that it's using the "old way."
Anyways, the source is there if any1 wants 2 peruse
P.S.
If there's a better way 2 do what I'm attempting, pleaz lemme know!
> TheShniz |
|
|
|
 |
TheShniz

|
Posted:
Thu Jun 10, 2004 12:25 pm |
|
Any ideas/help?
> TheShniz |
|
|
|
 |
Raven
Site Admin/Owner

Joined: Aug 27, 2002
Posts: 17088
|
Posted:
Thu Jun 10, 2004 1:12 pm |
|
What I do when I have this sort of a problem would be to take this codeCode:$sql = "SELECT * FROM ".$prefix."_users_extended WHERE username = $uname";
| and add this after it. This should ECHO the sql call after php resolves it.
Then I go into phpMySQL and open up the SQL window and copy an paste the line (to the right of the = sign) and run it. phpMyAdmin usually helps with its error message. |
|
|
|
 |
TheShniz

|
Posted:
Thu Jun 10, 2004 3:00 pm |
|
Whoohoo, thanx very much 4 that tipoff... made some good progress The prob was that I was querying a table where username = whatever, but I didn't put quotes around it (B'ing that it's a string). I'm now successfully retreiving the info from the Database. Now on 2 the next hurdle..... the notorious Checkbox......
How do I process/save the value of a Checkbox in2 the Database?
This is what I'm doing:
Code:
$sql = "SELECT * FROM ".$prefix."_users_extended WHERE username = '$uname'";
$resultaat = mysql_query($sql, $dbi);
while ($record = mysql_fetch_object($resultaat))
{
echo "
<form>
HTML CODE TO POPULATE FORM WITH DATABASE INFO
<input type=\"checkbox\" name=\"chksatday\" value=\"Yes\" border=0 ";
if ($record->tuemorn == 1) { echo " checked"; }
echo " >
<input type=\"submit\" name=\"submit\" value=\"Update Profile\">
</form>
";
if ($submit) {
if ($_POST["chksatday"] == 'Yes') { $record->satday = 1; }
if ($_POST["chksatday"] == '') { $record->satday = 0; }
$sql1 = "UPDATE ".$prefix."_users_extended SET satday = '$satday' WHERE username='$uname'";
mysql_query($sql1, $dbi) or die("An occured an error. Please try again later");
}
|
I'm now successfully saving the Text fields using similar code:
$record->whatever = $_POST["whatever"];
But, unfortunately, now the checkboxes R B'ing cleared whether they're checked or not. Any suggestions?
P.S.
How do I redirect the user 2 view their profile (ie. a specific URL) after they click Submit & everything is processed?
Thanx again!!!!!
> TheShniz |
|
|
|
 |
sixonetonoffun

|
Posted:
Thu Jun 10, 2004 6:14 pm |
|
Something like
echo "<form action=\"modules.php?name=$module_name\" method=\"post\">"
should work |
|
|
|
 |
TheShniz

|
Posted:
Fri Jun 11, 2004 12:07 am |
|
HMmmmmmmmm, I tried that....... & it definately forwarded me 2 the link, but it skipped the part about saving the info 2 the database Is there another way mayB.... 2 where it 4wards U after the 'Submit' is processed?
P.S.
Any ideas about saving the value of a Checkbox?
Thanx guys!
> TheShniz |
|
|
|
 |
Audioslaved
Regular


Joined: Nov 15, 2003
Posts: 53
Location: Hawaii and the Fan Forum
|
Posted:
Fri Jun 11, 2004 3:01 am |
|
Yes, checkboxes are rather easy, the value is only passed on the click of a form if the checkbox is checked, if the checkbox is not checked, than it will not be passed or in other words, will not be set. For example
Code:
echo "<input type=\"checkbox\" name=\"testcheck\" value=\"1\">";
|
The above code with create a checkbox that is not checked, pretending the checkbox is already in a form, we magically press our submit button.
Since the checkbox was not checked the value was not passed to the script, only when the checkbox is checked will it be set, you can see this for yourself by putting this in the top of your case or function
Code:
echo "<pre>";
print_r($_POST);
echo "</pre>";
|
This will print all the values passed in your $_POST super global array (you can do the same for all superglobals or arrays for that matter)
You can test this yourself, by submitting when it is checked and when it is not checked. From there in your code you can see if it is set like so
Code:
$blah = $_POST['testcheck'];
if(isset($blah)) {
echo "I rule";
} else {
echo "I dont rule";
}
|
You can do the above a number of ways I have been attempting to train myself to get away from globals a bit you could just use $testcheck instead
Let me know if you want better examples, need more help, etc.
-Bill (Audioslaved) |
_________________ The Audioslave Fan Forum
For the Fans, By the Fans
http://www.audioslaved.com |
|
|
 |
Audioslaved

|
Posted:
Fri Jun 11, 2004 3:09 am |
|
To foward after the submit process you could use the header function
Header("Location: url of user profile");
Also in your form tag, you could put $_SERVER['PHP_SELF'] where it says action and define the method as being post, I am not sure what method is used if the method is not defined, if it is $_GET than you having post in your statements above would not work, I would define the method as POST
Perhaps defining submit as $submit = $_POST['submit'];
More suggestions for you, hope you get it working. |
|
|
|
 |
Audioslaved

|
Posted:
Fri Jun 11, 2004 3:23 am |
|
Here is what I kind of put together for this situation, just trying to help you and possibly give some code examples you can possibly use:
Code:
require_once("mainfile.php");
function main() {
global $prefix, $dbi;
$uname = Not sure of where uname is coming from; :)
$sql = "SELECT * FROM ".$prefix."_users_extended WHERE username = '$uname'";
$resultaat = mysql_query($sql, $dbi);
while ($record = mysql_fetch_object($resultaat))
{
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"POST\">
HTML CODE TO POPULATE FORM WITH DATABASE INFO
<input type=\"checkbox\" name=\"chksatday\" value=\"Yes\" border=0 ";
if ($record['tuemorn'] == 1) { echo " checked"; }
echo " >
<input type=\"submit\" name=\"submit\" value=\"Update Profile\">
<input type=\"hidden\" name=\"uname\" value=\"$uname\">
</form>
";
}
function submit() {
global $prefix, $dbi;
if (isset($_POST['chksatday']) && $_POST['chksatday'] == "Yes") {
$satday = 1;
} else {
$satday = 0;
}
$uname = $_POST['uname'];
$sql1 = "UPDATE ".$prefix."_users_extended SET satday = '$satday' WHERE username='$uname'";
mysql_query($sql1, $dbi) or die("An occured an error. Please try again later");
Header("Location: modules.php?name=Your_Account&op=userinfo&username=$uname");
}
if (isset($_POST['submit'])) {
submit();
} else {
main();
}
|
Hope some of that helps, I am not really sure of your big picture so some of this may be off base
-Bill (Audioslaved) |
|
|
|
 |
TheShniz

|
Posted:
Fri Jun 11, 2004 11:08 am |
|
All I can say is wow, that's alot of helpful info! Ok, my module is just about complete. Now all the checkboxes are saving! I've got 2x things left 2 do: 1.) Figure out how 2 4ward after Submit is processed, & 2.) Insert & Delete records from users_extended whenever I add/remove a user as a member.
U've put a lot of great stuff there, I especially like how U can echo the _POST. What does the print_r function do specifically? It was MOST helpful. w/ those great answers, I've got some more Q's...
1.) What does print_r do? Is this a specific function or an ability of PHP.
2.) What does isset do? Does it just C if the variable is defined or not null? Is this a specific function or an ability of PHP.
3.) 'function submit() {' wouldn't work 4 me, but the 'if ($submit) {' would. Just wouldn't update the record on submit.
4.) Bcause of #3...
Code:if (isset($_POST['submit'])) { submit(); } else { main(); }
|
...wouldn't work either. What Xactly is this if statement trying 2 do?
...here's the error code...
Code:Fatal error: Call to undefined function: submit() in /nfs/home/mhclan2/public_html/modules/Members/index.php on line 663
|
...of course, since I didn't use submit as a function since it wouldn't update.
5.) When I specified the Header...
Code:Header("http://www.mhclan.com/modules.php?name=Members&profile=$uname");
|
...it would 4ward me instead 2: http://www.mhclan.com/modules.php and report "Sorry, such file doesn't exist..."
6.) How do U continue a line in code 2 the next line? eg.) In Visual Basic, U simply type '& _' There's gotta B a similar way in PHP. Would help clean up some things instead of having SQL statements go on 4ever.
Like I said, the Updating of Extended Profiles is now 100% working!!!! Whoohoo! I've just gotta figure out how 2 4ward the user 2 their profile now since right now, it's simply refreshing the page w/ the data BEFORE they edited & saved it. The only way they would know it was saved was if they scrolled 2 the bottom of the page 2 C the message "Profile Updated." Once that's done, the final thing I have 2 do is again figure out how 2 Insert & Delete records in the Users_Extended table when an Administrator adds a user as a (Clan) Member.
What am I doing wrong here on the 4warding?
P.S.
I've updated...
http://www.mhclan.com/members.zip
...with the latest version of the code. Everything is there if U wanna look it over.
U can view it in action here:
http://www.mhclan.com/modules.php?name=Members
In order 2 C members' Xtended Profiles, U must 1st B a member (not viewed by registered public or anonymous). The 'Test' Username w/ password 'testing' is still active and a declared member if U wanna C how the member only info worx.
:: edited ::
$uname comes from a prev Q I posted in the phpNuke 7.3 forum called 'User Session Info?' and is located here: http://www.ravenphpscripts.com/postt1755.html
Basically, it's what's allowing me 2 identify who the user is that's currently viewing the page. I then look up their info 2 C if they're a member, if they R... then I show the members' only info.
The code part of it is defined @ the beginning of the file:
Code:
global $user, $db, $cookie, $prefix
getusrinfo( $user );
cookiedecode( $user );
$uname = $cookie[1];
select * from whatever where username = $uname
|
Thanx so much!
> TheShniz  |
|
|
|
 |
TheShniz

|
Posted:
Fri Jun 11, 2004 11:20 am |
|
If there's a better way of doing things than what I've done, pleaz lemme know!
> TheShniz |
|
|
|
 |
Audioslaved

|
Posted:
Mon Jun 14, 2004 11:33 pm |
|
To make redirection work, in your script
FIND:
Code:
OpenTable();
echo "<center>Your Extended Profile is now Updated<p>Click <a href=\"/modules.php?name=Members&profile=$uname\">Here</a> to View</p></center>";
CloseTable();
|
REPLACE WITH:
Code:
HEADER("Location: modules.php?name=Members&profile=$uname");
|
OR, you could even do a delayed refresh to the page using this:
Code:
OpenTable();
echo "<center>Your Extended Profile is now Updated. You will be automatically redirected to your updated profile.</center>";
CloseTable();
echo "<META HTTP-EQUIV=\"refresh\" content=\"2;URL=modules.php?name=Members&profile=$uname\">";
|
The above will wait two seconds before redirecting them to the profile URL. That should give them enough time to read the message before being redirected. Any longer and it has almost a stagnant effect of "When is this going to happen?"
Hope that helps.
As far as your questions, I will answer them as I find time to, they are pretty easy, one thing to keep in mind is php.net, most often you can put php.net/name_of_function to see a related page on what you have a question about, i.e www.php.net/print_r should give you all php's information on what print_r() does and how it is used. I can tell you that it prints the contents and values of an array, though there may be other puposes for it.
-Bill (Audioslaved) |
|
|
|
 |
Audioslaved

|
Posted:
Mon Jun 14, 2004 11:55 pm |
|
Quote: |
1.) What does print_r do? Is this a specific function or an ability of PHP.
|
As defined by PHP, The PHP print_r function displays information about a variable in a way that's readable by humans. If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements.
Quote: |
2.) What does isset do? Does it just C if the variable is defined or not null? Is this a specific function or an ability of PHP.
|
The php function isset determines whether or not a variable is set,
so by saying
Code:
if (isset($_POST['submit'])) {
|
you are actually saying if $_POST['submit'] is set than...
You could also say
Code:
if (! isset($_POST['submit'])) {
|
you are saying if $_POST['submit'] is not set than...
Quote: |
3.) 'function submit() {' wouldn't work 4 me, but the 'if ($submit) {' would. Just wouldn't update the record on submit.
|
the reason the function submit wouldn't work for you is because you never defined function submit in your script. The $submit worked because that is the global variable of the submit button (name=submit)
Quote: |
if (isset($_POST['submit'])) { submit(); } else { main(); }
|
The statement was part of my example, only an example, it was saying if $_POST is set than go to the submit function, if it is not set than go to the main function. These functions were created in my test script, they were only an example.
Quote: |
When I specified the Header...
Code:
Header("http://www.mhclan.com/modules.php?name=Members&profile=$uname");
|
This did not work because you forgot to specify Location: before the URL, it should look like
Code:
HEADER("Location: modules.php?name=Members&blah=blah");
|
Quote: |
How do U continue a line in code 2 the next line? eg.) In Visual Basic, U simply type '& _' There's gotta B a similar way in PHP. Would help clean up some things instead of having SQL statements go on 4ever.
|
You can break it up by just going to the next line, there is no character, PHP will read it until it recieves the proper closing marks. For example
Not broken up
Code:
$sql = "SELECT id FROM ".$prefix."_ice_cream_tastes_good WHERE yum='1'";
|
broken up
Code:
$sql = "SELECT id
FROM ".$prefix."_ice_cream_tastes_good
WHERE yum='1'";
|
Hope that answers your questions.
-Bill (Audioslaved) |
|
|
|
 |
TheShniz

|
Posted:
Tue Jun 15, 2004 9:11 am |
|
:: que sound effect ::
OHhhhhhhhhhhhhhhhhhhhhhhhh, AHhhhhhhhhhhhhhhhhhhhhh, HMmmmmmmmmmmmmmmm, interesting even
The redirection now worx! I didn't realize U meant 2 type out Location: specifically, thought U were saying 2 type the URL. :: struck by lightening :: Ah hah
Thanx 4 telling me about the function listing on PHP.Net. That's good 2 know. I've tried 2 run through the manual/documentation, but that there is good The manual divides everything in2 categories, so no real list.
Well............ everything is working now. All that's left is 2 automate/code the Insertion of a new record in2 the users_extended table when admin defines user as member.
Thanx a bunch!
> TheShniz |
|
|
|
 |
|