Page 1 of 1
Changing a field in mysql table with php
Posted: Thu Mar 05, 2009 5:46 pm
by rickburgen
Hi. I am creating a settings page for my website. I want the user to be able to change their password there.
This is the code I have already..
$sql1="DELETE password FROM users WHERE username='$username'";
$result2=mysql_query($sql1);
$sql2 = "INSERT INTO users SET password='$newpass' WHERE username='$username'";
$result3 = mysql_query($sql2);
if ($result2)
echo "Settings Saved!";
This code is obviously not working. By that I mean not changing the old password. What can be done to fix this?
Re: Changing a field in mysql table with php
Posted: Thu Mar 05, 2009 9:10 pm
by Benjamin
Hey bud, you only need one query:
Code: Select all
mysql_query(
"UPDATE
users
SET
password = '" . mysql_real_escape_string($newpass) . "'
WHERE
username = '" . mysql_real_escape_string($username) . "'
LIMIT 1"
);
Re: Changing a field in mysql table with php
Posted: Thu Mar 05, 2009 9:19 pm
by rickburgen
Just one question. What does LIMIT 1 do?
Re: Changing a field in mysql table with php
Posted: Thu Mar 05, 2009 10:01 pm
by Benjamin
Prevents more than 1 record from being updated.
Re: Changing a field in mysql table with php
Posted: Thu Mar 05, 2009 11:34 pm
by rickburgen
Thanks. What are your feelings on cookies. Are they practical to have. And for what uses can they be used for.?
Re: Changing a field in mysql table with php
Posted: Thu Mar 05, 2009 11:38 pm
by Benjamin
Cookies are used to store persistent data. They should only be viewed as an untrusted assistant because they can be deleted/modified/created by end users. Special care needs to be taken when implementing them to ensure that it's the best and most practical solution.
Re: Changing a field in mysql table with php
Posted: Thu Mar 05, 2009 11:48 pm
by rickburgen
Can I use sessions, rather than cookies. Or is that like comparing apples and bananas.?
Re: Changing a field in mysql table with php
Posted: Fri Mar 06, 2009 12:00 am
by Benjamin
Yes, you can use sessions, that is fine. The issue is that sessions expire when the browser is closed whereas cookies can last for days, months, etc. Sessions actually use temporary cookies that expire when the browser is closed. Sessions can work without cookies, however you need to pass the session_id around to each and every page in order for it to work.
Re: Changing a field in mysql table with php
Posted: Sat Mar 07, 2009 7:26 pm
by rickburgen
My next goal, I am working on is to display files that a user uploaded to a special folder. I have it so that when the user uploads a file, the name, file-type(doc./audio/video), file-size, and users' name is stored into a table. The folder has the name of the user, along with the file they uploaded. I want to display each file on one line. Any help is appreciated.This is the code I am using. But when the page loads, it displays a period that links to the uploads folder, and two dots that links to the $username folder. How can this be fixed the show neither of those.
<?php
$username = $_SESSION['user'];
$dir = "uploads/$username/";
$dh = opendir($dir);
while (($file = readdir($dh)) !== false) {
echo " <input type='checkbox' name='$file' /> <a href=$dir$file>$file</a><br />\n";
echo "<hr />";
}
closedir($dh);
?>
Re: Changing a field in mysql table with php
Posted: Thu Mar 12, 2009 8:21 am
by rickburgen
I fixed the problem by putting
if ($file != "." && $file != "..")
before the output.
Now, I need to display the files! How can I display them in the browser.?
Re: Changing a field in mysql table with php
Posted: Thu Mar 12, 2009 9:53 am
by The Challenger
You have to use mysql fetch array for u print the fields of the database in your website
the sentece is:
while ( $total = Mysql_fetch_array($result2)){
$field1 = $total['field1'];
$field2 = $total['field2'];
$field.....
}
echo $field1 $field2 ...;
Re: Changing a field in mysql table with php
Posted: Thu Mar 12, 2009 11:17 am
by rickburgen
Are you replying to my first post? Cause I already fixed that.