将行和动态文本框插入mysql

时间:2012-06-23 05:01:16

标签: php mysql insert row

请从表和动态文本字段中获取行,然后将其插入另一个表中,我需要您的帮助。我想要做的是从下表中获取某些state_id,与同一页面上显示的<input type='text' name='constituency[]' />文本框中的数据结合,然后插入表格

                    +---------+--------------------------+
                    |state_id |  state   |   senatorial  |  
                    |--- -----|----------|---------------|
                    |    1    |  utah    |    A-North    | 
                    |    2    |  utah    |    B-South    |  
                    +---------+----------+---------------+

我用来抓取信息的HTML。选区是一个动态文本字段,不止一次出现,因此需要将它放在数组中

                    <input type='hidden' name="state_id[]"   value="<?php echo $row['state_id'];?>" />
                    <input type='text'   name='constituency[]' />

查询我正在使用INSERT进入表

                    <?php
$constituencys = $_POST['constituency']      ; 
        $state_id = $_POST['state_id']; 


         if(isses($_POST['constituency']))
   {
$constituencys = $_POST['constituency']      ;   
                      foreach($constituencys as          $constituency) {  
     $state_id[$i]= $state_id[$i];
                      $query = mysql_query(" 
         INSERT INTO `constituency` (
                    `constituency_id`,
                    `state_id`,
                    `constituency_name`
                    ) VALUES (
                    NULL,
                    '".$state_id[$i]."',
                    '".$constituency."'
                    "); 
                       }



                    if(mysql_affected_rows()>0){
                    echo "Inserted Successfully";
                    }else{
                    echo "No Row Inserted";
                    echo mysql_error() ;
                    echo"<br>Error:".mysql_error();
                   echo"<br>Query:".$query; 
      } } ?>

表我正在插入看起来像这样

                    +----------------+-------------+----------------------+
                    |constituency_id |  state_id   |   constituency_name  |  
                    +----------------+-----   -----+----------------------+

当我运行代码“No Row Inserted”时  显示在屏幕上

请帮帮我。 我非常感谢,谢谢。

错误显示

    Error: You have an error in your mysql syntax; check the manual that corresponds to your mysql server for the right syntax to use near " at line 2

Query:

1 个答案:

答案 0 :(得分:0)

<?php
$constituencys = $_POST['constituency']; 
$state_id = $_POST['state_id']; 

$i = 0;
while($i<count($constituencys)){
    $constituencys[$i]= $constituencys[$i];
    $state_id[$i]= $state_id[$i];
    $query = "INSERT INTO `constituency` ( 
        `constituency_id`, `state_id`, `constituency_name`
        ) VALUES (
        NULL,
        '".$state_id[$i]."',
        '".$constituencys[$i]."'
        "; 

    mysql_query($query);          
    $i++;  
}

if(mysql_affected_rows()>0){
    echo "Inserted Successfully";
} else {
    echo "No Row Inserted";
    echo "<br><br>Error: " . mysql_error();
    echo "<br><br>Query: " . $query;
} 
?>

首先尝试使用此代码,使用结果更新您的帖子。

<强>更新

看起来你可能有'脏'输入。请确保在插入之前使用mysql_real_escape_string对mysql进行清理。请尝试以下代码。

<?php
$constituencys = $_POST['constituency']; 
$state_id = $_POST['state_id']; 

$i = 0;
while($i<count($constituencys)){
    $constit = mysql_real_escape_string($constituencys[$i]);
    $state = mysql_real_escape_string($state_id[$i]);
    $query = "INSERT INTO `constituency` ( 
        `constituency_id`, `state_id`, `constituency_name`
        ) VALUES (
        NULL,
        '$state',
        '$constit')
        "; 

    mysql_query($query);          
    $i++;  
}

if(mysql_affected_rows()>0){
    echo "Inserted Successfully";
} else {
    echo "No Row Inserted";
    echo "<br><br>Error: " . mysql_error();
    echo "<br><br>Query: " . $query;
} 
?>

通知:在查询结束时,您错过了一个紧密的括号。它已在第二个示例中添加。在继续测试之前确保它在那里。

<?php
$constituencys = $_POST['constituency']; 
$state_id = $_POST['state_id']; 

$query = "INSERT INTO `constituency` (`constituency_id`, `state_id`, `constituency_name`) VALUES";

for($i=0; $i<count($constituencys); $i++){
    $constit = mysql_real_escape_string($constituencys[$i]);
    $state = mysql_real_escape_string($state_id[$i]);
    $query .= "(NULL, '$state', '$constit'), "; 
}
$query = substr(trim($query), 0, -1); //trims then removes trailing comma
mysql_query($query);          

if(mysql_affected_rows()>0){
    echo "Inserted Successfully";
} else {
    echo "No Row Inserted";
    echo "<br><br>Error: " . mysql_error();
    echo "<br><br>Query: " . $query;
} 
?>

假设您的HTML输入正确设置为数组,这应该可行。它还可以整理您的代码,并有助于避免不必要的延迟。每毫秒都很重要。 :)如果这不起作用,请发布返回的错误并再次查询。