在数据库表中插入值时会插入一些值,而有些值则不会

时间:2014-04-10 18:43:41

标签: php

这是我的PHP代码。当我输入值时,只有divisionname,divisionchief,month和periods值被插入到数据库表中。     

if (isset($_POST['submit'])) {

include('includes/connect.php');
for ($i=1; $i<=3; $i++)
{
$divisionname = $_POST['divisionname'];
$divisionchief= $_POST['divisionchief'];
$period = $_POST['period'];
$month = $_POST['month'];
$activityname = $_POST['activityname'.$i];
$unit = $_POST['unit'.$i];
$weightage = $_POST['weightage'.$i];
$per100 = $_POST['per100'.$i];
$per75 = $_POST['per75'.$i];
$per50 = $_POST['per50'.$i];
$perless50 = $_POST['perless50'.$i];


$sql="INSERT INTO workplan_detail(division_name, division_chief, Period, Month, activity_name, Unit, Weightage, per_100, per_75, per_50, per_less50) VALUES ('$divisionname', '$divisionchief', '$period', '$month', '$activityname', '$unit', '$weightage', '$per100', '$per75', '$per50', '$perless50')";
    mysql_query($sql);

    header("location:workplan.php");
    }`
    echo"This information has been added to the database."; 
}
?>

4 个答案:

答案 0 :(得分:1)

此代码:

$activityname = $_POST['activityname'.$i];

例如在第一个循环上生成:

$activityname = $_POST['activityname1'];

如果表单中没有具有该精确名称的输入,则该值将为空。

如果您有来自以下表单的输入数组:

<input name="activityname[]" />

然后数组索引将从0开始,你的PHP应该是:

for ($i=0; $i<=2; $i++)
{
    $divisionname = $_POST['divisionname'];
    $divisionchief= $_POST['divisionchief'];
    $period = $_POST['period'];
    $month = $_POST['month'];
    $activityname = $_POST['activityname'][$i]; //right here
    $unit = $_POST['unit'][$i];
    //etc, following this pattern...
}

答案 1 :(得分:0)

请将此vardump()函数放在您的网页下面,然后放行

  

print vardump($ _ POST);

位于代码的底部。我打赌你会发现那些变量没有定义。

  

function vardump($ var)       {

  $bgtable = '#9999CC';
  $bghead = '#CCCCFF';
  $bgvalue = '#E6E6FF';
  $bginfo = '#F3F3FF';
  $output = "<font face='Verdana,Arial'><table width='100%' bgcolor='$bgtable' cellpadding='2' cellspacing='1
'>";
  #<tr><td width='15%'><b>Type</b></td><td><b>Value(s) and information</b></td></tr>
  if(is_array($var)){
    #$element = 0;
    foreach($var as $key=>$current){
      if(is_array($current)){
        $output .= "<tr bgcolor='$bghead'><td width='20%'><font size='1'><b> ['$key'] => " . gettype($current
) . "</b></font></td><td bgcolor='$bgvalue'><font size='1'>" . vardump($current) . '</font></td></tr>';
      }
      else{
        $output .= "<tr bgcolor='$bghead'><td width='20%'><font size='1'><b> ['$key'] => " . gettype($current
) . "</b></font></td><td bgcolor='$bgvalue'><font size='1'>$current </font></td></tr>";
      }
      #$element++;
    }
  }
  else{
    $output .= "<tr bgcolor='$bghead'><td width='20%'><font size='1'><b>" . gettype($var) . "</b></font></td>
<td><font size='1'>$var </font></td></tr>";
  }
  $output .= '</table></font>';
  return $output;
}

答案 2 :(得分:0)

您需要处理插入错误:

if(!mysql_query($sql)){
    echo mysql_error(); 
}

不推荐使用mysql扩展,请考虑使用MySQLi。考虑在变量上使用转义字符串函数。

http://cl1.php.net/manual/es/mysqli.real-escape-string.php

答案 3 :(得分:0)

你可能会尝试添加echo $ sql;

像这样:

 $sql="INSERT INTO workplan_detail(division_name, division_chief, Period, Month, activity_name, Unit, Weightage, per_100, per_75, per_50, per_less50) VALUES ('$divisionname', '$divisionchief', '$period', '$month', '$activityname', '$unit', '$weightage', '$per100', '$per75', '$per50', '$perless50')";

echo $sql;

mysql_query($sql);

header("location:workplan.php");
}`
echo"This information has been added to the database."; 

这会给你查询,你可以尝试按顺序修复不工作的值。