php – 基于用户输入显示表单域
发布时间:2020-12-13 22:29:26 所属栏目:PHP教程 来源:网络整理
导读:我在我的数据库中为每辆车显示一组行. 每行都有一个表单字段,登录用户可以在其中提交商品. 当用户为任何汽车提出要约时,表单字段将替换为显示提交的要约价值的文本. 然而,我所经历的并不是理想的结果. 如果我提出一行,很好,逻辑是有效的.如果我继续为另一行
|
我在我的数据库中为每辆车显示一组行.
每行都有一个表单字段,登录用户可以在其中提交商品. 当用户为任何汽车提出要约时,表单字段将替换为显示提交的要约价值的文本. 然而,我所经历的并不是理想的结果. 如果有必要,我可以提供更多细节,但也许有人已经熟悉了这一点. 提前致谢. <?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; ?>
解决方法
安全第一的重要事项是:
搜索: 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,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
}
}
// ...
?>
我希望我没有弄错你,没有犯错(需要早起)这对你有帮助;-). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
