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 @:
Only registered users can see links on this board! Get registered or login to the forums!
U can C my work in progress @:
Only registered users can see links on this board! Get registered or login to the forums!
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.
Joined: Aug 27, 2002 Posts: 16987 Location: Kansas
Posted:
Thu Jun 10, 2004 1:12 pm
What I do when I have this sort of a problem would be to take this code
Code:
$sql = "SELECT * FROM ".$prefix."_users_extended WHERE username = $uname";
and add this after it. This should ECHO the sql call after php resolves it.
Code:
die('sql = '.$sql);
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.
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))
{
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?
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?
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
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.
Joined: Nov 15, 2003 Posts: 53 Location: Hawaii and the Fan Forum
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.
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: userinfo-.html$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
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.
Only registered users can see links on this board! Get registered or login to the forums!
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...
Only registered users can see links on this board! Get registered or login to the forums!
...with the latest version of the code. Everything is there if U wanna look it over.
U can view it in action here:
Only registered users can see links on this board! Get registered or login to the forums!
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:
Only registered users can see links on this board! Get registered or login to the forums!
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:
Joined: Nov 15, 2003 Posts: 53 Location: Hawaii and the Fan Forum
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();
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
Only registered users can see links on this board! Get registered or login to the forums!
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.
Joined: Nov 15, 2003 Posts: 53 Location: Hawaii and the Fan Forum
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
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'";
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.
View next topic View previous topic
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum