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



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

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 11th, 2007, 19:42
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,299
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 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());
}
?>
__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)

Last edited by saltedm8; Oct 13th, 2007 at 14:48.
Reply With Quote

  #2 (permalink)  
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)
Reply With Quote
  #3 (permalink)  
Old Oct 12th, 2007, 09:03
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
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
Reply With Quote
  #4 (permalink)  
Old Oct 12th, 2007, 16:53
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,299
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 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()'
__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)
Reply With Quote
  #5 (permalink)  
Old Oct 12th, 2007, 19:11
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
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?
Reply With Quote
  #6 (permalink)  
Old Oct 12th, 2007, 19:15
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,299
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 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
__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)

Last edited by saltedm8; Oct 12th, 2007 at 19:18.
Reply With Quote
  #7 (permalink)  
Old Oct 12th, 2007, 20:04
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
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
Reply With Quote
  #8 (permalink)  
Old Oct 12th, 2007, 20:31
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,299
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 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') )"); 
__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)

Last edited by saltedm8; Oct 12th, 2007 at 20:46.
Reply With Quote
  #9 (permalink)  
Old Oct 12th, 2007, 22:04
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
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
Reply With Quote
  #10 (permalink)  
Old Oct 12th, 2007, 22:30
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
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
Reply With Quote
  #11 (permalink)  
Old Oct 13th, 2007, 14:09
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,299
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 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
__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)
Reply With Quote
  #12 (permalink)  
Old Oct 13th, 2007, 14:28
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
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.
Reply With Quote
  #13 (permalink)  
Old Oct 13th, 2007, 14:32
saltedm8's Avatar
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,299
Blog Entries: 1
Thanks: 1
Thanked 6 Times in 6 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
__________________
My Recipe forum...don't click here
Last Blog Entry: Basic Advice for newbies (Feb 1st, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Image UPDATE problem kidreapertronV PHP Forum 2 Jan 31st, 2008 18:38
[SOLVED] PHP contact form redirect to same form Posie PHP Forum 14 Jan 29th, 2008 20:28
[SOLVED] need a little help with my asp form Andrew1986 Classic ASP 3 Nov 23rd, 2007 07:55
So, You have three weeks free and nothing to do? Accurax Webforumz Cafe 21 May 6th, 2007 19:18
Update form question David Blake Databases 0 Nov 26th, 2005 15:59


All times are GMT. The time now is 05:41.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43