[SOLVED] 2 weeks trying to update from a form

This is a discussion on "[SOLVED] 2 weeks trying to update from a form" within the PHP Forum section. This forum, and the thread "[SOLVED] 2 weeks trying to update from a form are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 11th, 2007, 19:42
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,518
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
[SOLVED] 2 weeks trying to update from a form

i have spent the last 2 weeks trying to update a table from a form and just cannot get it, the times i get no errors, it does not update, please help

change.php
PHP: Select all

<?php include 'config.php';?>
<?php 
include 'change_user.php';?>
<div align="center">
  <table width="355" border="1" bordercolor="#000000">
    <tr>
      <td height="439"><form action="<?php echo $_SERVER['PHP_SELF'?>" method="post">
      <p>
        <div align="center">
    <p>&nbsp;</p>
    <p><strong>CREATE YOUR USERNAME AND PASSWORD</strong></p>
    <p>&nbsp;</p>
    <table width="408" height="79" border="1" bordercolor="#000000">
      <tr>
        <td width="197" height="38"><strong>ENTER NEW USERNAME</strong></td>
        <td width="201"><label>
          <div align="center">
            <input name="username" type="text" id="username" size="25" maxlength="25">
            </div>
        </label></td>
      </tr>
      <tr>
        <td><strong>ENTER NEW PASSWORD</strong></td>
        <td><label>
          <div align="center">
            <input name="password" type="text" id="password" size="25" maxlength="30">
            </div>
        </label></td>
      </tr>
    </table>
    <table width="413" border="0">
      <tr>
        <td width="407"><div align="right">
          <label>
          <input type="submit" name="Submit" id="Submit" value="Submit">
          </label>
        </div></td>
      </tr>
    </table>
    <p>&nbsp;</p>
  </div>
</form>&nbsp;
<table width="414" height="54" border="0">
  <tr>
    <td width="408">&nbsp;</td>
  </tr>
</table></td>
    </tr>
  </table>
</div>
change_user.php

PHP: Select all

<?php include 'config.php';?>
<?php
$conn 
mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
function 
filterThis($string) {
$string htmlentities($string);
$string mysql_real_escape_string($string);
return 
$string; }
$username '$string';
$password '$string' ;
$string filterThis($username);
$string filterThis($password);
$query sprintf("SELECT * FROM members WHERE username AND password");
mysql_real_escape_string($username);
mysql_real_escape_string($password);
 
if (isset(
$_POST['submit'])) {
  
$username $_POST['username'];
  
$password $_POST['password'];
  
$sql="UPDATE members id='username' 'password','
        VALUES ('$username','$password', )"
;
 
mysql_select_db("$dbname",$conn);
mysql_query($sql) or die (mysql_error());
}
?>
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)

Last edited by saltedm8; Oct 13th, 2007 at 14:48.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 12th, 2007, 07:14
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: 2 weeks trying to update from a form

Hi Saltedm8,

Can you explain a bit more about what you're trying to do? Do you have the current user details available anywhere or are these values to create a new entry in the database?

I have this code that is based off what little I could pick up from your script

PHP: Select all

<?php*include*'config.php';?>

<?php

// Connect to the database
$conn*=*mysql_connect($dbhost,*$dbuser,*$dbpass)*or*die*('Error*connecting*to*mysql');
mysql_select_db("$dbname",$conn) or die('Ooops, we have no db. I am sad now');
// Used to filter and return clean input
function*filterThis($string)*
{
$string*=*htmlentities($string);
$string*=*mysql_real_escape_string($string);
return*
$string;*
}

// I'm not sure whether you are trying to retrieve data or add it so we'll do both
// $username*=*'$string'; What exactly is in $string?
// $password*=*'$string'*;


$username*=*filterThis($_POST['username']);
$password*=*filterThis($_POST['password']);
$query*=*"SELECT***FROM*members*WHERE*id = '$username'*AND*password = $password");
mysql_real_escape_string($username);
mysql_real_escape_string($password);
*
if*(isset(
$_POST['submit']))*{

    
$username*=*filterThis($_POST['username']);
    
$password*=*filterThis($_POST['password']);

    
// to insert into use the INSERT SQL
**$sql="INSERT INTO*members*(id, password) 
********VALUES*('$username','$password',*)"
;*

mysql_query($sql)*or*die*(mysql_error());

    
// To update you need to use the UPDATE Query but if you're changing both it would be good to have something
    // else to use in your WHERE clause like a user id or their current username so assuming you add a hidden form element
    // named currentid use this
    
$currentid filterThis($_POST['currentid']);
    
$sql "UPDATE members SET id = '$username', password = '$password' WHERE id = '$currentid'";
}
?>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 12th, 2007, 09:03
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: 2 weeks trying to update from a form

As Rakuli has shown in the script that he came up with, that your SQL query was wrong and you didn't specify which record was getting updated.

Hese has a lot of useful examples on PHP, SQl etc so it worth having a look http://www.tizag.com/sqlTutorial/sqlupdate.php
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 12th, 2007, 16:53
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,518
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: 2 weeks trying to update from a form

