1

我无法获得正确的语法。我正在尝试获取$nproj_hours通过表单提交的hours_id值,以在小时表中查找与其关联的键值,并将该数值放在汇总表中的新行中(我将这样做部门和项目也是如此,所以如果有办法将它们全部打包成一个,我也有一个项目和部门表)。

最终代码:

if (isset($_POST['btnnew']))  

    {
    echo "<pre>Value of \$_POST:</br>";print_r($_POST);echo"</pre>";

        $nclarity_id    = $_POST['nclarity_id'];
        $nproj_hours    = $_POST['nproj_hours'];
        $ndept_name     = $_POST['ndept_name'];
        $proj_id        = $_POST['nclarity_id'];
        $hours_id       = $_POST['nproj_hours'];
        $dept_id        = $_POST['ndept_name'];

        $sql        = "INSERT INTO `summary` VALUES (null,'$proj_id','$hours_id','$dept_id',null)" 
                            or die ("couldn't update".mysql_error());
        $query = mysql_query($sql);

//echo "<pre>Value of \$sql:</br>";print_r($sql);echo"</pre>";
        if ($query)
{
echo "success!";
}
else
            {
        die('error inserting new record'.mysql_error());
    } // end of the nested if statement
}

?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
   <tr>
   <td align="center">&nbsp;</td>
   <td align="center"><strong>Clarity ID</strong></td>
   <td align="center"><strong>Hours</strong></td>
   <td align="center"><strong>Department</strong></td>
   </tr>
<form name="form1" method="post" action="stupid.php">
<tr>
<td>&nbsp;</td>
<select name="nclarity_id">

<?php
$sql = "SELECT proj_id, clarity_id FROM projects " . "ORDER by clarity_id";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['proj_id']."\">".$row['clarity_id']."</option>\n  ";
}
?>

</select>
<select name="nproj_hours">
<?php
$sql = "SELECT hours_id, proj_hours FROM hours";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['hours_id']."\">".$row['proj_hours']."</option>\n  ";
}
?>
</select>

<select name="ndept_name">

<?php
$sql = "SELECT dept_id, dept_name FROM departments";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['dept_id']."\">".$row['dept_name']."</option>\n  ";
}
?>
</select>
   <input type="submit" name="btnnew" value="Enter New Record">
</tr>
</table>

<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="500" border="1" cellspacing="0" cellpadding="3">
</tr>


<?php
while($rows=mysql_fetch_array($res))
{

?>

<?php
}

?>

</table>
</td>
</tr>
</table>


<?php
mysql_close();
?>
4

1 回答 1

1

您必须更改查询 - 特别是删除关键字VALUES 并添加列名。有关确切的语法和详细信息,请参阅规范。我刚刚输入colname1, colname2, colname3, colname4- 您需要用汇总表中的实际列名替换它

INSERT INTO summary (colname1, colname2, colname3, colname4) 
   SELECT NULL, NULL, proj_hours, NULL
   FROM hours 
   WHERE proj_id ='$nproj_hours

像往常一样,由于 SQL 注入的可能性而避免使用此类查询的免责声明是适用的。如果您不知道,请谷歌sql injection / prepared statements / parameterized queries等。

于 2012-10-06T13:58:03.233 回答