 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
05-07-2009, 10:41 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
|
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
|
|
|
05-07-2009, 10:58 PM
|
#2 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
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'];
|
|
|
|
05-07-2009, 11:01 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
|
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
|
|
|
05-07-2009, 11:07 PM
|
#4 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Randy
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?
|
|
|
|
05-07-2009, 11:01 PM
|
#5 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
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).
|
|
|
|
05-07-2009, 11:08 PM
|
#6 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Village Idiot
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...
|
|
|
|
05-07-2009, 11:12 PM
|
#7 (permalink)
|
|
The Acquainted
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
|
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
|
|
|
05-07-2009, 11:19 PM
|
#8 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Randy
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...
|
|
|
|
05-07-2009, 11:21 PM
|
#9 (permalink)
|
|
The Acquainted
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
|
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
|
|
|
05-07-2009, 11:29 PM
|
#10 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Randy
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.....
|
|
|
|
05-08-2009, 03:24 PM
|
#11 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by allworknoplay
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
|
|
|
|
05-08-2009, 04:12 PM
|
#12 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Village Idiot
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... 
|
|
|
|
05-07-2009, 11:28 PM
|
#13 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
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.
|
|
|
|
05-08-2009, 05:25 PM
|
#14 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
05-08-2009, 05:38 PM
|
#15 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by sketchMedia
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:
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|