0

i cant retrieve data from database table .please help me) there's too many error. cant fetch any data from database

&nbsp<input type="checkbox" name="model" value="Jetta" />Jetta
<input type="checkbox" name="model" value="Laguna" />Laguna
<input type="checkbox" name="model" value="Focus" />Focus
<input type="checkbox" name="model" value="Tico" />Tico
<input type="checkbox" name="model" value="Avensis" />Avensis

<input type="submit" name="submit" value="Register" /> </div>   
<input type="hidden" name="submitted" value="TRUE"/>
</fieldset>

file=(test2.php)

<?      
  mysql_connect("localhost","root","");
        mysql_select_db("carforsale");

        $sql =mysql_query("SELECT * FROM carforsale");  
        $row=mysql_fetch_assoc($sql);       
        $manufacturerName = 'manufacturerName';
        $modelName = 'modelName';
        $acquisitionPrice = 'acquisitionPrice';

$model = $_POST['model'];
if ($model=="Jetta") {

include('connect.php');
$result = mysql_query("manufacturerName,modelName,acquisitionPrice FROM  
carforsale WHERE modelName='Jetta'");
while($row = mysql_fetch_array($result))
{
echo '<td>'. $row['manufacturerName'].' </td>';
echo '<td>'.$row['modelName'].'</td>';
echo '<td>'.$row['acquisitionPrice'].'</td>';
echo '</tr>';
}
elseif ($model=="Laguna")

$result = mysql_query("manufacturerName,modelName,acquisitionPrice FROM  
carforsale WHERE modelName='Laguna'");
while($row = mysql_fetch_array($result))        {
echo '<td style="border-left: 1px solid #eacae7;">'. 
$row['manufacturerName'].'</td>';
echo '<td>'.$row['modelName'].'</td>';
echo '<td>'.$row['acquisitionPrice'].'</td>'; 
echo '</tr>';
}
}

?>

*i cant retrieve data from database table .please help me there's too many error ;'(

shby
  • 1
  • 4
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Mar 28 '15 at 03:19
  • Please extract *only* the relevant parts from your WoT (wall of text). It's confusing for everyone. Please – CSᵠ Mar 29 '15 at 08:04
  • as you can see.. i am confuse too... :'( – shby Mar 29 '15 at 08:48

2 Answers2

1

You have the condition

if ($model=="Jetta") {...

But $model is not being assigned anywhere

Change it to something like

$model = $_POST['model'];
if ($model=="Jetta") {

Since that wasn't assigned, the condition was false, and you don't have anything in the else at the bottom for it to do, so it basically does nothing

cwurtz
  • 3,177
  • 1
  • 15
  • 15
1

Your code is to scatterd, try to refactor your code in more logical block. Also there is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.

A more finetuned version of what you want to achieve

connect.php

<?php
    $host = '127.0.0.1';
    $user = 'root';
   $pass = '';
   $database = 'blub';

    try { 
        $pdo = new PDO('mysql:host=' . $host . ';dbname=' . $database, $user, $pass);
    }catch(PDOException $e) {
        echo 'Could not connect to database: ' . $e->getMessage();
        exit;
    }

cars.php

<?php
    include('connect.php'); //only connect once

    $sql = 'SELECT * FROM carforsale'; //default query
    $parms = array();
    $parameters = '';
    
    if (isset($_POST['model'])) {
        $sql .= ' WHERE modelName IN ({parameters})'; //adjust query when checkbox was selected
       foreach($_POST['model'] as $checkbox) {
        $parms[] = $checkbox;
        $parameters. = '?,';//forsee a placeholder for this checkbox
    }
        $parameters =      rtrim($parameters, ',');//remove last comma from ?,?,?, 
        $sql = str_replace('{parameters}', $parameters);//inject parameters when needed
    try {
        $stmt = $pdo->prepare($sql); //use PDO with prepared statements instead of mysql_query (see comment above)
        $stmt->execute($parms); //inject the model name in the parameter ? in the sql if needed
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }catch(PDOException $e) {
        echo 'Query failed: ' . $e->getMessage(); //always check SQL errors
        exit;
    }
    
    $table  = '<table border="1">';
    $table .= '<tr><th>Manufacturer</th><th>Model</th><th>Price</th></tr>';
    foreach($rows as $row) {
            $table .= '<tr>';
            $table .= '<td>'. $row['manufacturerName'].' </td><td>'.$row['modelName'].'</td><td>'.$row['acquisitionPrice'].'</td>';
            $table .= '</tr>';
    }
    $table .= '</table>';
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Cars</title>
    </head>
    <body>
        <form action="#" method="POST">
            <fieldset>
                <input type="checkbox" name="model[]" value="Jetta" />Jetta
                <input type="checkbox" name="model[]" value="Laguna" />Laguna
                <input type="checkbox" name="model[]" value="Focus" />Focus
                <input type="checkbox" name="model[]" value="Tico" />Tico
                <input type="checkbox" name="model[]" value="Avensis" />Avensis
                
                <input type="submit" name="submit" value="Register" /> </div>   
                <input type="hidden" name="submitted" value="TRUE"/>
            </fieldset>
        </form>
        <?= $table ?>
    </body>
</html>
Community
  • 1
  • 1
DarkBee
  • 16,592
  • 6
  • 46
  • 58