将数组元素插入数据库

时间:2014-11-13 08:50:17

标签: php mysql

我有这个功能:它正常工作,

function ms_get_did_detail($id) {
    global $link;
    $q2="select Dest,Priority from destpr where Id='$id'";

    if($res2=mssql_query($q2)) {
        while($row2[]=mssql_fetch_array($res2,MSSQL_ASSOC)) {
            return $row2;
        }

        return 0;
    }

    return 0;
}

我希望将每个元素(每个Dest& Priority)插入到MYSQL中

if($info=ms_get_did_detail($value)) {
    print_r($info);
    $destination = $info['Dest'];
    $priority = $info['Priority'];
    my_did_destination ($priority , $dest , $active , $did_voip , $cc_id);                 
}     

它返回如下数组:

[0]=> Array (
    [Dest] => 100
    [Priority] => 1
)
[1]=> Array (
    [Dest] => 200 
    [Priority] => 3 
) 
[2] => (
)

另外,我有这个函数在数据库中插入值:

function my_did_destination($priority="",$destination="") {
    global $link_voip;
    $sql="INSERT INTO cc_did_destination (destination,priority) 
          VALUES ('$destination','$priority')";

    $retval = mysql_query( $sql , $link_voip);

    if(! $retval ) {
        die('Could not enter data: ' . mysql_error());
    }
}

但它在

中插入空值

4 个答案:

答案 0 :(得分:1)

您正在插入ID为0的所有行,因此,如果已存在id=0的行,则会失败并且不会插入。

也许最简单的解决方案是使用以下SQL语句创建IDautoincrement

    ALTER TABLE cc_did_destination 
  MODIFY COLUMN id INT auto_increment;

然后更改您的INSERT声明:

  $sql="INSERT INTO cc_did_destination (destination,priority) 
             VALUES ('$destination','$priority')";

答案 1 :(得分:0)

您的$ info是行数组,它有数字键,而不是' Dest'。
您应该添加索引,例如$dest = $info[0]['Dest']

         if($info=ms_get_did_detail($value))
         {
            print_r($info);
            $dest = $info[0]['Dest'];
            $priority = $info[0]['Priority'];
            my_did_destination ($priority , $dest , $active , $did_voip , $cc_id);                 
         }     

或者您可以使用循环遍历$info

         if($info=ms_get_did_detail($value))
         {
            foreach($info as $row) {
              $dest = $row['Dest'];
              $priority = $row['Priority'];
              my_did_destination ($priority , $dest);                 
            }
         }     

另外,从插入语句中删除id

答案 2 :(得分:0)

这里有几个问题。

首先,您的第一个函数返回一个数组数组。即,它返回第一行的下标为0的数组(它只返回一行详细信息),这是一个包含行详细信息的数组。

您将此分配给$ info变量,因此它包含: -

[0]=> Array (
[Dest] => 100
[Priority] => 1
)

然后您将$ info ['Dest']分配给$ destination,将$ info ['Priority']分配给$ priority。然而,这些都不存在。你需要$ info [0] ['Dest']和$ info [0] ['Priority']。

第二个问题是您尝试将特定值分配给自动增量ID字段。只需将其从插入中删除,或将其赋值为null。

快速重写,我建议你需要这样的东西: -

<?php

if($info=ms_get_did_detail($value))
{
    print_r($info);
    foreach($info AS $info_row)
    {
        $destination = $info_row['Dest'];
        $priority = $info_row['Priority'];
        my_did_destination ($priority , $dest , $active , $did_voip , $cc_id);                 
    }
}     

function ms_get_did_detail($id)
{
    global $link;
    $q2="select Dest,Priority from destpr where Id='$id'";
    if($res2=mssql_query($q2))
    {
        if ($row2[]=mssql_fetch_array($res2,MSSQL_ASSOC))
        {
            while ($row2[]=mssql_fetch_array($res2,MSSQL_ASSOC))
            {
            }
            return $row2;
        }
        else
        {
            return 0;
        }
    }
    return 0;
}

function my_did_destination($priority="",$destination="")
{
    global $link_voip;
    $priority = mysql_real_escape_string($priority);
    $destination = mysql_real_escape_string($destination);
    $sql="INSERT INTO cc_did_destination (id,destination,priority) VALUES (NULL,'$destination','$priority')";

    $retval = mysql_query( $sql , $link_voip);
    if(! $retval )
    {
        die('Could not enter data: ' . mysql_error());
    }
}

编辑

