Changing a field in mysql table with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rickburgen
Forum Newbie
Posts: 10
Joined: Thu Mar 05, 2009 5:41 pm

Changing a field in mysql table with php

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Changing a field in mysql table with php

Post 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"
);
 
rickburgen
Forum Newbie
Posts: 10
Joined: Thu Mar 05, 2009 5:41 pm

Re: Changing a field in mysql table with php

Post by rickburgen »

Just one question. What does LIMIT 1 do?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Changing a field in mysql table with php

Post by Benjamin »

Prevents more than 1 record from being updated.
rickburgen
Forum Newbie
Posts: 10
Joined: Thu Mar 05, 2009 5:41 pm

Re: Changing a field in mysql table with php

Post by rickburgen »

Thanks. What are your feelings on cookies. Are they practical to have. And for what uses can they be used for.?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Changing a field in mysql table with php

Post 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.
rickburgen
Forum Newbie
Posts: 10
Joined: Thu Mar 05, 2009 5:41 pm

Re: Changing a field in mysql table with php

Post by rickburgen »

Can I use sessions, rather than cookies. Or is that like comparing apples and bananas.?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Changing a field in mysql table with php

Post 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.
rickburgen
Forum Newbie
Posts: 10
Joined: Thu Mar 05, 2009 5:41 pm

Re: Changing a field in mysql table with php

Post 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' />&nbsp;<a href=$dir$file>$file</a><br />\n";
echo "<hr />";
}

closedir($dh);
?>
rickburgen
Forum Newbie
Posts: 10
Joined: Thu Mar 05, 2009 5:41 pm

Re: Changing a field in mysql table with php

Post 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.?
The Challenger
Forum Newbie
Posts: 3
Joined: Tue Mar 10, 2009 10:17 am

Re: Changing a field in mysql table with php

Post 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 ...;
rickburgen
Forum Newbie
Posts: 10
Joined: Thu Mar 05, 2009 5:41 pm

Re: Changing a field in mysql table with php

Post by rickburgen »

Are you replying to my first post? Cause I already fixed that.
Post Reply