【问题标题】:Display Form Fields Based on User Input根据用户输入显示表单字段
【发布时间】:2010-12-07 15:02:53
【问题描述】:

我在这里为我数据库中的每辆车显示一组行。 每行都有一个表单字段,登录用户可以在其中提交报价。 当用户提出任何汽车的报价时,表单字段将替换为显示所提交报价值的文本。

但是,我遇到的结果并不理想。 如果我为一排报价,很好,逻辑有效。如果我继续为另一行提供另一个报价,则逻辑有效,除了上一行现在再次显示表单这一事实。

如有必要,我可以提供更多详细信息,但也许有人已经对此很熟悉了。

提前致谢。

<?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; ?>

【问题讨论】:

  • 为什么不离开加入您的报价表?您似乎无法控制为给定的行或循环带回哪个报价,只为给定的用户返回所有报价?在加入中,您可以获取该用户针对该车辆的最新报价。

标签: php mysql


【解决方案1】:

安全第一的重要

搜索:

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

替换为:

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

查看header()exit() 的PHP 文档 - 两者都描述了exit() 的需求(或安全问题)。

你的问题:

您在$oRow 中只有第一行$oResult - 所以您(例如)有1000 辆汽车,但只有一个报价。您需要在循环中获取$oResult 的结果(while()for(),... - 你喜欢什么...),然后检查是否可以找到car_id(在$dRow 内也在优惠)。

代码示例(非常容易理解):

<?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)) {
   // H A V E an offer for that car ;-) - show offer details
 } else {
   // H A V E N O offer that car - show form
 }
}
// ...
?>

我希望我没有误会你,没有犯错(需要早起),这对你有帮助 ;-)。

【讨论】:

  • 谢谢,您的解决方案正是我所需要的。我还有一个问题,它与打印出多维数组的值有关。在 if 语句之后,我想显示报价详细信息,其中包括在表单字段中输入的金额。我该怎么做呢?这就是我要打印的内容 $oRows[$oRow['car_id']]['value'] (至少我是这样认为的),但我收到一个错误,说索引不存在。有什么想法吗?
  • 尝试调试数组$oRows -> 在while($oRow = mysql_fetch_array($oResult)) ... 之后生成print_r($oRows); 并在此处打印结果;-)。
猜你喜欢
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多