【发布时间】:2019-09-14 14:05:15
【问题描述】:
无法将数据插入表中
这是一个必须插入到我的数据库中的文件示例。文件通过简单的表单上传到服务器。
{
“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 表中。可能我在脚本的逻辑上做错了,但不明白在哪里。谁能帮帮我吗。谢谢。
【问题讨论】:
-
您的 sql 完全错过了准备好的语句的要点,并且可能存在漏洞。
-
那么sql部分应该怎么写呢?谢谢
-
$row["address"]应该是$row["ADDRESS"]? -
您的 JSON 使用
”而不是"作为引号。除此之外,您还有一些关于使 SQL 错误可见的作业,以便您了解服务器的响应。