Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在mysql表中插入关联数组_Php_Mysql - Fatal编程技术网

Php 在mysql表中插入关联数组

Php 在mysql表中插入关联数组,php,mysql,Php,Mysql,我有一个包含名称和id的数组,我想将这些东西插入表中,但我只能插入11行中的4行我不知道我在哪里犯了错误 这是我的阵列: Array ( [0] => Array ( [id] => 6143310130 [name] => ) [1] => Array ( [id] => 3310773561 [name] => Arau ) [2] => Array ( [id] => 1127328875 [name] =>

我有一个包含名称和id的数组,我想将这些东西插入表中,但我只能插入11行中的4行

我不知道我在哪里犯了错误

这是我的阵列:

Array (
    [0] => Array ( [id] => 6143310130 [name] => ) 
    [1] => Array ( [id] => 3310773561 [name] => Arau ) 
    [2] => Array ( [id] => 1127328875 [name] => Womens  )
    [3] => Array ( [id] => 9847224704 [name] => shoulder ) 
    [4] => Array ( [id] => 7099570428 [name] => Swapna )
    [5] => Array ( [id] => 5263908918 [name] => Top Hospitals India )
    [6] => Array ( [id] => 7504483424 [name] => Wodsjls ) 
    [7] => Array ( [id] => 2051860883 [name] => Star ) 
    [8] => Array ( [id] => 4582742687 [name] => Manipal chand ) 
    [9] => Array ( [id] => 1024875457 [name] => lokpal ) 
    [10] => Array ( [id] => 7120814620 [name] => Shiney ) 
    [11] => Array ( [id] => 8367976928 [name] => suma )
)
我的sql查询如下:

foreach ($ret as  $key) {   
    if(mysqli_query($con,"insert ignore INTO AdsAccount(accountID,accountName) VALUES ('$key[id]','$key[name]')")) {
        echo "success";
    }   
}

我猜您的SQL语法中有一个输入错误。在变量中定义SQL查询字符串,如下所示,不带“ignore”(ignore语句抑制错误):

使用循环为插入创建查询:

$i = 0;
$sql = 'insert INTO AdsAccount(accountID,accountName) VALUES';
foreach($array as $key) {
    $sql .= '('.$key['id'].','.$key['name'].')';
    if($i < count($array) - 1) {
        $sql .= ",\n";
    }
    $i++;
}
$sql .= ';';
mysqli_query($con, $sql);

有关详细说明,请访问:

您在表中使用的是主键还是唯一键?如果可能,请添加您的表架构。主键作为帐户id请检查您试图插入的任何帐户id是否已经存在。否,它在表中的任何位置都不存在,即为什么我将insert ingnore作为查询放入adsaccount主键(id)字段的长度是多少?试着跟着做,这样每个试图帮助你的人就不必通过冗长的问卷调查来找出有意义的东西。使用准备好的陈述,该死。你是对的,准备好的陈述更好!但这并不是实际问题的答案。关于准备好的语句的更多信息:虽然准备好的语句很少是问题的焦点,但是querystring构建几乎从来都不是最佳/正确的使用途径,并且显示这种代码会在PHP新手之间重复错误的用法。请再多走一英里,把片段整理一下。我很乐意用事先准备好的陈述来补充这个答案。
$i = 0;
$sql = 'insert INTO AdsAccount(accountID,accountName) VALUES';
foreach($array as $key) {
    $sql .= '('.$key['id'].','.$key['name'].')';
    if($i < count($array) - 1) {
        $sql .= ",\n";
    }
    $i++;
}
$sql .= ';';
mysqli_query($con, $sql);
$dbh = new PDO('mysql:host=<dbhost>;dbname=<dbname>', <dbuser>, <dbuserpass>);
$stmt = $dbh->prepare("INSERT INTO AdsAccount (accountID , accountName ) VALUES (:accountID, :accountName)");

// $id and $name work as references
$stmt->bindParam(':accountID',  $id, PDO::PARAM_INT); 
$stmt->bindParam(':accountName', $name, PDO::PARAM_STR);

foreach($array as $key) {
    $id = $key['id']; //set the reference for id
    $name = $key['name']; //set the reference for name
    $stmt->execute(); //execute the statement
}