question on Variables

This is a discussion on "question on Variables" within the PHP Forum section. This forum, and the thread "question on Variables 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 8th, 2007, 19:17
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,521
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
question on Variables

would this work if i used Variables to replace the 'username' and 'password' here ?

PHP: Select all

mysql_query("INSERT INTO `members` VALUES (1, 'username', md5('password') )"); 

e.g

PHP: Select all

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

i want to create a form where those values can be changed.

thank you
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)

Last edited by saltedm8; Oct 8th, 2007 at 19:21.
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 8th, 2007, 19:42
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: question on Variables

You got it! That not only works, that is the whole point of using 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
  #3  
Old Oct 8th, 2007, 19:44
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,521
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: question on Variables

thank you, i just did not know if it would literly insert '$username' and '$password'

so how would i change the values from a form ?
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)

Last edited by saltedm8; Oct 8th, 2007 at 19:49.
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 8th, 2007, 20:00
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: question on Variables

Here's a little modified chunk of the login script I use on my site. Feel free to ask about anything you don't understand:
PHP: Select all

<?php

$error_message
='';
if(isset(
$_POST['login_user'])) {
    
// get user info for posted values
    
$user_name=mysql_real_escape_string($user_name);
    
$query=mysql_query("SELECT id FROM users WHERE user_name = '".$user_name."'");
    if(
mysql_num_rows($query)>0) {
        
$query=mysql_query("SELECT * FROM users WHERE user_name = '".$user_name."' AND password = '".mysql_real_escape_string($password)."'");
        if(
$row=mysql_fetch_object($query)) {
            
// log user in
            
$_SESSION['logged_in']=true;
        } else {
            
$error_message.='Password is not correct.';
        }
    } else {
        
$error_message.='User name does not exist.';
    }
}

if(
$_SESSION['logged_in']) { // user logged in
?>
<p>You are logged in</p>
<?php } else { ?>
<p>please login.</p>
<?php echo(($error_message!=''?'<div class="error">'.$error_message.'</div>':'')); ?>
<div class="contact">
    <form name="contact" action="<?php echo($link_prefix); ?>login" method="post">
        <p>User Name: <input class="box" type="text" name="login_user" value="<?php echo($_POST['login_user']); ?>" /></p>
        <p>Password: <input class="box" type="password" name="login_pass" /></p>
        <p><input class="button" type="submit" action="submit" value="Login" />
    </form>
</div>
<?php ?>
The part that you are interested in is where I access the values from the form using the $_POST array. If a form element has a name tag like so:
Code: Select all
<input type="text" name="username" />
Then you can access it in PHP like so:
PHP: Select all

$my_variable $_POST['username']; 

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 8th, 2007, 20:08
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,521
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: question on Variables

once the form is submitted is the value then stored for good in the variable? ( unless changed ) - simply because i have taken that a variable has to = something, what does it = ?

where is the value stored ?
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)

Last edited by saltedm8; Oct 8th, 2007 at 20:11.
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 8th, 2007, 20:29
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: question on Variables

I'm not too sure what you mean,
but once the form is submitted and the variable is put into the database, it will stay in the database.
The php variable will be lost when that particular script is ended.
Hope that is what you were asking.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 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
  #7  
Old Oct 8th, 2007, 20:40
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,521
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: question on Variables

ahhh, i think i have got it,

so i need to post the form data to the database and the variable will recover it

UPDATE 'members' etc
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
  #8  
Old Oct 8th, 2007, 20:43
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: question on Variables

Quote:
Originally Posted by saltedm8 View Post
would this work if i used Variables to replace the 'username' and 'password' here ?

PHP: Select all

mysql_query("INSERT INTO `members` VALUES (1, 'username', md5('password') )"); 

e.g

PHP: Select all

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

i want to create a form where those values can be changed.

thank you
Presuming the first field is an id field which auto-increments I would use '' (2 single quotes) instead of a number
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 8th, 2007, 21:26
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,521
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: question on Variables

does this look right ?

PHP: Select all

<?php include 'config.php';?>
<?php
$conn 
mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
if (isset(
$_POST['submit'])) {
  
$user $_POST['username'];
  
$pass $_POST['password'];
  
$sql="UPDATE members (username, password,)
        VALUES ('$user', '$pass')"
;  
mysql_select_db("$dbname",$conn);
mysql_query($sql) or die (mysql_error());
}
 
?>
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
  #10  
Old Oct 8th, 2007, 21:32
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: question on Variables

This is how I do it and you need to specify which record is getting updated (after the WHERE clause)

PHP: Select all

sql="UPDATE members SET username='$user', password='$pass' WHERE ........ "
Here is my query


PHP: Select all

$query="UPDATE articles SET title='$name', content='$newstr' WHERE id='$ud_id'"
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 9th, 2007, 06:13
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: question on Variables

What you're doing there is very dangerous and can and will be exploited by SQL injection. (Google for that if you're not sure SQL injection is). ALWAYS check user input before you use it in an SQL query!!!!

PHP: Select all

 $user mysql_real_escape_string($_POST['username']);
$pass mysql_real_escape_string($_POST['password']); 
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 9th, 2007, 06:17
Highly Reputable Member

SuperMember
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: question on Variables

Quote:
SQL injection is a technique that exploits a security vulnerability occurring in the database layer of an application
Quote:
A form of attack on a database-driven Web site in which the attacker executes unauthorized SQL commands by taking advantage of insecure code on a system connected to the Internet
Last Blog Entry: Whats your Niche? (Jun 10th, 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
  #13  
Old Oct 12th, 2007, 00:16
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: question on Variables

SQL Injection video
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 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
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
Using Variables in ASP with LightBox LeeNic Classic ASP 4 Mar 18th, 2008 15:12
Multiplying variables... Germaris Flash & Multimedia Forum 2 Feb 27th, 2008 23:26
Form - variables not going through comaiwat JavaScript Forum 3 Oct 24th, 2007 13:53
Variables!! bionics PHP Forum 6 Apr 25th, 2006 15:39
Get URL and use variables JamieH PHP Forum 2 Jan 1st, 2006 03:13


All times are GMT. The time now is 13:08.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization 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 1