在PHP中通过POST传递要插入MySQL的数组

在PHP中通过POST传递要插入MySQL的数组,php,mysql,post,Php,Mysql,Post,我有一个表单,允许用户在多个字段中输入类和活动,这些字段声明如下: label for ="classact">Classes and Activities</label> <input type = "text" name = "classact[0]" value ="" id ="classact[0]"> <input type = "text" name = "classact[1]" value ="" id

我有一个表单,允许用户在多个字段中输入类和活动,这些字段声明如下:

    label for ="classact">Classes and Activities</label>
        <input type = "text" name = "classact[0]" value ="" id ="classact[0]">
        <input type = "text" name = "classact[1]" value ="" id ="classact[1]">
        <input type = "text" name = "classact[2]" value ="" id ="classact[2]">
查询没有插入,但也没有返回任何错误,即使我已经打开了它们

我的主要问题是,我做错了什么导致act1、act2和act3的值显示为数组[0]、数组[1]和数组[2]

我的第二个问题是,我这样做对吗?我对php有点陌生,我担心我可能会很难做到这一点


任何帮助都将不胜感激,如果您需要任何其他信息,请告诉我

它不会插入任何内容,因为(除其他外)您的查询字符串没有正确生成

('@lid','Array[0],'Array[1],'Array[2])
撇号弄乱了。我想建议(在我看来)一种更干净、更有条理的方式来完成你的任务:

注意:您显然是在使用mysql_*-stack,所以我的示例也是基于它的。但请注意,这是不推荐的。请使用mysqli或更好的:取而代之。

<?php

$maininsert = "INSERT INTO `camptest`
              (`name`, `city`, `phone`, `photo`)
              VALUES
              ('{$_POST['name']}', '{$_POST['city']}', '{$_POST['phone']}', '$photoinfo')";

//perform the main insert and fetch the insert id
mysql_query($maininsert);

$last_id = mysql_insert_id();

// Put the keys and values of the acts in arrays. We can already 
// populate them with the one key-value-pair we already know
$act_keys = array('cid');
$act_values = array($last_id);

foreach($_POST['classact'] as $key => $value) {
  //walk through the POSTed acts and add them to the corresponding array
  $act_keys[] = 'act'.($key+1);
  $act_values[] = $value;
}

//Now build the whole string:
$insert_acts = "INSERT INTO `class_act` 
               (`" . implode("`, `", $act_keys) . "`) 
               VALUES 
               ('" . implode("', '", $act_values) . "')";

//and finally perform the query:
mysql_query($insert_acts);
在下一步中,您可能希望稍微重构代码,使其更具结构化和可读性。由于您正在执行一项任务,即“向表中插入某些内容”两次,因此我们还可以从中生成一个可恢复的功能:

<?php 

function my_insert($table, $data) {
  // We leverage the flexibility of associative arrays
  $keys   = "`" . implode("`, `", array_keys($data)) . "`";
  $values = "'" . implode("', '", $data) . "'";

  mysql_query("INSERT INTO `{$table}` ({$keys}) VALUES ({$values})");

  return mysql_insert_id(); //This might come in handy...
}

//in order to use this function, we now put our data into associative arrays:
$class_insert = array(
  'name'  => $_POST['name'],
  'city'  => $_POST['city'],
  'phone' => $_POST['phone'],
  'photo' => $photoinfo
);

$class_insert_id = my_insert('camptest', $class_insert); //and pass it to the function

// You can either build the array while looping through the POSTed values like above, 
// or you can pass them in directly, if you know that there will always be 3 values:
$activities_insert = array(
  'cid'  => $class_insert_id,
  'act1' => $_POST['classact'][0],
  'act2' => $_POST['classact'][1],
  'act3' => $_POST['classact'][2]
); 

$activities_insert_id = my_insert('class_act', $activities_insert);

您的代码可能容易受到sql注入的攻击。尝试使用api。简单地切换到mysqli不会阻止SQL注入;事实上,这可能会使漏洞变得更严重,因为
mysql\u query
不允许在一个SQL查询中使用多个语句(而mysqli可以)。无论如何,您都会想切换,因为旧的mysql内容已经被弃用了……但为了安全起见,您还需要学习使用预处理语句,这些语句在正确使用时可以在很大程度上防止SQL注入。非常感谢您花时间阅读这些内容并给我一些提示。我需要做些什么才能使用mysqli或PDO吗?@SmashCode不,你不需要任何东西,PHP内置了所有的好东西。您只需使用不同的方法与数据库通信。我想这里有一个很好的起点:好的,再次感谢你,我真的很感谢你的帮助。我相信你会知道我对php还很陌生。@SmashCode不客气!当我们开始学习的时候,我们都是新手。是的,老实说,你的代码看起来并不是很吸引人,但事情就是这样的——你必须从某个地方开始才能变得更好:)我的代码在我开始使用PHP时可能一点也不好看。很明显,你确实花了一些精力,而不是从某个地方复制/粘贴它;你要求的是反馈,而不是解决方案——我喜欢这样-如果有人偶然发现这个问题,我选择这个作为答案,因为PDO的东西把我带到了我需要的地方。如果你是这样做的,你应该重新考虑,这样你就不会像我一样有问题了。
<?php

$maininsert = "INSERT INTO `camptest`
              (`name`, `city`, `phone`, `photo`)
              VALUES
              ('{$_POST['name']}', '{$_POST['city']}', '{$_POST['phone']}', '$photoinfo')";

//perform the main insert and fetch the insert id
mysql_query($maininsert);

$last_id = mysql_insert_id();

// Put the keys and values of the acts in arrays. We can already 
// populate them with the one key-value-pair we already know
$act_keys = array('cid');
$act_values = array($last_id);

foreach($_POST['classact'] as $key => $value) {
  //walk through the POSTed acts and add them to the corresponding array
  $act_keys[] = 'act'.($key+1);
  $act_values[] = $value;
}

//Now build the whole string:
$insert_acts = "INSERT INTO `class_act` 
               (`" . implode("`, `", $act_keys) . "`) 
               VALUES 
               ('" . implode("', '", $act_values) . "')";

//and finally perform the query:
mysql_query($insert_acts);
<input type="text" name="classact[]" value="" id="classact1">
<input type="text" name="classact[]" value="" id="classact2">
<input type="text" name="classact[]" value="" id="classact3">
<?php 

function my_insert($table, $data) {
  // We leverage the flexibility of associative arrays
  $keys   = "`" . implode("`, `", array_keys($data)) . "`";
  $values = "'" . implode("', '", $data) . "'";

  mysql_query("INSERT INTO `{$table}` ({$keys}) VALUES ({$values})");

  return mysql_insert_id(); //This might come in handy...
}

//in order to use this function, we now put our data into associative arrays:
$class_insert = array(
  'name'  => $_POST['name'],
  'city'  => $_POST['city'],
  'phone' => $_POST['phone'],
  'photo' => $photoinfo
);

$class_insert_id = my_insert('camptest', $class_insert); //and pass it to the function

// You can either build the array while looping through the POSTed values like above, 
// or you can pass them in directly, if you know that there will always be 3 values:
$activities_insert = array(
  'cid'  => $class_insert_id,
  'act1' => $_POST['classact'][0],
  'act2' => $_POST['classact'][1],
  'act3' => $_POST['classact'][2]
); 

$activities_insert_id = my_insert('class_act', $activities_insert);