2013-02-06 56 views
0

我有一个表格,它允许用户输入的类和活动分为多个字段,这些字段被声明如下:通过POST在PHP将数组传递给被插入的MySQL

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]"> 

当窗体被传递这是在插入处理代码:

$maininsert = "INSERT INTO `camptest` 
     (`name`, `city`, `phone`, `photo`) 
     VALUES 
     ('$_POST[name]', '$_POST[city]', '$_POST[phone]', '$photoinfo') 
     SET @lid = LAST_INSERT_ID() 
     "; 

    $classactinsert = "INSERT INTO `class_act` 
       (`cid`"; 

    for($i = 0; $i < 3; $i++) 
    { 
     if(isset($_POST['classact'][$i])) 
     { 
      $temp = $i+1; 
      $classactinsert = $classactinsert . ",`act$temp`"; 
     } 
    } 

    $classactinsert = $classactinsert . ") 
           VALUES 
           ('@lid'"; 

    for($i = 0; $i < 3; $i++) 
    { 
     if(isset($_POST['classact'][$i])) 
     { 
     $classactinsert = $classactinsert . ",'$_POST[classact][$i]"; 
     } 
    } 

    $classactinsert = $classactinsert . ")";         

    $indata = $maininsert . $classactinsert; 

    $result = mysql_query($indata); 

我意识到这就是很多的代码,但在填写表格并提交,这是获取生成查询:

INSERT INTO `camptest` (`name`, `city`, `phone`, `photo`) VALUES ('Multiple Activities', 'Nowhere', '555-555-1111', 'images/51127f6b06d1e.jpg') SET @lid = LAST_INSERT_ID() INSERT INTO `class_act` (`cid`,`act1`,`act2`,`act3`) VALUES ('@lid','Array[0],'Array[1],'Array[2]) 

查询没有插入,但它也没有丢回任何错误,即使我打开了它们。

我的主要问题是,我在做什么错误导致值act1,act2和act3显示为Array [0],Array [1]和Array [2]?

我二次的问题是,我是不是甚至要对这个正确的方式?我对PHP有点新鲜感,恐怕我可能会这么做很难吗?

任何帮助,将不胜感激,让我知道如果你需要任何额外的信息。

+0

您的代码可能容易受到SQL注入。尝试使用[mysqli](http://www.php.net/manual/en/book.mysqli.php)api。 – Alepac

+0

简单地切换到mysqli不会阻止SQL注入;事实上,它可能会使这个漏洞变得更糟,因为'mysql_query'将不允许在一个SQL查询中使用多个语句(而mysqli可以)。无论如何,你会想要切换,因为旧的mysql的东西已被弃用......但出于安全考虑,你还需要学习使用准备好的语句,这些语句会在正确使用时阻止SQL注入。 – cHao

回答

2

它不插入任何东西,因为(除其他事项外)您的查询字符串没有被正确建立。

('@lid','Array[0],'Array[1],'Array[2]) 

撇号乱成一团。我想建议一个(在我看来)更清洁,更结构化的方式来执行您的任务:

注意:你显然正在使用mysql _ * - 堆栈,所以我的例子也是基于它。但请注意,这已被弃用。请使用mysqli或更好: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); 

也请注意,这个代码是关于SQL-Injection非常脆弱,应该绝对不生产使用!确保使用准备好的语句(如使用PDO)或/和正确清理输入。

而且,这种解决方案只是我的建议和很多方法可以做到这一个。但是,嘿,你问的意见:) PHP是非常灵活的语言,所以很容易得到的东西做了,但也有许多方法来完成它,所以总有机会选择一个硬难看。其他的,特别是强类型的语言可能会阻止设计。但是,PHP是很容易学习,我敢肯定,你的代码将逐步完善:)

我注意到另一件事:你并不需要在你的HTML指定数组的键,你只需要使清楚的是这是一个名称后面带有[]的数组。另外,我不知道,如果你使用id -attributes是有效的,但你可能想使用一些更简单:

<input type="text" name="classact[]" value="" id="classact1"> 
<input type="text" name="classact[]" value="" id="classact2"> 
<input type="text" name="classact[]" value="" id="classact3"> 

在接下来的步骤中,您可能需要重构你的代码一点点,使它更具结构和可读性。由于您正在执行一项任务,这是“将东西放到一台”,两次,我们也可以做一个resusable功能出来的:

<?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); 

有确保足够的空间用于改进和优化 - 只是想告诉你多么可怕的PHP可以:-P

+0

非常感谢您花时间阅读本文,并给我指点。我需要做什么才能够使用mysqli或PDO? – SmashCode

+0

@SmashCode不需要任何东西,PHP内置了所有好东西。您只需使用不同的方法与数据库进行通信。我认为这是一个很好的起点:http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/ – Quasdunk

+0

好的,再次感谢,我非常感谢帮助。我相信你可以告诉我很新的PHP。 – SmashCode