TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-07-2009, 10:41 PM   #1 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default Personal Notes via PHP

So i know i just got help with some update settings from yall but this one is quite different in a way.

I am trying to have a personal notepad in the user/admin dashboard but for some reason it won't update like last time or read the data from the mysql database to post..

Full Code:
http://pastie.org/471693

Code To Read & Post Data To Form:
PHP Code:
            $notesquery mysql_query("SELECT * FROM user WHERE `username` = '$username' && `password` = '$password'") or die(mysql_error()); 
            
$notesresult mysql_fetch_array$notesquery ); 
            
$notes $notesresult['notes']; 
Code To Update/Display Form:
PHP Code:
            <?php
                            
if (isset($_POST['submit'])) {

                            
// Define Variables
                            
$notes $_POST['notes'];
                            
                        
// Attempt To Update
                        
$update mysql_query("UPDATE user SET `notes` = '".$notes."' WHERE `username` = '$username' && `password` = '$password'") or die(mysql_error());
                            if(
$update) {
                                echo 
"Success";
                                }  
                        
                } else {
            
                
?>
              <form id="form1" name="form1" method="post" action="">
                My Personal Notes<br />
                  <label class="textarea">
                    <textarea name="notes" id="notes" rows="6" cols="80"><?php echo $notes ?></textarea>
                  </label>
                  <p><small>Any notes that you put in this box are your private notes, Nobody else will be able to see them, so make them as secret as you want.</small></p>
                <p>
            <input name="submit" id="submit" type="submit" />
                  </p>
              </form>
              <?php ?>
It won't even display the $notes in the form.. nor will it update the mysql database, im not sure whats going on. please help again.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 05-07-2009, 10:58 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Let's do it one step at a time. Let's first clean up your query..



PHP Code:
$notesquery mysql_query("SELECT * FROM user WHERE `username` = '".$username."' AND `password` = '".$password."' ") or die(mysql_error()); 
            
$notesresult mysql_fetch_array$notesquery ); 
$notes $notesresult['notes']; 
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 11:01 PM   #3 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

not quite sure how i would do that as this is how i read to do it in a few tutorials i found on google :p
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 05-07-2009, 11:01 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

When I debug queries in .net, I use the following steps:
1. Output the formatted query that you are sending. That means you echo the exact same string you are giving the database. That can reveal potential errors that are in the variables.
2. Simulate a correct query in the database viewer.
3. Erase the query and re-write it from square one
4. Repeat

I've only had to ask two questions regarding queries in the last year with this method (and don't say I am better with SQL, I have had to write extremely advanced queries I learned how to write ten minutes before).
__________________

Village Idiot is offline  
Reply With Quote
Old 05-07-2009, 11:07 PM   #5 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Randy View Post
not quite sure how i would do that as this is how i read to do it in a few tutorials i found on google :p
I don't understand? Not sure about what?
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 11:08 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
When I debug queries in .net, I use the following steps:
1. Output the formatted query that you are sending. That means you echo the exact same string you are giving the database. That can reveal potential errors that are in the variables.
2. Simulate a correct query in the database viewer.
3. Erase the query and re-write it from square one
4. Repeat

I've only had to ask two questions regarding queries in the last year with this method.
Very much agree on #1. So many times when I put the query in the database itself, CLI, it doesn't work. If it works via CLI, it will most surely work in your PHP scripts...
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 11:12 PM   #7 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

1. Worked Fine
2. First Had Errors Cause of MD5 Hashed Passwords
3. Rewrote without verifying the password as that seems to be what was causing it.

So it worked.

Final Code:
PHP Code:
            <?php
                            
