Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Compare $_POST Strings

Posted on 2006-05-08
6
Medium Priority
?
265 Views
Last Modified: 2013-12-12
Hi all

Wonder if you help me out please.

What i am doing is creating a list box from a db and echoing the db username into the list box (all working good)($_POST['list_box'] )
im then doing a while statement to gather all the usernames then creating an array
Im then assigning a variable $frm_username = $_POST['username'] (from the array)
and a variable for the auto increment column $frm_user_id = $_POST['id']

My query is
$sql = "SELECT * FROM table_name WHERE username = ($_POST['list_box'] )"
mysql_query ($sql)

The syntax above may not be correct as i have quickly typed this up at work but hope you get the idea
the problem im having is when the username is in the list box more than once and im calling all the data from that username it as no way to differ between both names that’s why i added the id column to try and make it look at two fields in the db rather than just the username but not sure how to call this in my $sql variable

Any ideas greatly appreciated
0
Comment
Question by:avo42
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16630255
$sql = "SELECT * FROM table_name WHERE username = '" .  $_POST['list_box'] . "' "
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16630461
you should build your list this way
<option value=userID>username</option>

e.g.
<option value=15>Mark</option>

usually, you don't search for a unique record by name, as names are likely to be repeated, that's why you use id
if u use id (preferably auto increment), then all you have to do is to search for that user by his id

the above select statement will show the name of the user but will send its id to the processing page, and the select statment would be like this
$sql = "SELECT * FROM table_name WHERE userID = '" .  $_POST['list_box'] . "' "
0
 

Author Comment

by:avo42
ID: 16633800
Hi thanks for the reply

i see where you are coming from but the 'userID' column is never going to = $_POST['list_box']

$_POST['list_box']  is the username selected
userID is the auto increment column

i need a way to select the username if there is multiple of the same name pressent in the db and the userid in the same row as the username selected

All help appriciated
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:avo42
ID: 16633826
This is my code :

[code]

session_start () ;


// includes
require ('admin_header.php') ;
include ('includes/dbconfig.php') ;


//connect to db
mysql_connect ($dbhost, $dbuser, $dbpass) ;
mysql_select_db ($dbname) or die ( mysql_error ());
//get form data for list box
$query = "SELECT username FROM member_messages ORDER BY username ASC";
$result = mysql_query ($query) or die ( mysql_error () );


//get form data for form

$query_all = "SELECT * FROM member_messages WHERE username='".$_POST ['list_box']."'";
$result_all = mysql_query ($query_all) or die ( mysql_error () );
//fetch all other rows

while ( $row = mysql_fetch_assoc($result_all)) {
//assign variable to field names
$frm_id = "{$row['id']}" ;
$frm_name = "{$row['name']}" ;
$frm_email = "{$row['email']}" ;
$frm_message = "{$row['message']}" ;
$frm_date = "{$row['date']}" ;
$frm_title = "{$row['title']}" ;
$frm_message = "{$row['admin_message']}" ;
}

//when edit user pressed output list box name to username text box field
if ($_POST ['Edit_user']) {
$_SESSION['$PHP_SELF'] ;
}


//creates array for all usernames in db
while($nt=mysql_fetch_array($result)) {
echo "<option value='{$nt["username"]}'>{$nt["username"]}</option>";
}

[/code]
0
 
LVL 16

Accepted Solution

by:
dr_dedo earned 2000 total points
ID: 16634490
i've made a few mods to make you get what i mean

<?
session_start () ;


// includes
require ('admin_header.php') ;
include ('includes/dbconfig.php') ;

//connect to db
mysql_connect ($dbhost, $dbuser, $dbpass) ;
mysql_select_db ($dbname) or die ( mysql_error ());
//get form data for list box
$query = "SELECT username,userID FROM member_messages ORDER BY username ASC"; //<-- assume u have userID to each use which is already save in db and is auto increment
$result = mysql_query ($query) or die ( mysql_error () );


//get form data for form

$query_all = "SELECT * FROM member_messages WHERE username='".$_POST ['list_box']."'";
$result_all = mysql_query ($query_all) or die ( mysql_error () );
//fetch all other rows

while ( $row = mysql_fetch_assoc($result_all)) {
//assign variable to field names
$frm_id = $row['id'];
$frm_name = $row['name'] ;
$frm_email = $row['email'];
$frm_message = $row['message'];
$frm_date = $row['date'];
$frm_title = $row['title'];
$frm_message = $row['admin_message'];
}

//when edit user pressed output list box name to username text box field
if ($_POST ['Edit_user']) {
$_SESSION['$PHP_SELF'] ;
}


//creates array for all usernames in db
echo '<select name="user">';
while($nt=mysql_fetch_array($result)) {
echo "<option value='{$nt["userID"]}'>{$nt["username"]}</option>"; //<-- this selet will show usernames but when the form is submited, $_POST['user'] will equal the user id
}
echo '</select>';

?>

notice that if u use autoincrement keys, each row will be unique, and searching by id eliminates the possibilty of selecting more than one rrecord due to similar usernames !!
0
 

Author Comment

by:avo42
ID: 16634783
HI

Thankyou I now get it dont know what i have been thinking ive been posting into the list_box the username > username instead of the id (value)>username(display)
with you help ive now got it sorted thanks i finished up with this code .

[code]

$query = "SELECT id, username FROM member_messages ORDER BY id ";

$query_all = "SELECT * FROM member_messages WHERE id='".$_POST ['list_box']."'";

echo "<option value='{$nt["id"]}'>{$nt["username"]}</option>";

[/code]  

it works just as i planned

cheers
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

804 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question