重复输入mysql数据库

时间:2013-08-05 08:23:56

标签: php mysql

将数据插入数据库时​​遇到问题。但是,当我回应它时,它不会重复。这是我的代码:

    foreach($_POST['purpose'] as $i => $a){
         echo $a."-".$i."<br />";
         if($_POST['purpose'][$i] == "recieved"){
              $purpose[$i] = "'1','0','0','0','0'";
         } elseif($_POST['purpose'][$i] == "released"){
              $purpose[$i] = "'0','1','0','0','0'";
         } elseif($_POST['purpose'][$i] == "recalled"){
              $purpose[$i] = "'0','0','1','0','0'";
         } elseif($_POST['purpose'][$i] == "charges"){
              $purpose[$i] = "'0','0','0','1','0'";
         } elseif($_POST['purpose'][$i] == "adjustments"){
              $purpose[$i] = "'0','0','0','0','1'";
         }   
         $query = "INSERT INTO responsibility_center VALUES(
               '',
               '".$_POST['or_number']."',
               '".$_POST['response'][$i]."', 
               $purpose[$i])";
         echo $query."<br />";
         $result1 = mysql_query($query) or die(mysql_error()." inserting responsibility center");

          $responseid = mysql_insert_id();

          $query2 = "INSERT INTO particulars VALUES (
                     '',
                     '".$responseid."',
                     '".$_POST['details'][$i]."')";
          echo $query2."<br />";
          $result2 = mysql_query($query) or die(mysql_error()." inserting particulars");
    }

我不知道我的代码在哪里出错是因为它没有给我错误。 :/

1 个答案:

答案 0 :(得分:3)

$result2 = mysql_query($query)

应该是

$result2 = mysql_query($query2)

这就是为什么你应该为变量命名比$query更有意义的原因。另请阅读:http://bobby-tables.com/php.html