if (isset($_POST['submit'])) {

                            
// Define Variables
                            
$notes $_POST['notes'];
                            
                        
// Attempt To Update
                        
$update mysql_query("UPDATE user SET `notes` = '".$notes."' WHERE `username` = '$username'") or die(mysql_error());
                            if(
$update) {
                                echo 
$notes;
                                }  
                        
                } else {
            
                
?>
              <form id="form1" name="form1" method="post" action="">
                My Personal Notes<br />
                  <label class="textarea">
                    <textarea name="notes" id="notes" rows="6" cols="80"><?php echo $notes ?></textarea>
                  </label>
                  <p><small>Any notes that you put in this box are your private notes, Nobody else will be able to see them, so make them as secret as you want.</small></p>
                <p>
            <input name="submit" id="submit" type="submit" />
                  </p>
              </form>
              <?php ?>
as for your first comment allworknoplay, im not sure what you mean by cleaning it up, seems alright to me.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 05-07-2009, 11:19 PM   #8 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Randy View Post
1. Worked Fine
2. First Had Errors Cause of MD5 Hashed Passwords
3. Rewrote without verifying the password as that seems to be what was causing it.

So it worked.

Final Code:
PHP Code:
            <?php
                            
if (isset($_POST['submit'])) {

                            
// Define Variables
                            
$notes $_POST['notes'];
                            
                        
// Attempt To Update
                        
$update mysql_query("UPDATE user SET `notes` = '".$notes."' WHERE `username` = '$username'") or die(mysql_error());
                            if(
$update) {
                                echo 
$notes;
                                }  
                        
                } else {
            
                
?>
              <form id="form1" name="form1" method="post" action="">
                My Personal Notes<br />
                  <label class="textarea">
                    <textarea name="notes" id="notes" rows="6" cols="80"><?php echo $notes ?></textarea>
                  </label>
                  <p><small>Any notes that you put in this box are your private notes, Nobody else will be able to see them, so make them as secret as you want.</small></p>
                <p>
            <input name="submit" id="submit" type="submit" />
                  </p>
              </form>
              <?php ?>
as for your first comment allworknoplay, im not sure what you mean by cleaning it up, seems alright to me.
Glad to hear it worked...

I just have my own personal preference when it comes to queries. So it looks like yours was fine..but I like to use AND instead of && etc....stuff like that....no biggie...
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 11:21 PM   #9 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

ah yes my friend told me to use && i dont quite know the big difference.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 05-07-2009, 11:28 PM   #10 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

using AND over && is a matter of preference, I personally like && so I don't get AND or and confused with a string for some odd reason whilst scanning code.
Enfernikus is offline  
Reply With Quote
Old 05-07-2009, 11:29 PM   #11 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Randy View Post
ah yes my friend told me to use && i dont quite know the big difference.

I like to go for readability.

So AND instead of &&.

OR instead of ||

etc...other than preference, I don't think SQL really cares which way you do it...

I also like to make my SELECT statements capitalized and columns,tables lowercase like this:

SELECT * FROM user WHERE user_id = 5;


Again, just preference.....
allworknoplay is offline  
Reply With Quote
Old 05-08-2009, 03:24 PM   #12 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
I like to go for readability.

So AND instead of &&.

OR instead of ||.
To make a counterpoint, && and || are the universal* standards for AND and OR. Since you are writing programming code, it is best to stick to the programming language opposed to English. I personally find AND and OR really messy and hard to read (primarily because it can be easily mistaken for non-logic code). Although I do admit that I tend to use AND and OR a lot because I didn't realize you could use && and || till I was in SQL for a while and habit formed.

*Except for BASIC, but that hardly counts
__________________

Village Idiot is offline  
Reply With Quote
Old 05-08-2009, 04:12 PM   #13 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
To make a counterpoint, && and || are the universal* standards for AND and OR. Since you are writing programming code, it is best to stick to the programming language opposed to English. I personally find AND and OR really messy and hard to read (primarily because it can be easily mistaken for non-logic code). Although I do admit that I tend to use AND and OR a lot because I didn't realize you could use && and || till I was in SQL for a while and habit formed.

*Except for BASIC, but that hardly counts

Oh come on....we all know English is the *universal language...


hahaha....just kidding...
allworknoplay is offline  
Reply With Quote
Old 05-08-2009, 05:25 PM   #14 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

In PHP AND and OR operate under different precedence rules to || and &&.
http://uk3.php.net/manual/en/languag...precedence.php

An example:
PHP Code:
$e false || true;
$f false or true
On the face of it, it seems like both operate the same and thus both evaluate to 'true' however this wont behave as expected, and $f will be assigned 'false', why?

If you look at the precedence chart on php.net, you see that 'or' is lower down the list than '||' this means that it has lower precedence, this in itself isn't the issue however.
The issue arises when you use it in conjunction with another operator, for example '='. '||' is higher in the list than 'or', so therefore any expression with '=' in it must be evaluated first, thus:

PHP Code:
$f false or true;
//php interprets as
($f false) or true
Hope that clarifies it.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-08-2009, 05:38 PM   #15 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
In PHP AND and OR operate under different precedence rules to || and &&.
http://uk3.php.net/manual/en/languag...precedence.php

An example:
PHP Code:
$e false || true;
$f false or true
On the face of it, it seems like both operate the same and thus both evaluate to 'true' however this wont behave as expected, and $f will be assigned 'false', why?

If you look at the precedence chart on php.net, you see that 'or' is lower down the list than '||' this means that it has lower precedence, this in itself isn't the issue however.
The issue arises when you use it in conjunction with another operator, for example '='. '||' is higher in the list than 'or', so therefore any expression with '=' in it must be evaluated first, thus:

PHP Code:
$f false or true;
//php interprets as
($f false) or true
Hope that clarifies it.
Wow, what a great find Sketch! I tried this out just now and you are correct...this is almost an easter egg....

In PHP, I always use '||', I never use OR or AND surprisingly so I hope that by using '||' and '&&', I won't ever run into any issues that spits out the wrong results...

How come when something evals to true, you get 1, but when it evals to false, you don't get any output? I thought a '0' would be the output?

The output of your code above would be:

e: 1
f:
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get PHP 5. to work? Newbie windows PHP guy DotNetTim Absolute Beginners 11 02-01-2013 11:02 AM
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 06:55 AM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 12:14 AM
what are all the subjects in php? sarmenhb General 7 01-21-2008 05:41 PM
Uploading Files with PHP daz Absolute Beginners 3 09-30-2007 06:23 PM


All times are GMT. The time now is 06:59 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design