【问题标题】:two arrays in one form to mysql with php用php将两个数组以一种形式发送到mysql
【发布时间】:2014-09-06 06:59:04
【问题描述】:

我有一个包含复选框的两个字段集的表单:

<fieldset style="width:300px; height:200px; overflow:scroll;">
    <input type="checkbox" name="table[]" id="01" value='ado'> Adoption <br />
    <input type="checkbox" name="table[]" id="02" value='acc'> Accomodations <br />
    <input type="checkbox" name="table[]" id="03" value='ann'> Announcements <br />
    <input type="checkbox" name="table[]" id="04" value="bea"> Beauty/Fitness <br />
    <input type="checkbox" name="table[]" id="05" value="bus"> Business Oportunities    
</fieldset>

还有这个

<fieldset style="width:300px; height:200px; overflow:scroll;">
    <input type="checkbox" name="State[]" id="01" value='AL'> Alabama <br />
    <input type="checkbox" name="State[]" id="02" value='AK'> Alaska<br />
    <input type="checkbox" name="State[]" id="03" value='AZ'> Arizona<br />
    <input type="checkbox" name="State[]" id="04" value='AR'> Arkansas <br />
    <input type="checkbox" name="State[]" id="05" value='CA'> California <br />
</fieldset>

我正在使用此代码进入他们各自的表格

$table = $_POST['table'];  
$name = $_POST['name'];
$state = $_POST['State'];

if(is_array($table)){
  while(list($tables) = each($table)){
    $sql2 = "INSERT INTO tableName (name,table) VALUES ('$name','$tables')";
    $q2 = mysqli_query($db_conx,$sql2);
    }
}           

if(is_array($state)){
  while(list($key,$value) = each($state)){
   $sql3 = "INSERT INTO states (name,State) VALUES ('$name','$value')";
   $q3 = mysqli_query($db_conx,$sql3);
     }
}

当它被执行时,唯一输入的数据是状态 我用过

echo "table; ".$table."<br /> State; ".$state;

得到了

table; Array
State; Array012ALAKAZ

谁来帮帮我!

【问题讨论】:

  • 你不能回显一个数组,使用var_dump($table)var_dump($state)看看里面有什么。

标签: php mysql arrays checkbox


【解决方案1】:

你很容易受到sql injection attacks的攻击。

而您的table 查询正在使用reserved word,因此整个插入查询都失败了。由于您没有检查失败,只是假设成功,因此您永远不会看到任何错误消息。

永远不要EVER在处理外部资源(尤其是数据库)时假设成功。查询成功的方式只有 一种,失败的方式几乎无限。然而,您似乎认为 1:infinity 的赔率非常好。

$sql2 = "INSERT INTO tableName (name,`table`) VALUES ('$name','$tables')";
                                     ^-----^---you need these

$q2 = mysqli_query($db_conx,$sql2) or die(mysqli_error($db_conx));
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---you also need this

【讨论】:

  • 感谢我进行了必要的更改,现在它可以工作了
【解决方案2】:

这里你有解决方案,只做 2 个查询而不是 20 个查询:

$tables = $_POST['table'];  
$name = $_POST['name'];
$states = $_POST['State'];

$states_values = '';
$tables_values = '';

$i = 0;
foreach($states as $state)
{
    $i++;
    $last = $i == count($states) ? true : false;
    $states_values .= '(' . $name . ', ' . $state . ')' . ($last ? '' : ',');
}

$i = 0;
foreach($tables as $table)
{
    $i++;
    $last = $i == count($tables) ? true : false;
    $tables_values .= '(' . $name . ', ' . $table . ')' . ($last ? '' : ',');
}

mysqli_query($db_conx, 'INSERT INTO states (name, State) VALUES ' . $states_values;
mysqli_query($db_conx, 'INSERT INTO tableName (name, table) VALUES ' . $tables_values;

正如马克所说,你应该逃避你的输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多