PHP - 将db值传递给$ _POST - 值错误

时间:2010-09-12 06:16:05

标签: php

我的代码出现问题 - 当我运行它时,它似乎只传递最后一条记录的ID信息而不是我想要列出的行中的那条信息,并且有一个按钮。该页面的代码如下(对不起,如果有那些堆 - 我只是很新):

<?php require_once('Connections/Demand.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_Recordset1 = "-1";
if (isset($_COOKIE['DRLogin'])) {
  $colname_Recordset1 = $_COOKIE['DRLogin'];
}
mysql_select_db($database_Demand, $Demand);
$query_Recordset1 = sprintf("SELECT Title, `Action`, Type, Information, `Date`, Status, ID FROM username WHERE Username = %s AND Active = '1' ORDER BY `Date` ASC", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $Demand) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$row=0; 
$numrows=mysql_num_rows($row_Recordset1);

$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login - Demand &amp; Resolve</title>
<script type="text/javascript">
    function ChangeColor(tableRow, highLight)
    {
    if (highLight)
    {
      tableRow.style.backgroundColor = '#dcfac9';
    }
    else
    {
      tableRow.style.backgroundColor = 'white';
    }
  }

  function DoNav(theUrl)
  {
  document.location.href = theUrl;
  }
  </script>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>
    <input name="hiddenField"  type = "hidden" id="GUsername" value="<?php echo $_COOKIE['DRLogin'] ?>" />
  To Do List</p>
  <p>&nbsp;</p>
</form>
<table border="1">
  <tr>
    <td>Title</td>
    <td>Action</td>
    <td>Type</td>
    <td>Information</td>
    <td>Date</td>
    <td>Status</td>
    <td>Task ID</td>
  </tr>
  <?php do { ?>
    <tr onmouseover="ChangeColor(this, true); " 
              onmouseout="ChangeColor(this, false);" 
              >
      <td><?php echo $row_Recordset1['Title']; ?></td>
      <td><?php echo $row_Recordset1['Action']; ?></td>
      <td><?php echo $row_Recordset1['Type']; ?></td>
      <td><?php echo $row_Recordset1['Information']; ?></td>
      <td><?php echo $row_Recordset1['Date']; ?></td>
      <td><?php echo $row_Recordset1['Status']; ?></td>
      <td><form method="post" action="full.php"><input type="hidden" name="pull0" value="<?php echo $row_Recordset1['ID']; ?>"/><input type="submit" name="Action" id="Submit" value="Action" /></td>


    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));  ?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

在full.php上,我使用$ _POST操作来获取值。

2 个答案:

答案 0 :(得分:0)

您必须关闭表单:

<td>
    <form method="post" action="full.php">
        <input type="hidden" name="pull0" value="<?php echo $row_Recordset1['ID']; ?>"/>
        <input type="submit" name="Action" id="Submit" value="Action" />
    </form> <!-- add this line -->
</td>

除此之外:使用while循环而不是do-while循环。您的代码有点“hackish”,因为当您使用do_while循环时,$row_Recordset1未在第一次迭代中定义。您可以通过在文件开头单击mysql_fetch_assoc来解决此问题。

while循环将更容易理解,并确保在每次迭代中定义$row_Recordset1

<?php while(($row_Recordset1 = mysql_fetch_assoc($Recordset1))): ?>
    <tr onmouseover="ChangeColor(this, true); " 
              onmouseout="ChangeColor(this, false);" 
              >
      <td><?php echo $row_Recordset1['Title']; ?></td>
      <td><?php echo $row_Recordset1['Action']; ?></td>
      <td><?php echo $row_Recordset1['Type']; ?></td>
      <td><?php echo $row_Recordset1['Information']; ?></td>
      <td><?php echo $row_Recordset1['Date']; ?></td>
      <td><?php echo $row_Recordset1['Status']; ?></td>
      <td><form method="post" action="full.php"><input type="hidden" name="pull0" value="<?php echo $row_Recordset1['ID']; ?>"/><input type="submit" name="Action" id="Submit" value="Action" /></form></td>   
    </tr>
<?php endwhile; ?>

请注意,我在这里使用alternative syntax for control structures,我个人觉得在处理HTML时更容易阅读和使用。另请注意,使用while循环时,您应该在文件开头调用mysql_fetch_assoc,否则您会跳过第一条记录。

在{em>一行行上应用mysql_num_rows对我没有意义;)

答案 1 :(得分:0)

函数GetSQLValueString应该是这样的

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  switch ($theType) {
    case "text":
      if (function_exists("mysql_real_escape_string")) {
        $theValue = mysql_real_escape_string($theValue);
      } else {
        $theValue = mysql_escape_string($theValue);
      }
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
  }
  return $theValue;
}
虽然我说甚至这个都是矫枉过正的 而theType概念完全错误。为什么你认为定义的值也不应该被转义?