首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测量php mysql计数器

测量php mysql计数器
EN

Stack Overflow用户
提问于 2010-11-30 16:48:21
回答 2查看 509关注 0票数 0

我正在做一个小的php/mysql调查。我的所有问题/答案/猫狗都存储在mysql数据库中。我还将我的类别存储在我的php脚本中的数组中。类别array=(“人”、“妈妈”、“学生”、“某人”等)每个类别有4个问题和4个答案。有8类。用户通过从类别1到下一个类别等来回答调查.

下面是我硬编码了类别2的sql查询,这样我就可以显示与类别2相关联的4个问题:从类别a中选择a,b.,在其中a.id = b.id和a.id='2‘。

我的问题是,我想让数字"2“动态。dynamic这里意味着,当用户单击"NEXT“按钮时,我希望新的sql查询如下所示:

从a类中选择a.,b.,问题b,其中a.id = b.id和a.id='3‘

单击“下一步”按钮再次转到:从类别a中选择a、B、问题b,其中a.id = b.id和a.id='4‘等.

下面是一个代码片段:

代码语言:javascript
复制
 <?php
    $categories = array ("Person","A Mom","A Student","Somebody");

    $sql="select a.*, b.* from category a, questions b where a.id = b.id and a.id='2'";
    $result=mysql_query($sql,$db_connection);
    if (!$result)
    {
   die('Error: ' . mysql_error());
    }    
 ?>
  <tr>
    <td height="32" colspan="5" bgcolor="#ECA4DD"><b><?php echo $result['name'];?></b></td></tr>
  <tr>
  <tr>
    <td colspan="5" class="content_2"><?php echo $result['description'];?></td>
  </tr>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

     <?php
    while ($row = mysql_fetch_assoc($result)) 
    {

 ?>

   <tr>
    <td class="content_2"><?php echo "Question1"; ?></td>
    <td align="center" valign="middle"><input type="radio" name="q1" id="s1" value="<?php echo $row['q1']; ?>"/> Rare </td>
    <td align="center" valign="middle"><input type="radio" name="q1" id="s1" value="<?php echo $row['q1']; ?>"/> Sometimes</td>
    <td align="center" valign="middle"><input type="radio" name="q1" id="s1" value="<?php echo $row['q1']; ?>"/> True</td>
    <td align="center" valign="middle"><input type="radio" name="q1" id="s1" value="<?php echo $row['q1']; ?>"/> Always</td>
  </tr>
  <tr>
       <td height="108"></td>
       <td align="center" valign="middle">&nbsp;</td>
       <td align="center" valign="middle">&nbsp;</td>
       <td colspan="2" align="right" valign="middle"><input type="submit" name="save" value="NextButton"/>
       </td>
      </tr>

   <?php
   mysql_free_result($result);
   }
  ?>      

 </form>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-30 17:22:52

在您的表单中,您需要指示当前正在执行的步骤或下一步的步骤。这样你就可以增加它来移动你的表单。

因此,在您的表单中,放入一个隐藏变量,如下所示:

代码语言:javascript
复制
<input type='hidden' name='thisCategoryID' value='2'>

然后,在PHP代码中,您可以这样做:

代码语言:javascript
复制
// If there was a category ID posted, set some variables and increment it to load the next section
if(isset($_POST['thisCategoryID]) && is_numeric($_POST['thisCategoryID']) {
    $currentCategoryID = $_POST['thisCategoryID'];
    $nextCategoryID = $currentCategoryID + 1;
} else {
    // There's something wrong with your input, show an appropriate error
}

// Now run your query with the updated Category ID
$sql="select a.*, b.* from category a, questions b where a.id = b.id and a.id='{$nextCategoryID}'";
$result=mysql_query($sql,$db_connection);
if (!$result) {
    die('Error: '.mysql_error());   
}

在上面的查询中,为了防止SQL注入,我本可以做得更多,在本例中,我只是确保您有一个数字值。

票数 0
EN

Stack Overflow用户

发布于 2010-11-30 17:04:50

您可以通过$_GET变量或类似性质的内容传递当前的问号。因此,在页面的开头,抓取当前的问题:

代码语言:javascript
复制
$current_question = isset($_GET['current_question']) ? $_GET['current_question'] : '1';

然后在查询中可以运行以下内容:

代码语言:javascript
复制
$sql = "SELECT `a`.*, `b`.* FROM `category` AS `a`, `questions` AS `b` WHERE `a`.`id` = `b`.`id` AND `a`.`id` = '{$current_question}'";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4316216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档