User Input Based Display Form Field

I show a rowset for each car that I have in my database. Each line has a form field where a registered user can send an offer. When the user has made an offer for any car, the form field is replaced by text that displays the cost of the submitted proposal.

What I experience, however, is a less than ideal result. If I make a suggestion for one line, fine, the logic works. If I make another suggestion for another line, then the logic will work, except for the fact that the previous line now displays the form again.

I can provide more detailed information if necessary, but maybe someone is already familiar with this.

Thanks in advance.

<?php require("db-connect.php"); $display = "SELECT filename, car_id, make, model, year, mileage, vin, description, GROUP_CONCAT(filename) FROM scraplis_cars LEFT JOIN scraplis_images USING (car_id) GROUP BY car_id ORDER BY date_time DESC"; $dResult = mysql_query($display) or die('error:' . mysql_error()); $offer = "SELECT car_id, user_id, offer_id, value FROM scraplis_offers WHERE user_id = '".$_SESSION['user_id']."'"; $oResult = mysql_query($offer) or die('Error ' . mysql_error()); $oRow = mysql_fetch_array($oResult); if(!isset($_SESSION['access'])){ header("location:index.php"); } ?> <?php if($dResult): ?> <table class="post"> <thead> <tr> <?php if(isset($_SESSION['email']) && $_SESSION['access'] == 0) : ?> <th scope="col">Images</th> <th scope="col">Make</th> <th scope="col">Model</th> <th scope="col">Year</th> <th scope="col">Mileage</th> <th scope="col">VIN #</th> <th scope="col">Description</th> <th scope="col">Offer</th> </tr> </thead> <tbody> <?php while($dRow = mysql_fetch_array($dResult)) : ?> <?php $str = $dRow[8]; ?> <?php $images = explode(',', $str); ?> <tr> <td> <ul> <?php if(!empty($str)) : ?> <?php foreach($images as $value) :?> <li> <a href="images/<?php echo $value; ?>" rel="lightbox[<?php echo $row['car_id']; ?>]"> <img src="images/<?php echo $value; ?>"/> </a> </li> <?php endforeach; ?> <?php endif; ?> <ul> </td> <td><?php echo $dRow['make']; ?></td> <td><?php echo $dRow['model']; ?></td> <td><?php echo $dRow['year']; ?></td> <td><?php echo number_format($dRow['mileage']); ?></td> <td><?php echo $dRow['vin']; ?></td> <td><span><?php echo $dRow['description']; ?></span></td> <td> <?php if($oRow['car_id'] == $dRow['car_id']) : ?> Offer pending approval - $<?php echo $oRow['value']; ?> <?php else : ?> <form id="offer" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <input type="text" id="price" name="offer" /> <input type="hidden" name="submitted" value="<?php echo $dRow['car_id']; ?>" /> <input type="submit" name="price" value="Submit" /> </form> <?php endif; ?> </td> </tr> <?php endwhile; ?> <?php else : ?> <th scope="col">Delete</th> <th scope="col">Images</th> <th scope="col">Make</th> <th scope="col">Model</th> <th scope="col">Year</th> <th scope="col">Mileage</th> <th scope="col">VIN #</th> <th scope="col">Description</th> </tr> </thead> <tbody> <?php while($dRow = mysql_fetch_array($dResult)) : ?> <?php $str = $dRow[8]; ?> <?php $images = explode(',', $str); ?> <tr> <td> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <input type="checkbox" name="record" value="<?php echo $row['car_id']; ?>" /> <input type="submit" name="delete-car" value="Delete" /> </form> </td> <td> <ul> <?php if(!empty($str)) : ?> <?php foreach($images as $value) :?> <li> <a href="images/<?php echo $value; ?>" rel="lightbox[<?php echo $row['car_id']; ?>]"> <img src="images/<?php echo $value; ?>"/> </a> </li> <?php endforeach; ?> <?php endif; ?> </ul> </td> <td><?php echo $dRow['make']; ?></td> <td><?php echo $dRow['model']; ?></td> <td><?php echo $dRow['year']; ?></td> <td><?php echo number_format($dRow['mileage']); ?></td> <td><?php echo $dRow['vin']; ?></td> <td><span><?php echo $dRow['description']; ?></span></td> </tr> <?php endwhile; ?> <?php endif; ?> </tbody> </table> <?php endif; ?> 
+4
source share
1 answer

One important important thing for safety:

SEARCH:

 if(!isset($_SESSION['access'])){ header("location:index.php"); } 

REPLACE WITH:

 if(!isset($_SESSION['access'])) { header("Location: index.php"); exit; } 

Take a look at the PHP documentation for header() or exit() - as described here (or a security issue) exit() here.

To your question:

You only have the first line of $oResult in $oRow - so you have (for example) 1000 cars, but only one sentence. You need to get the results of $oResult in a loop ( while() , for() , ... is what you prefer ...), and then check if you can find car_id (inside $dRow also in sentences).

sample code (very easy to understand):

 <?php // ... // get the offers // info: user_id would not be necessary here ;-) $offer = "SELECT car_id, user_id, offer_id, value FROM scraplis_offers WHERE user_id = '".$_SESSION['user_id']."'"; $oResult = mysql_query($offer) or die('Error ' . mysql_error()); $oRows = array(); while($oRow = mysql_fetch_array($oResult)) { $oRows[$oRow['car_id']] = array( 'offer_id' => $oRow['offer_id'], 'value' => $oRow['value'] ); } // looping the through the cars // just the while()-loop based on your code while($dRow = mysql_fetch_array($dResult)) { // check if offer exists if(array_key_exists($dRow['car_id'], $oRows)) { // HAVE an offer for that car ;-) - show offer details } else { // HAVENO offer that car - show form } } // ... ?> 

I hope I was not mistaken, made no mistakes (you need to get up early), and this helps you; -).

+1
source

All Articles