【发布时间】:2015-02-25 08:23:53
【问题描述】:
<?php
$sql = mysql_query("SELECT * FROM testimonial ORDER BY id DESC");
//This block grabs the whole list for viewing
while($row = mysql_fetch_array($sql)){
$id = $row['id'];
$name = $row['name'];
$comment = $row['comment'];
$visibility = $row['visibility'];
$date_added = strftime("%b %d, %Y", strtotime($row['date_added']));}
This section is for buttons if i press the button named update it should update the field named "visibility" to approved and if i press the button named update1 it should update the field named "visibility" to hide
if(isset($_POST['update']))
{
$a = addslashes(strip_tags($_POST['update']));
$selected = mysql_query("SELECT * FROM `testimonial` WHERE id = '$id'");
$info = mysql_fetch_assoc($selected);
$sql = mysql_query("UPDATE `testimonial` SET `visibility` = 'approved' WHERE id = '$id'");
}
if(isset($_POST['update1']))
{
$a = addslashes(strip_tags($_POST['update1']));
$selected = mysql_query("SELECT * FROM `testimonial` WHERE id = '$id'");
$info = mysql_fetch_assoc($selected);
$sql = mysql_query("UPDATE `testimonial` SET `visibility` = 'hide' WHERE name='$name'");
}
这一行是我开始循环的表的标题
$sql = mysql_query("SELECT * FROM testimonial");
echo '<form onSubmit="return validateForm()" action="testimonials.php" enctype="multipart/form-data" method="post">
<div align="center">
<table width="1273" border="1" cellspacing="0" cellpadding="6">
<tr>
<th width="119" bgcolor="#333333" scope="col"><strong>ID</strong></th>
<th width="88" bgcolor="#333333" scope="col"><strong>User</strong></th>
<th width="629" bgcolor="#333333" scope="col"><div align="center"><strong>Content</strong></div></th>
<th width="107" bgcolor="#333333" scope="col"><strong>Status</strong></th>
<th width="111" bgcolor="#333333" scope="col"><strong>Date Added</strong></th>
<th width="111" bgcolor="#333333" scope="col"><strong>Action</strong></th>
<th width="111" bgcolor="#333333" scope="col"><strong>Action</strong></th>
</tr>';
此条件是如果可见性等于已批准,则将表字段名称“可见性”更新为已发布,如果按下其他按钮,则应将表字段“可见性”更新为未发布
while($row = mysql_fetch_array($sql)){
echo'<tr>';
echo'<th width="119" scope="col">'.$row['id'].'</th>';
echo'<th width="88" scope="col">'.$row['name'].'</th>';
echo'<th width="629" scope="col"><div align="center">'.$row['comment'].'</div></th>';
echo'<th width="107" scope="col">'.$row['visibility'].'</th>';
echo'<th width="111" scope="col">'.$row['date_added'].'</th>';
$activ= $row['visibility'];
if ($activ=='approved'){ $visibility="Posted";} if ($activ=='hide'){$visibility="Not Posted";}
echo '<th width="107" scope="col">'.$visibility.'</th>';
echo'<th width="111" scope="col"><button type="submit" name="update" class="button" value='.$row['id'].'>Post</button><button type="submit" name="update1" class="button" value='.$row['id'].'>Unpost</button></th>';
echo'</tr>';
}
?>
【问题讨论】:
标签: php mysql forms button while-loop