TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Personal Notes via PHP (http://www.talkphp.com/absolute-beginners/4248-personal-notes-via-php.html)

Randy 05-07-2009 10:41 PM

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.

allworknoplay 05-07-2009 10:58 PM

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']; 


Randy 05-07-2009 11:01 PM

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

Village Idiot 05-07-2009 11:01 PM

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).

allworknoplay 05-07-2009 11:07 PM

Quote:

Originally Posted by Randy (Post 23825)
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 05-07-2009 11:08 PM

Quote:

Originally Posted by Village Idiot (Post 23826)
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...

Randy 05-07-2009 11:12 PM

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.

allworknoplay 05-07-2009 11:19 PM

Quote:

Originally Posted by Randy (Post 23829)
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...

Randy 05-07-2009 11:21 PM

ah yes my friend told me to use && i dont quite know the big difference.

Enfernikus 05-07-2009 11:28 PM

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.

allworknoplay 05-07-2009 11:29 PM

Quote:

Originally Posted by Randy (Post 23831)
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.....

Village Idiot 05-08-2009 03:24 PM

Quote:

Originally Posted by allworknoplay (Post 23833)
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

allworknoplay 05-08-2009 04:12 PM

Quote:

Originally Posted by Village Idiot (Post 23848)
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... :-P

sketchMedia 05-08-2009 05:25 PM

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.

allworknoplay 05-08-2009 05:38 PM

Quote:

Originally Posted by sketchMedia (Post 23851)
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:


All times are GMT. The time now is 05:20 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0