Jump to content

[SOLVED] SQL Statement. cannot retieve a record I know exists


Recommended Posts

Hi all

I have a record in my mysql table"users"  keyed on a username JohnAin.

 

this SQL statment finds it ...

 

SELECT * FROM users WHERE username = 'JohnAin'  "

 

This does not ...

 

SELECT * FROM users WHERE username = '".$uname."' "

 

 

and this does not ...

 

SELECT * FROM users WHERE username = '".addslashes($uname)."' "

 

On the line before the select statement runs I echo $uname and it reads as JohnAin on the scree.

 

Any ideas will be welcome.

 

Regards to all

 

John

Hi

 

I changed line 111/112 to read

 

  if (DB::isError($check) || $check->numRows() == 0) {

        die("That username (".$uname.") does not exist in our database.");

 

and the darned thing echoed out ok for me there too. it read  JohnAin which is exactly the constant I used to get a successful retrieval.

 

Phew !!!

 

Regrds

 

John

that is right, I realised that just after I posted, but I put it in anyway.

 

I changed to

 

<form method="post" action="../scripts/changeuser.php">

 

... and it refinds the form (as it did before)  but still comes back saying ...

 

"That username ( JohnAin ) does not exist in our database." 

 

Which of course is not the case since a select using the constant "JohnAin"  does find and retrieve the record.

 

Regards

 

John

sorry, just to be clear, it is these statments that I have the problem with ...

 

case 2: // Start step 2. User details maintenance.

    if (!get_magic_quotes_gpc()) {

        $_POST['uname'] = addslashes($_POST['uname']);

    }

    ?>

    <br /><h4>User maintenance - Maintain user details for ...<br /> <?echo($uname) ?></h4>

    <?

    $qry = "SELECT * FROM users WHERE username = '$uname'";

      //$qry = "SELECT username, password FROM users WHERE username = '".$_POST['uname']."'";

      $check = $db_object->query($qry);

 

    if (DB::isError($check) || $check->numRows() == 0) {

        die("That username (".$uname.") does not exist in our database.");

    }

    break;

 

 

The select will find my record when I use a constant for the key, but not when I use $uname.

 

Regards

 

John

<?php
    $uname = $_POST['uname'];
    if (!get_magic_quotes_gpc()) {
        $uname = addslashes($uname);
     }
     ?>
     <br /><h4>User maintenance - Maintain user details for ...<br /> <?echo($uname) ?></h4>
     <?php
     $qry = "SELECT * FROM users WHERE username = '$uname'";
      //$qry = "SELECT username, password FROM users WHERE username = '".$_POST['uname']."'";
      $check = $db_object->query($qry);

     if (DB::isError($check) || $check->numRows() == 0) {
        die("That username (".$uname.") does not exist in our database.");
    }
?>

Since you can put in 'JohnAin' directly in the query and it works, there is something specific with the value that comes from the form.

 

You posted this above -

 

"That username ( JohnAin ) does not exist in our database."

 

If that line is EXACTLY as it was output to the browser, there is leading and trailing white-space on the name. The posted code would have given (JohnAin) with no spaces.

 

Use the trim() function before the query -

 

$uname = trim($uname);

 

If this does not solve it, then your browser/form in doing some character encoding, so that the query does not match the name but the name is "displayed" correctly when you echo it.

LOL. I looked at your code for the form and it is deliberately putting in a space before and after the name in the value (everyone here probably thought you were typing the name in instead of selecting it from a dropdown) -

 

$linetext = "<option value= ' ".mysql_result($result,$i)." ' >".mysql_result($result,$i)."</option>";

 

Change this to -

 

$linetext = "<option value= '".mysql_result($result,$i)."' >".mysql_result($result,$i)."</option>";

Hi Guys

 

I finally worked it out with your help.

 

My basic error was forgetting that I was constructing a string containing an SQL statment, not the statement itself!

 

The solution is ...

 

$qry = "SELECT * FROM users WHERE username = "."'".$uname."'";

 

I hope I do not do anything as daft again ... but I bet I do

 

Regards to you all and thanks for all of your help.

 

John

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.