从html表向mysql中插入数据

问题描述

| 我对此是新手,但要掌握它,但我需要帮助。 我目前有一个名为Newkpi.PHPPHP文件,该文件可在数据库表StaffList中获取我想要的所有用户,然后将其放入可以输入数据的表中。然后我按下按钮,数据被发送到另一个表,称为服务。我发现的是它在表中创建记录,但它没有插入任何数据,而只是将其放入空白记录中,我的代码如下
<?PHP 
include \'dbc.PHP\';

page_protect();

if(!checkAdmin()) {
header(\"Location: myaccount.PHP\");
exit();
}
company();
$page_limit = 10; 
$stafflist = MysqL_query(\"SELECT * FROM StaffList WHERE full_name != \'Adam Carter\' 
AND      full_name != \'Jakata\' AND branch = \'$_SESSION[branch]\' \");

$host  = $_SERVER[\'HTTP_HOST\'];
$host_upper = strtoupper($host);
$login_path = @ereg_replace(\'admin\',\'\',dirname($_SERVER[\'PHP_SELF\']));
$path   = rtrim($login_path,\'/\\\\\');

// filter GET values
foreach($_GET as $key => $value) {
    $get[$key] = filter($value);
}

foreach($_POST as $key => $value) {
    $post[$key] = filter($value);
}



$rs_all = MysqL_query(\"select count(*) as total_all from StaffList\") or die(MysqL_error());
$rs_active = MysqL_query(\"select count(*) as total_active from StaffList where approved=\'1\'\") or die(MysqL_error());
$rs_total_pending = MysqL_query(\"select count(*) as tot from StaffList where approved=\'0\'\");                           

list($total_pending) = MysqL_fetch_row($rs_total_pending);
list($all) = MysqL_fetch_row($rs_all);
list($active) = MysqL_fetch_row($rs_active);


?>
<html>
<head>
<title>Administration Main Page</title>
<Meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<link href=\"styles.css\" rel=\"stylesheet\" type=\"text/css\">
<script language=\"JavaScript\" type=\"text/javascript\" src=\"js/jquery-1.3.2.min.js\"></script>

</head>

<body>
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  <tr><br>
  </tr>
  <tr>
    <td width=\"14%\" valign=\"top\"><?PHP
    if (isset($_SESSION[\'user_id\'])) {?>
<div class=\"myaccount\">
  <p><strong>Administration</strong></p>
  <a href=\"createuser.PHP\">Create New User</a><br>
  <a href=\"UserEdit.PHP\">Edit User</a><br>
</div>


<?PHP }
if (checkAdmin()) {
/*******************************END**************************/
?>

      <?PHP } ?>
    </td>
    <td width=\"74%\" valign=\"top\" style=\"padding: 10px;\">

      <p><?PHP 

      ?></p>

      <p>

      &nbsp;</p>
      <?PHP
      if($_POST[\'doSubmit\'] == \'Create\')
{


MysqL_query(\"INSERT INTO Services (Staffname,ServiceAmount,ServiceDate,ForecastForNextMonth,ProductSales,ClientsThisMonth,personnelRetension,totalRetension,Colours,Cuts,PreBooking,TimeUsed)
             VALUES (\'$post[user_name]\',\'$post[Serviceamount]\',\'$post[servicedate]\',\'$post[forecast]\',\'$post[productsales]\',\'$post[Clientsthismonth]\',\'$post[Personelret]\',\'$post[Totalret]\',\'$post[colours]\',\'$post[cuts]\',\'$post[prebooking]\',\'$post[timeused]\')
             \") or die(MysqL_error()); 


echo \"<div class=\\\"msg\\\">New KPI\'s Entered</div>\"; 
}

      ?>

      <h3 class=\"titlehdr\"><font color=\"#000050\">Create New User</font></h3>
      <table width=\"300px\" border=\"0\" align=\"Centre\" cellpadding=\"2\" cellspacing=\"0\">
          <tr bgcolor=\"#000050\">
              <form name=\"form1\" method=\"post\" action=\"Newkpi.PHP\">


            <td width=\"20px\"><h3 class=\"Text2\">Staff Member</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Service Amount</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Service Date</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Forecast For Next Month</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Product Sales</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Clients This Month</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Personel Retension</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Total Retension</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Colours</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Cuts</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Pre-Booking</h3></td>
            <td width=\"20px\"><h3 class=\"Text2\">Time Used</h3></td>

                   </tr>

          <?PHP while ($rrows = MysqL_fetch_array($stafflist)) {?>
          <tr> 

           <td name=\"user_name\"><h3 class=\"Text3\"><?PHP echo $rrows[\'full_name\'];?></h3></td>
           <td><input name=\"Serviceamount\" type=\"text\" size=\"4\" id=\"Serviceamount\"></td>
           <td><h3 class=\"Text3\"><input name=\"servicedate\" type=\"text\" size=\"4\" id=\"servicedate\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"forecast\" type=\"text\" size=\"4\" id=\"forecast\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"productsales\" type=\"text\" size=\"4\" id=\"productsales\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"Clientsthismonth\" type=\"text\" size=\"4\" id=\"Clientsthismonth\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"Personelret\" type=\"text\" size=\"4\" id=\"Personelret\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"Totalret\" type=\"text\" size=\"4\" id=\"Totalret\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"colours\" type=\"text\" size=\"4\" id=\"colours\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"cuts\" type=\"text\" size=\"4\" id=\"cuts\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"prebooking\" type=\"text\" size=\"4\" id=\"prebooking\"></h3></td>
           <td><h3 class=\"Text3\"><input name=\"timeused\" type=\"text\" size=\"4\" id=\"timeused\"></h3></td>

          </tr>

          <?PHP } ?>
           </form>
        </table>




                <input name=\"doSubmit\" type=\"submit\" id=\"doSubmit\" value=\"Create\">



      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    <td width=\"12%\">&nbsp;</td>
  </tr>
</table>
</body>
</html>
希望对此有所帮助     

解决方法

        想必您要使用
$_POST
而不是$ post吗? 另外,您可能要考虑在表上添加一些约束,假设所有不为null都不是所需的功能。 一旦工作完成,我将对来自SQL注入的安全性问题进行一些研究,并检查传入的数据,并假设这些数据不是在安全的Intranet上运行的。     ,        您的问题出在语法上。 在SQL插入查询中将$ post的所有实例更改为$ _POST。我认为您还应该为每个$ _POST创建变量,因为如果不对引号进行转义,则会遇到语法问题。 这是一个简单的示例:
 $field1 = $_POST[\'field1\'];
 $field2 = $_POST[\'field2\'];
 $field3 = $_POST[\'field3\'];

 mysql_query(\"INSERT INTO table (field1,field2,field3) VALUES (\'$field1\',\'$field2\',\'$field3\')\");
    ,        
mysql_query(\"INSERT INTO Services (Staffname,ServiceAmount,ServiceDate,ForecastForNextMonth,ProductSales,ClientsThisMonth,personnelRetension,totalRetension,Colours,Cuts,PreBooking,TimeUsed)
             VALUES (\'$post[user_name]\',\'$post[Serviceamount]\',\'$post[servicedate]\',\'$post[forecast]\',\'$post[productsales]\',\'$post[Clientsthismonth]\',\'$post[Personelret]\',\'$post[Totalret]\',\'$post[colours]\',\'$post[cuts]\',\'$post[prebooking]\',\'$post[timeused]\')
             \") or die(mysql_error()); 
$ post [用户名] 更改为$ _POST [\'user_name \']; 每个$ post []都一样 将$ _POST直接传递给sql不好,因为有人会进行sql注入。 最好先将它们分配给变量,然后在sql中传递变量