首页 > 解决方案 > 不好:[php] foreach?

问题描述

无法将数据插入表中

这是必须插入到我的数据库中的文件的示例。文件以简单的形式上传到服务器。

{
    “FileName”: “XXXX",
    “Code”: “11112233",
    “Contacts”: [
        {
        “rowId” => '',
        “TicketId” => "xxxxxxxxxxxx",
        “otherId” => "YYYYYYYYYYYYYYYYYYYYYYY",
        “ClientId” => "wwwwwwwwwwwwwwwwwwwwwwwwwww",
        “Name” => "MARCELLO",
        “LName” => "MARCELLO",
        “Phone” => "4315415151434",
        “ADDRESS” => "hhhhhvofvofvvv",
        “Mail” => "dfwfwf@fwes.fd"
        },
        {
        “rowId” => '',
        “TicketId” => "xxxxxxxxxxxx",
        “otherId” => "YYYYYYYYYYYYYYYYYYYYYYY",
        “ClientId” => "wwwwwwwwwwwwwwwwwwwwwwwwwww",
        “Name” => "MARCELLO",
        “LName” => "MARCELLO",
        “Phone” => "4315415151434",
        “ADDRESS” => "hhhhhvofvofvvv",
        “Mail” => "dfwfwf@fwes.fd"
        }
    ]
}

在主页中,我包含连接到数据库的脚本,我确信它可以正常工作,通常用于我的网络工作的所有其他页面。

$host = "localhost";
$db_user = "xxxx";
$db_pw = "xxxxx";
$db_name = "xxxxx";
// connessione
try {
  // stringa di connessione al DBMS
  $connessione = new PDO("mysql:host=$host;dbname=$db_name", $db_user, $db_pw);
  // impostazione dell'attributo per il report degli errori
  $connessione->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

catch(PDOException $e)
{
  // notifica in caso di errore nel tentativo di connessione
  echo "Errore:" .$e->getMessage();
  die();
}   

这是我的上传页面代码的一部分,它对我没有魔力:

$file = file_get_contents($filejson);
if(!function_exists('json_decode')) die('Il server non ha tale funzione');
$result = json_decode($file, true);
                foreach ($result as $row){
                        $sql = "INSERT INTO ibl_Anag (TicketId,otherId,ClientId,Name,LName,MobPhone,Phone,address,mail)
                        VALUES ('".$row["TicketId"]."'
                                ,'".$row["otherId"]."'
                                ,'".$row["ClientId"]."'
                                ,'".$row["Name"]."'
                                ,'".$row["LName"]."'
                                ,'".$row["Phone"]."'
                                ,'".$row["MainPhone"]."'
                                ,'".$row["address"]."'
                                ,'".$row["mail"]."' )";
                        $stmt=$connessione->prepare($sql);
                        $stmt->execute();
                        if(!$stmt){Echo "la insert non ha funzionato";}


    }

我没有从代码中得到错误,但数据没有插入到 mysql 表中。可能我在脚本的逻辑上做错了,但不明白在哪里。谁能帮帮我吗。谢谢。

标签: phpmysqljsonpdo

解决方案


您需要考虑以下几点:

  • 使用参数化准备好的语句。这将帮助您防止可能的 SQL 注入和来自错误用户输入的错误。在您的语句中分配占位符 ( ?),准备一次该语句,将值绑定到您的参数并多次执行该语句。
  • JSON的无效(如问题中所述)。使用从中JSON读取的有效"Contacts"数组JSON来执行您的语句。此外,"MainPhone"您的JSON"address"is中没有"ADDRESS"

您可以尝试使用以下代码:

<?php
# Connection
$host    = "localhost";
$db_user = "xxxx";
$db_pw   = "xxxxx";
$db_name = "xxxxx";
try {
    $connessione = new PDO("mysql:host=$host;dbname=$db_name", $db_user, $db_pw);
    $connessione->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "Errore:" .$e->getMessage();
    die();
}  

# Data
$file = file_get_contents($filejson);
if (!function_exists('json_decode')) {
    die('Il server non ha tale funzione');
}   
$result = json_decode($file, true);

# Prepare and execute statement
try {
    $sql = 
        "INSERT INTO ibl_Anag (TicketId,otherId,ClientId,Name,LName,MobPhone,Phone,address,mail)
        VALUES (?,?,?,?,?,?,?,?,?)";
    $stmt = $connessione->prepare($sql);
    foreach ($result["Contacts"] as $row) {
        $ticketId = $row["TicketId"];
        $otherId  = $row["otherId"];
        $clientId = $row["ClientId"];
        $name     = $row["Name"];
        $lname    = $row["LName"];
        $mobPhone = $row["Phone"];
        $phone    = $row["Phone"];
        $address  = $row["ADDRESS"];
        $mail     = $row["mail"];
        $stmt->BindParam(1, $ticketId);
        $stmt->BindParam(2, $otherId); 
        $stmt->BindParam(3, $clientId);
        $stmt->BindParam(4, $name);    
        $stmt->BindParam(5, $lname);   
        $stmt->BindParam(6, $mobPhone);
        $stmt->BindParam(7, $phone);   
        $stmt->BindParam(8, $address);
        $stmt->BindParam(9, $mail);    
        if (!$stmt->execute()) {
            echo "la insert non ha funzionato";
        }
    }                       
} catch(PDOException $e) {
    echo "Errore:" .$e->getMessage();
    die();
}  
?>

有效的 JSON 示例(基于问题中的示例数据):

{
    "FileName": "XXXX",
    "Code": "11112233",
    "Contacts": [
        {
        "rowId": "",
        "TicketId": "xxxxxxxxxxxx",
        "otherId": "YYYYYYYYYYYYYYYYYYYYYYY",
        "ClientId": "wwwwwwwwwwwwwwwwwwwwwwwwwww",
        "Name": "MARCELLO",
        "LName": "MARCELLO",
        "Phone": "4315415151434",
        "ADDRESS": "hhhhhvofvofvvv",
        "Mail": "dfwfwf@fwes.fd"
        },
        {
        "rowId": "",
        "TicketId": "xxxxxxxxxxxx",
        "otherId": "YYYYYYYYYYYYYYYYYYYYYYY",
        "ClientId": "wwwwwwwwwwwwwwwwwwwwwwwwwww",
        "Name": "MARCELLO",
        "LName": "MARCELLO",
        "Phone": "4315415151434",
        "ADDRESS": "hhhhhvofvofvvv",
        "Mail": "dfwfwf@fwes.fd"
        }
    ]
}

推荐阅读