0

请帮助我从表格和动态文本字段中获取行,然后将其插入另一个表格。我想做的是从下表中获取一些特定的 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[]' />

我用来插入表的查询

                    <?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:
4

1 回答 1

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 清理它,使用mysql_real_escape_string。试试下面的代码。

<?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 输入正确设置为数组,这应该可以工作。它还可以整理您的代码并帮助解决不必要的延迟。每毫秒都很重要。:) 如果这不起作用,请发布返回的错误并再次查询。

于 2012-06-23T05:40:31.147 回答