从mysql检索数据并以表格形式显示 [英] retieve data from mysql and display in form

查看:57
本文介绍了从mysql检索数据并以表格形式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单选框和 2 个下拉菜单,提交时会保存到 mysql.单选框是是或否,2 个下拉菜单是在 html 中创建的.它目前正在全面工作并保存所有数据.

I have a radio box and 2 drop down menus which when submitted save to mysql. The radio box is either yes or no and the 2 dropdowns where created in html. It is currently fully working and saves all the data.

我现在想要做的是当用户重新登录时,它会显示他们之前选择的内容(如果有的话).

What I now wish to do is when a user logs back in, it will show what they have previously selected (if they have).

PHP 脚本:

<?php
session_start(); 
require_once("config.php"); 
if(!isset($_SESSION['username'])){
   header('Location: login.php'); 
   exit; 
}else{
    $sql = "SELECT attendance1 FROM user WHERE username = '".mysql_real_escape_string($_SESSION['username'])."'";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res);
    if(($row[0] == "Yes") || ($row[0] == "No")){
        header("Location: errorsubmit.html");
        exit;
    }
}
if(isset($_POST['submit'])){         
     $sql = "UPDATE user SET attendance1 = '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" .  mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());  

   $sql = "UPDATE user SET colour1= '" . mysql_real_escape_string($_POST['colour1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());  

   $sql = "UPDATE user SET shade1= '" . mysql_real_escape_string($_POST['shade1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());  

   header("Location: thanks.html", true, 303);
}
?>

表格:

<form>
         <input name="attendance1" type="radio" id="Yes" value="Yes" checked="checked"/>Yes
         <br />
<input name="attendance1" type="radio" id="No" value="No" />No
       </h3></td>
       <td>
              <select name="colour1" id="colour1" >
           <option selected="selected">Please Select</option>
           <option>Red</option>
           <option>White</option>
           <option>Green</option>
         </select>
       </td>
       <td><h3>
                 <select name="shade1" id="shade1" >
           <option selected="selected">Please Select</option>
           <option>light</option>
           <option>heavy</option>
         </select>
       <td>&nbsp;</td>
       <td><label>
         <input type="submit" name="submit" id="button" value="Submit" />
       </label></td>
     </tr>
   </table>

推荐答案

试试下面的:

您需要从数据库中获取值并将它们与选择框值匹配以显示它们被选中.

you need to fetch values from database and match them with select box values to show them selected.

<select name="shade1" id="shade1" >
  <option>Please Select</option>
  <option value="light" <?php if($val=='light') echo 'selected'; ?>>light</option>
  <option value="heavy"<?php if($val=='heavy') echo 'selected'; ?>>heavy</option>
</select>

这里的 $val 是从数据库中检索到的值的变量.

Here $val is variable having value retrieved from database.

添加时你应该有:

<option value="light" >light</option>
<option value="heavy">heavy</option>

这篇关于从mysql检索数据并以表格形式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