2013-04-20 76 views
-1

当前正在与朋友一起处理服务脚本。当我说服务脚本时,我的意思是它只会用于更新和向数据库添加值。现在我的朋友已经把她所有的数据存储为数组了。多个数组 - 将值插入数据库

我已经成功地进入了其中一个数组,现在我很困惑我该怎么做多个数组。因此,总而言之,这是一个关于如何将当前代码重构为更具动态性的问题。

另外请记住,她有数百个数组,这需要做到,因此它不像2或3它的字面数百。

我更新的代码:(请阅读评论)

$colors['Colors_All'] = array("Black","Charcoal"); // Add unique indexes 
$colors['Colors_Bright_All'] = array("Silver","White"); // Add unique indexes 

$AllArrays = get_defined_vars(); // Get all defined vars 
$Arrays = array(); // Set a default array 

foreach ($AllArrays as $varName => $value) { // Run through all the variables set in AllArrays 
     if(is_array($value) && $varName == 'colors') { // If array is colors then 
       $Arrays = array_merge($Arrays, $value); // Merge those arrays into the new array 
     } 
} 

var_dump($Arrays); 

$sql = "INSERT INTO `product_features` ("; // Create the initial query 

foreach ($Arrays as $column => $value) { // ForEach over the array 
     $sql .= "$column,"; // Use the Key Example : 'Colors_All and Color_Bright_All' as a column name 
} 

$sql2 = rtrim($sql, ","); // trim the initial "," from the columns at the end 
$sql2 .= ")"; // Close off the columns names 
$sql2 .= " VALUES "; 

foreach ($Arrays as $column => $value) { // This is where the problem starts -_- 
     foreach ($value as $key => $insert) { // Get the value 
       $sql2 .= "('$insert', '$insert'),"; // I need to have unique values here :(
     } 
} 

$finSQL = rtrim($sql2, ","); // Strip off the remaining "," 

而且我知道我不是绑定的参数,我一旦我得到的实际硬的东西出路。

现在做$ finSQL转储,当我得到这个:

string(152) "INSERT INTO `product_features` (Colors_All,Colors_Bright_All) VALUES ('Black', 'Black'),('Charcoal', 'Charcoal'),('Silver', 'Silver'),('White', 'White')" 

怎样才可以有独特的价值在我插入查询中的值?这是令我困惑的最后一部分。

+0

你有很多定义的变量在PHP脚本中定义?我不会推荐它,但如果它们不遵循一个明确的模式,你可以尝试使用'get_defined_vars()'。你能够展示更多的代码吗? – calcinai 2013-04-20 05:04:49

+0

你可以重新输入你刚输入的内容吗? PHP文档中定义了一堆定义的变量?我会看看get_defined_vars() – 2013-04-20 05:05:44

+1

你似乎缺乏将各种颜色链接在一起的明智钥匙。如果你解释了最终数据库应该是什么样子,这将有所帮助。 – 2013-04-20 05:14:03

回答

0

嗯,我知道了“做”的序列化每一个阵列,通过实际的数组索引作为唯一标识符,像这样的数据库:

<?php 
ini_set("error_reporting", E_ALL); 
ini_set("display_errors", TRUE); 

$conn = new MysqlI("HOST", "USER", "PASSWORD", "DATABASE" /*Optionally put the PORT NUMBER HERE : , 3306 */);  
$conn->set_charset('utf8'); // Always explicitly state the character encoding! 

$clearSQL = "TRUNCATE TABLE `product_features`;"; 
$clearNOW = $conn->prepare($clearSQL);// For updating this will delete all records in the table 
$clearNOW->execute(); 

$colors['Colors_All'] = array("Black", "Charcoal", "Pink", "Cheese", "Dog", "Punk"); // Add unique indexes 
$colors['Colors_Bright'] = array("Silver", "White", "Yellow", "Blue", "Indigo"); // Add unique indexes 
$colors['Colors_Bright_All'] = array("Silver", "White", "DarkYellow", "Dark_Pink",); // Add unique indexes 
$colors['Colors_Dark'] = array("Silver", "White", "DarkWE", "DarkCheese", "DarkBla"); // Add unique indexes 
$colors['Colors'] = array("Silver", "White", "DarkIDK", "DarKsome", "waDark", "dark"); // Add unique indexes 
$colors['Colors_Green'] = array("Silver", "White", "WAA", "WIWIW", "OMG", "Blood"); // Add unique indexes 
$colors['Colors_Red'] = array("Silver", "White", "IRI", "owow", "Darkness", "night"); // Add unique indexes 

$AllArrays = get_defined_vars(); // Get all defined vars 
$Arrays = array(); // Set a default array 

foreach ($AllArrays as $varName => $value) { // Run through all the variables set in AllArrays 
    if (is_array($value) && $varName == 'colors') { // If array is colors then 
     $Arrays = array_merge($Arrays, $value); // Merge those arrays into the new array 
    } 
} 

$sql = "INSERT INTO `product_features` (`ArrayIndex`, `ArrayValues`) VALUES "; // Create the initial query 

foreach ($Arrays as $ArrayIndex => $Arrayvalues) { // ForEach over the array 
    $sql .= "('$ArrayIndex','" . $mysqli->mysqli_real_escape_string(serialize($Arrayvalues)) . "'),"; // Use Array Index as the Unique Array Identifyer and Arrayvalues for the value 
} 

$finSQL = rtrim($sql, ","); // Strip off the remaining "," 

var_dump($finSQL); // Check the query 

$INSERT = $conn->prepare($finSQL); // Get query ready 
$INSERT->execute(); // Execute 

?> 

与数据库中的结果是this

+0

all这个'$ AllArrays'的东西**完全没用。**你已经得到**完全相同的'$ colors'数组**。将序列化数组存储在数据库中是没有意义的。 – 2013-04-24 12:32:04

+0

谁在乎你对什么有意义? - 你是谁?噢......这个工作,我取得了成功。我只是回答我的回答,因为你和别人说什么都是毫无用处的。所以请在你的生活中做点什么。 - 谢谢。 – 2013-04-24 20:54:16

+0

如果你有更好的方法来做到这一点,那么请告诉。这不是我的问题,我不会将数组存储在数据库中,但是我遇到了一位SO成员遇到的问题,并且我试图提供帮助。不像其他人 – 2013-04-24 20:58:23

1

把$ Colors_Bright_All,$ Colors_Light_All放到最后一个数组中让我们说$ finalArray并使用foreach循环遍历该数组,然后执行你现有的foreach循环。

+0

对,但现在我需要这些值插入到基于数组的数据库中的不同列中 – 2013-04-20 05:03:24

+0

if name = foo然后插入到else if name = yo然后插入到b中。佛拉。你应该使用单独的数组。 – Jonast92 2013-04-20 05:13:46

+0

好吧chandresh我可能想出了一个解决方案,但我有一个问题给你。我如何将〜300个阵列组合起来而不必输入它们? (字面意思是约300阵列) – 2013-04-20 13:29:40

-1
<?php 
function myInsert($myArray) 
{ 
    $colour_value = ""; 
    $stmt = $mysqli->prepare("INSERT INTO `product_features` (Colors_All) VALUES (?)"); 

    if ($stmt) { 
     $stmt->bind_param("s", $colour_value); 


     foreach ($myArrayas AS $value) { 
      $colour_value = $value; 
      $stmt->execute(); 
     } 

     $stmt->close(); 
    } 
} 


function myInsert($Colors_Bright_All); 
function myInsert($Colors_Light_All); 
?> 

编辑

<?php 
$newArray = array(); 

foreach($_GLOBALS as $val) 
{ 
    if(substr($val, 0, 7) == "Colors_") 
     myInsert($val); 
} 

?> 
+0

所以100个+阵列功能......是啊。 ..不,你刚刚做了什么,我已经拥有了? – 2013-04-20 05:26:52

+0

我的文本缺失..是的,我知道他们是相似的,你只是部分地使用你准备的声明。在你的问题中,你不会说明你从哪里得到数组。你只需要在每个数组的循环中调用该函数。 – Bradmage 2013-04-20 05:29:41

+0

要有100 +阵列我不得不从数据库中假设?或者他们是硬编码的?你只需使用foreach($ result as $ data)myInsert($ data); – Bradmage 2013-04-20 05:31:28

0

作为解决您上述问题的请尝试执行下面的代码片段

例如

$finalarray=array(); 

    $Colors_Bright_All = array("Silver","White","Gold","Royal_Blue","Dodger_Blue","Deep_Sky_Blue","Deep_Green","Forest_Green","Bright_Green","Violet"); 

    $Colors_Light_All = array("Light_Gray","Silver","White","Gold","Dodger_Blue","Deep_Sky_Blue","Light_Blue","Bright_Green","LightGreen","Light_Green"); 

array_push($finalarray,'('.join(',',$Colors_Bright_All).')'); 
array_push($finalarray,'('.join(',',$Colors_Light_All).')'); 

    $value=join(',',$finalarray); 

$sql = "INSERT INTO `product_features` (Colors_All) VALUES ".$value; // Build the initial query 
$finSQL = rtrim($sql, ","); // Strip the last , so our mysql query doesn't goof up. 
$stmt = $conn->prepare($finSQL); 
$stmt->execute(); 
0

正如你在其他的答案被告知已,做一个嵌套数组是唯一明智的方法

$colors['bright'] = array("Silver","White","Gold"...); 
$colors['light'] = array("Light_Gray","Silver","White"...); 

foreach ($colors as $type => $array) { 

    // put your code here 

} 

,如果你需要更多的数据只是$type(因为它不能被旁边插入从极其模糊的问题告诉了),你可以用这个数据增加一个阵列以及

+0

没想到这是模糊的......我现在有一段代码可以工作,我需要那段代码来动态地将数据从数组添加到数据库。对不起,如果这是什么,每一个stumping。 – 2013-04-20 05:36:37

+0

'将数据从数组动态添加到数据库的代码' - 您的代码已经完成了。还有一些其他问题你仍然面临着人们的隐瞒。 – 2013-04-20 05:39:23

+0

隐藏它? :S我迷路了,如果你这么想,我很抱歉。当我写下这个问题时,我认为它写得很好,并且重点在于:S – 2013-04-20 05:40:37

0

,如果你的代码工作正常,然后更改

foreach ($Arrays as $column => $value) { // This is where the problem starts -_- 
     foreach ($value as $key => $insert) { // Get the value 
       $sql2 .= "('$insert', '$insert'),"; // I need to have unique values here :(
     } 
} 

foreach ($Arrays as $column => $value) { // The problem will be fixed :D 
     $sql2 .="("; 
     foreach ($value as $key => $insert) { // Get the value 
       $sql2 .= "'$insert',"; // You will have unique values here :D 
     } 
     $sql2 = chop($sql2,","); // strip the ending , 
     $sql2 .= "),"; 
}