Changing A Value From A Textbox - or something. don't exactly how to explain.

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Changing A Value From A Textbox - or something. don't exactly how to explain.

Feelay
Hey!

I am trying to do the following:
A user type his name in a textbox, and press the submit button.
Then, another file checks if the name exist in the database. if it does, the code will select the id from the database where the username is (SELECT id from username where username='$textboxvalue')


This is the code I am using:

CODE
<?php
session_start();
require "database.php";

$to=$_POST['message_to'];

$ck_reciever = mysql_query("SELECT username FROM user WHERE username = '".$to."'");
$to_id = mysql_query("SELECT id FROM user WHERE username = '".$to."'");
die($to_id ."<br>".$ck_reciever."<br>".$to);
?>


but when i try to echo all these variables (die(blabla))this is what I get:

QUOTE
Resource id #5
Resource id #4
feelay


anyone who knows what might be wrong?

Thanks //Feelay

 

 

 


Reply

Mordent
No idea what's up with your code that's making it not work, but on a fairly important issue I notice you're not doing any kind of escaping on the posted variable. Even if it's a simple mysql_real_escape_string(), it should stop a large portion of problems with people managing to insert horrible little bits of code in to your queries. wink.gif

Reply

pyost
mysql_query doesn't return an exact value, but a resource ID. You need to use mysql_result in order to extract the desired data from a resource ID.

Reply

Feelay
I've never used mysql_result in my own codes, because I have no idea how it works. Any help ?

Reply

Jeigh
try something like:

CODE
$ck_reciever = mysql_query("SELECT username FROM user WHERE username = '".$to."'");
$row = mysql_fetch_array($ck_reciever);
if ($row[0] == null) {
    //error or no result returned
  } else {
    //do what you want with the result. $row[0] should be the returned id
      }


When you use the mysql query it returns an item that you need to turn into useable data unless I'm completely off base. I haven't used php in awhile but I think thats sort of what you should look at doing

Reply

Feelay
should I change the "0" to the column name?

Reply

faulty.lee
QUOTE(Feelay @ Mar 16 2008, 02:25 AM) *
should I change the "0" to the column name?


For this case, you don't have to. [0] here refers to the first column of result you're returning. If in later stages, you start retrieving more columns of data, you might want to consider using the column name like ['id'] or ['username'] and so forth. Using text as index will compromise a little bit on speed. But it won't be noticeable until you start accessing hundreds of column per page. The alternative is to use constant. Declare constant with representative name and assign number to it, then use it to refer to your column, it's similar to using numbers directly, just that it's less confusing. E.g. $row[tablename_id], where tablename_id = 0.

Using constant and using number has an disadvantage, you need to take note of it when you change your query in the later stage, like retrieving more columns, or if you're using "*" to retrieve all the columns, the code will break when you update your table structures.

 

 

 


Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Recent Queries:-
  1. changing textbox data - 4.41 hr back. (1)
  2. changing data in textboxes - 6.16 hr back. (1)
  3. how to assign value to text box using php request - 13.22 hr back. (1)
  4. php textbox codes - 20.25 hr back. (2)
  5. php set textbox value - 26.00 hr back. (1)
  6. assign text box value in another textbox by php - 26.46 hr back. (1)
  7. assigning one textbox value to other textboxes in php - 33.25 hr back. (1)
  8. php text box - 55.36 hr back. (1)
  9. php textbox suggestion - 60.09 hr back. (1)
  10. changing textbox value by php - 60.13 hr back. (1)
  11. php mysql textbox sugestion - 63.15 hr back. (1)
  12. php get value from textbox - 72.19 hr back. (1)
  13. php display text box if another has a value - 93.86 hr back. (1)
  14. retrieve the value to the textbox from the database using php - 106.94 hr back. (1)
Similar Topics

Keywords : changing, textbox, explain,

  1. Making A Value In A Textbox Stay
    (4)
  2. Converting <enter> To \n In Textbox
    (2)
    I'm making a script that emails data and in the message box, I need all to be converted to \n
    so when it is mailed, it will format properly into: This is a test When submitted, it would be
    turned into: This \n is \n a \n test So that it can easily be mailed. Or does PHP do this
    automatically? Thanks! F....
  3. Logging Items Entered Into A Textbox
    (2)
    I'd like to have a log of items entered into a search box on my site. How can I do this so it
    will write to a text file but not interfear with search operations? Thanks! F....
  4. Help .. Cant Even Explain It
    (7)
    Alright.... I have a idea for my website but im not sure how to do it. Basicly i want to count my
    files and show it on the main page ... kinda like Background : 200 Scripts: 155
    Tutorials: 325 Templates: 5 Total: 695 like i said i dont know really where to start. any
    ideas? ....

    1. Looking for changing, textbox, explain,






*SIMILAR VIDEOS*
Searching Video's for changing, textbox, explain,
advertisement




Changing A Value From A Textbox - or something. don't exactly how to explain.