如果您想避免不必要的多次插入,那么使用对象可能会更容易。这样,当有足够的批次时,你可以轻松地进行插入(我通常一次做255次)。

这样的东西,虽然你可能应该使用mysqli _ *

<?php

if($info=ms_get_did_detail($value))
{
    print_r($info);
    $insert_object = new insert_details($link_voip);
    foreach($info AS $info_row)
    {
        $insert_object->set_row($info_row['Priority'], $info_row['Dest']);
    }
    unset($insert_object);
}     

function ms_get_did_detail($id)
{
    global $link;
    $q2="select Dest,Priority from destpr where Id='$id'";
    if($res2=mssql_query($q2))
    {
        if ($row2[]=mssql_fetch_array($res2,MSSQL_ASSOC))
        {
            while ($row2[]=mssql_fetch_array($res2,MSSQL_ASSOC))
            {
            }
            return $row2;
        }
        else
        {
            return 0;
        }
    }
    return 0;
}

class insert_details()
{
    private $db;
    private $insert_row = array();
    public function __CONSTRUCT($db)
    {
        $this->db = $db;
    }
    public function __DESTRUCT()
    {
        $this->do_insert();
    }
    public function set_row($priority="",$destination="")
    {
        $priority = mysql_real_escape_string($priority, $this->db);
        $destination = mysql_real_escape_string($destination, $this->db);
        $this->insert_row[] = "(NULL,'$destination','$priority')";
        if (count($this->insert_row) > 255)
        {
            $this->do_insert();
        }
    }
    private function do_insert()
    {
        $sql="INSERT INTO cc_did_destination (id,destination,priority) VALUES ".implode(',', $this->insert_row);

        $retval = mysql_query($sql, $this->db);
        if(! $retval )
        {
            die('Could not enter data: ' . mysql_error());
        }
        $this->insert_row = array();
    }

}

快速粗略mysqli_ *等效,假设 $ link_voip 是一个mysqli连接。请注意,带有绑定参数的预处理语句是一个选项(并且它会使忘记转义变量变得更加困难),但是当您执行多个这样的插入时,它会变得有点混乱。

<?php

if($info=ms_get_did_detail($value))
{
    print_r($info);
    $insert_object = new insert_details($link_voip);
    foreach($info AS $info_row)
    {
        $insert_object->set_row($info_row['Priority'], $info_row['Dest']);
    }
    unset($insert_object);
}     

function ms_get_did_detail($id)
{
    global $link;
    $q2="select Dest,Priority from destpr where Id='$id'";
    if($res2=mssql_query($q2))
    {
        if ($row2[]=mssql_fetch_array($res2, MSSQL_ASSOC))
        {
            while ($row2[]=mssql_fetch_array($res2, MSSQL_ASSOC))
            {
            }
            return $row2;
        }
        else
        {
            return 0;
        }
    }
    return 0;
}

class insert_details()
{
    private $db;
    private $insert_row = array();
    public function __CONSTRUCT($db)
    {
        $this->db = $db;
    }
    public function __DESTRUCT()
    {
        $this->do_insert();
    }
    public function set_row($priority="",$destination="")
    {
        $priority = mysqli_real_escape_string($this->db, $priority);
        $destination = mysqli_real_escape_string($this->db, $destination);
        $this->insert_row[] = "(NULL,'$destination','$priority')";
        if (count($this->insert_row) > 255)
        {
            $this->do_insert();
        }
    }
    private function do_insert()
    {
        $sql="INSERT INTO cc_did_destination (id,destination,priority) VALUES ".implode(',', $this->insert_row);

        $retval = mysqli_query($this->db, $sql);
        if(! $retval )
        {
            die('Could not enter data: ' . mysqli_sqlstate($this->db));
        }
        $this->insert_row = array();
    }

}

答案 3 :(得分:0)

你的数组是:

[0]=> Array (
    [Dest] => 100
    [Priority] => 1
)
[1]=> Array (
    [Dest] => 200 
    [Priority] => 3 
) 
[2] => (
)

所以它是一个多维数组。如果您需要插入所有这些条目,则不应为同一事件运行多个查询。只需使用mysql批量插入语法。 (例如INSERT INTO tbl (col1,col2,col3) VALUES(a,b,c),(d,e,f),(g,h,i)

为insert创建查询字符串。

foreach($a as $i => $v)
{
   $b[] = '("'.$v['Dest'].'","'.$v['Priority'].'")';
}

$c = implode(',', $b);

$sql = "INSERT INTO cc_did_destination (destination,priority) 
          VALUES ".$c;

然后运行查询

N.B。

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial