简体   繁体   中英

Database query php MySQL - no search results displayed

(PROBLEM IS VERY DETAILED FOR TOO LONG DIDN'T READ: "My guess is that i'm using the MYSQL_FETCH_ARRAY wrong.")

Hello! The following codes purpose is to do a basic search in the database. The data is passed by a form. The tutorial I was using was written by: 'Frost of Slunked.com' and it was a basic register/login php MySQL tutorial, which worked perfectly. I managed to write a woking table-updater function and form-submit to add new data to the selected (so that's working as intended.

config.php - conncets to the MySQL server, selects the database, starts the session, requires the functions.php (with authors comments included)

<?php
/*****************************
    File: includes/config.php
    Written by: Frost of Slunked.com
    Tutorial: User Registration and Login System
******************************/
// start the session before any output.
session_start();

// Set the folder for our includes
$sFolder = ''; 

/***************
    Database Connection 
        You will need to change the user (user) 
        and password (password) to what your database information uses. 
        Same with the database name if you used something else.
****************/
mysql_connect('localhost', 'myusername', 'mypassword') or trigger_error("Unable to connect to the database: " . mysql_error());
mysql_select_db('tormex') or trigger_error("Unable to switch to the database: " . mysql_error());

/***************
    password salts are used to ensure a secure password
    hash and make your passwords much harder to be broken into
    Change these to be whatever you want, just try and limit them to
    10-20 characters each to avoid collisions. 
****************/
define('SALT1', '24859f@#$#@$');
define('SALT2', '^&@#_-=+Afda$#%');

// require the function file
require_once 'functions.php';

// default the error variable to empty.
$_SESSION['error'] = "";

// declare $sOutput so we do not have to do this on each page.
$sOutput="";
?>

functions.php - has multiple functions (login, createRide, Register etc.). Most of the functions purpose is to get the values from the submitted HTML forms and then maintain the required actions - I will only mentioned my searchRide function (which in my guess has the error or atleast, has to do something with it) and the createRide function, which is working properly.

<?php ...

unction searchRide($pWhen_min, $pWhen_max, $pFrom, $pTo){
    if (!empty($pWhen_min) && !empty($pWhen_max) && !empty($pFrom) && !empty($pTo)) {
        global $sql2, $query2;
        $sql2 = "SELECT * FROM ride WHERE `from` ='$pFrom' AND `to` = '$pTo' AND `when` >= '$pWhen_min' AND `when` <= '$pWhen_max' ";
        $query2 = mysql_query($sql2) or trigger_error("Query Failed: " . mysql_error());
    }
}
function createRide($pFrom, $pTo, $pWhen, $pSeats, $pPrice, $pCar){
    if (!empty($pFrom) && !empty($pTo) && !empty($pWhen) && !empty($pSeats) && !empty($pPrice) && !empty($pCar)){
        $sql = "SELECT id FROM users WHERE username= '" . $username . "' LIMIT 1";
        $result = mysql_query($sql);
        if(!$result) {
            trigger_error("ELKURTAD " . mysql_error());
        }
        $row = mysql_fetch_array($result);
        $sql = "INSERT INTO ride (`from`, `to`, `when`, `seats`, `price`, `car`, `u_id`)
        VALUES ('" . $pFrom . "', '" . $pTo . "', '" . $pWhen . "', 
        '" . $pSeats . "', '" . $pPrice . "', '" . $pCar . "', '" . $result . "');";


        $query = mysql_query($sql) or trigger_error("Query Failed: " . mysql_error());

        if ($query) {
            return TRUE;
        } 
    }
    return FALSE;
}

...?>

searchRide.php - checks if the variables which are dedicated to get the search filter values have any values; (in the else statement) if there are no values, the form wasn't submitted and displays the searchRide form and after result passes the variables for the searchRide.php ( $_SERVER['PHP_SELF'] )

<?php
require_once 'config.php';

$sOutput .= '<div id="searchRide-body">';

if (isset($_GET['action'])) {
    switch (strtolower($_GET['action'])) {
        case 'searchride':
        if (isset($_POST['when_min']) && isset($_POST['when_max']) && isset($_POST['from']) && isset($_POST['to'])) {
            if (searchRide($_POST['when_min'], $_POST['when_max'], $_POST['from'], $_POST['to'])) {
                while($row = mysql_fetch_array($query2)){
                    $sOutput .= "' ID: '" .$row['id'] . "' <br />
                    When: '" . $row['when'] . "' <br />
                    From: '" . $row['from'] . "' <br />
                    To: '" . $row['to'] . "' <br />
                    Seats left: '" . $row['seats'];

                }
            }
        }
    }
}else{
    if (isset($_SESSION['error'])) {
        $sError = '<span id="error">' . $_SESSION['error'] . '</span><br />';
    }

    $sOutput .= '<h2>Search for rides</h2>
        ' . $sError . '
        <form name="searchride" method="post" action="' . $_SERVER['PHP_SELF'] . '?action=searchride">
            From: <input type="text" name="from" value=* /><br />
            To: <input type="text" name="to" value=* />
            When_min: <input type="text" name="when_min" value=* />
            When_max: <input type="text" name="when_max" value=* />
            <br /><br />
            <input type="submit" name="submit" value="Search" />
        </form>
        <br />
        <h4>Would you like to <a href="index.php">Go back?</a></h4>';
}   
echo $sOutput . "<br />";

echo "TEST string" . "<br />";
echo $query2 . " query2<br /> ";
echo $sql2 . " sql2<br />";
echo $row . "<br />";
?>

At the end of this code You can see some printed variables, which are used to check their values after searRide form is submitted. I updated my database with the following data and checked with phpMyAdmin for the exact values so I can test the search with existing data:

From: TEST01 To: TEST02 When: 500 Seats: 5 Price: 7 Car: volvo

Test data submitted with the searchRide form: From: TEST01 To: Test02 When_min: 1 Whn_max: 3000

After is press Search button on the searchRide form these are the following results (what the browser shows):

(sOutput variable

TEST WRITE TEXT

Resource id #5 (query2 variable

SELECT * FROM ride WHERE from ='TEST01' AND to = 'TEST02' AND when >= '1' AND when <= '5000' (sql2 variable

(row variable

After this I inserted the SQL query in the phpMyAdmin SQL command line and resulted the data I was searching for.

Was trying many times to figure out what could be the problem, with my own knowledge and varius searches on google, php.net and w3chools.com. My guess is that i'm using the MYSQL_FETCH_ARRAY wrong.

following condition will not work

if (searchRide($_POST['when_min'], $_POST['when_max'], $_POST['from'], $_POST['to'])) {

as you have not return any value from searchRide function you need to return true to go into the condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM