Page 1 of 1

Retrieving data from database

Posted: Tue Nov 03, 2009 4:23 pm
by phpnoobyyy
Hi guys

I have a database with fields 'a', 'b', 'c' and 'd'. I want to retrieve info from the database and display it using a form where the info in fields a, b, c or d equals the info selected in the form. I have the following php code and form to achieve this:

PHP code:

Code: Select all

if ($_POST['xyz']) {
        $x = trim($_POST['xyz']);
    } else {
        $errors[] = '<small id="error">Error</small>';
    }
        
        
// Make the query:
$q = "SELECT CONCAT(first_name, ' ', last_name) AS name, (email) FROM users 
WHERE a OR b OR c OR d = '$x'";        
$r = @mysqli_query ($dbc, $q); // Run the query.
 
if ($r) { // If it ran OK, display the records.
 
    // Table header.
echo     'display data';
HTML form

Code: Select all

<select name="xyz" class="input"/>
    <option value="something1">XXX</option>   
    <option value="something2">XXX</option>
        </select>
My problem is that when i execute the form all records in the database are being retrieved regardless of whether the database field has the required data.

I've looked into what may be going wrong without success.

Any heads up on what may be going wrong would be greatly appreciated.

Many thanks.

Re: Retrieving data from database

Posted: Tue Nov 03, 2009 4:33 pm
by AbraCadaver
Query looks bad. Try this:

Code: Select all

$q = "SELECT CONCAT(first_name, ' ', last_name) AS name, (email) FROM users WHERE a = '$x' OR b = '$x' OR c = '$x' OR d = '$x'";

Re: Retrieving data from database

Posted: Tue Nov 03, 2009 4:41 pm
by phpnoobyyy
Hi

I actually just tried that literally before coming back on the forum to check if anyone had replied and was just going to post that i had solved the problem!

Thanks for confirming the solution.