i am just trying to create a form that is able to change an existing username and password stored in the database, but i wanted to use 'MD5()'
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 12th, 2007, 19:11
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: 2 weeks trying to update from a form

Who is doing the changing?

Is it the user who's username and password that need to be changed?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 12th, 2007, 19:15
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,518
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: 2 weeks trying to update from a form

there only needs to be one table with id, username and password fields, only access will be for admin to do EVERYTHING, there will be no user access to anything on the site at all

i just want to be able to adjust it without entering the database, obviously converting it to md5 etc
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)

Last edited by saltedm8; Oct 12th, 2007 at 19:18.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Oct 12th, 2007, 20:04
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: 2 weeks trying to update from a form

How are you getting that record that needs to be updated? Are all usenames displayed in a table somewhere (html table) or are you going to search for a specific record?

i.e. I have a list of records with radio buttons and I click the radio button to edit that record

I only ask these questions becuase they are probably so many different ways to do it depending on the situation
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Oct 12th, 2007, 20:31
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,518
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: 2 weeks trying to update from a form

mysql

PHP: Select all

  $mem="CREATE TABLE members (
  id int(4) NOT NULL auto_increment,
        username varchar(65) NOT NULL default '',
        password varchar(65) NOT NULL default '',
        PRIMARY KEY (id)
      ) TYPE=MyISAM AUTO_INCREMENT=2 "
;
 
  
mysql_query($mem) or die (mysql_error());
 
  
mysql_query("INSERT INTO `members` VALUES (1, 'saltedm8', md5('alarm7') )"); 
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)

Last edited by saltedm8; Oct 12th, 2007 at 20:46.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Oct 12th, 2007, 22:04
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: 2 weeks trying to update from a form

I've looked at your code and I should have a solution in my next post.

There seems to be a lot of confusion with you php and html and I will try and sort it out for you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Oct 12th, 2007, 22:30
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: 2 weeks trying to update from a form

A few things here

First, this only needs to be executed once

PHP: Select all

$mem="CREATE TABLE members (
  id int(4) NOT NULL auto_increment,
        username varchar(65) NOT NULL default '',
        password varchar(65) NOT NULL default '',
        PRIMARY KEY (id)
      ) TYPE=MyISAM AUTO_INCREMENT=2 "

2. You could just use this for username and same for password

PHP: Select all

$username mysql_real_escape_string($_POST['username']); 

3. Don't use tables for layout. I tried using your source code to see the form and it was all over the place and i didn't see any input fileds

4. Change these

HTML: Select all
<input name="password" type="text" id="password" size="25" maxlength="30">
to
HTML: Select all
<input name="password" type="password" name="password" size="25" maxlength="30">
HTML: Select all
<input name="username" type="text" id="username" size="25" maxlength="25">
to
HTML: Select all
<input name="username" type="text" name="username" size="25" maxlength="25">
HTML: Select all
<input type="submit" name="Submit" id="Submit" value="Submit">
to
HTML: Select all
<input type="submit" name="Submit" name="Submit" value="Submit">
5. You could use this to create the md5 password

PHP: Select all

$db_password md5($password); 

6. and to insert into the database use this

PHP: Select all

mysql_query("INSERT INTO `members` (username, password) VALUES ('$username', '$db_password')"); 

7. When updating a record you need to know which record you are updating

PHP: Select all

mysql_query("UPDATE users SET username='$username', password='$password WHERE id='$id'")) 

When performing your SELECT query you could use the WHERE clause for either the id or username...it's up to you.

If you want to see an old example of mine before i improved it i'll post the code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Oct 13th, 2007, 14:09
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,518
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: 2 weeks trying to update from a form

what a bloomin pain that was !!!

ended with this

PHP: Select all

<?php include 'config.php';?>
<?php
$conn 
mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
if (isset(
$_POST['submit']))
{
$username mysql_real_escape_string($_POST['username']); 
$db_password mysql_real_escape_string($_POST['password']);
$db_password md5($db_password);

$upmem ="INSERT INTO members (username, password) VALUES ($username, $db_password)"
$upmem "UPDATE members SET username = '$username', password = '$db_password' WHERE 1"
mysql_select_db("$dbname",$conn);
mysql_query($upmem) or die (mysql_error());
}
?>
works perfectly

thanks for your help
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Oct 13th, 2007, 14:28
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: 2 weeks trying to update from a form

You only need to perform one query for which action you want to take

When you add a new member you use
PHP: Select all

$upmem ="INSERT INTO members (username, password) VALUES ($username, $db_password)"
When you want to update a member's details, you use
PHP: Select all

$upmem "UPDATE members SET username = '$username', password = '$db_password' WHERE 1"
Just remember that your WHERE clause will always change depending on which record you want to update.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Oct 13th, 2007, 14:32
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,518
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: 2 weeks trying to update from a form

i was thinking about that, but i will sort that out later, at the moment i have built it for one user and no more, i could always re-write it later if i wanted - for now i am happy with what i have, cheers
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!