gpt4 book ai didi

使用/cURL 将数据发布到数据库时的 PHP/SQL 问题

转载 作者:行者123 更新时间:2023-11-29 21:49:39 27 4
gpt4 key购买 nike

下面您将找到代码。

我试图让它检索 JSON 数据,并将值发布到我的数据库。

JSON 结构以及输出顺序...

[{
"record_locator": "value",
"status": "value",
"area": "value",
"region": "value",
"name": "value",
"position":"value",
"phone_": "value",
"project_manager": "value",
"ipad_email": "value",
"email": "value",
"team_leader": "value",
"regional": "value",
"title": "value",
"start_date": "value",
"employee_id_number": "value",
}]

为了方便眼睛...

$VAR = 我的表 key = JSON key

$area   =area   =area
$email =email =email
$ipad_email =ipad_email =ipad_email
$empid =empid =employee_id_number
$fullname =fullname =name
$manager =manager =If Clause
$mobilenumber =mobilenumber =phone_
$record_locator =record =record_locator
$status =status =status
$region =region =region
$position =position =position
$project_manager =project_manager =project_manager
$team_leader =team_leader =team_leader
$regional =regional =regional
$title =title =title
$startdate =startdate =startdate

我已经对该值进行了简单的回显测试,他们正在抓取它,只是由于某种原因它没有发布到数据库。可悲的是我无法弄清楚。

任何帮助将不胜感激。 这不起作用。

<html>
<head>
<style>
td {
text-align:center;
border: 1px solid black;
}
</style>
</head>
<body>
<div style="margin-top:5%; width:300px; margin:auto; border: 1px solid black; padding:3px;"><center><p id="container">Loading...</p></div>
<?php

$error_msg = "";
$url="someurl.com";

$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$json = curl_exec($ch);
curl_close($ch);
$rdata = json_decode($json, TRUE);

$c=0;
$repverified = 0;

foreach ($rdata as $data) {

// Assigned key JSON KEY
$user_rl = $rdata[$c]['record_locator'];
$status = $rdata[$c]['status'];
$area = $rdata[$c]['area'];
$region = $rdata[$c]['region'];
$fullname = $rdata[$c]['name'];
$position = $rdata[$c]['position'];
$mobilenumber = $rdata[$c]['phone_'];
$project_manager = $rdata[$c]['project_manager'];
$ipad_email = $rdata[$c]['ipad_email'];
$email = $rdata[$c]['email'];
$team_leader = $rdata[$c]['team_leader'];
$regional = $rdata[$c]['regional'];
$title = $rdata[$c]['title'];
$startdate = $rdata[$c]['start_date'];
$empid = $rdata[$c]['employee_id_number'];


if(
$fullname == $rdata[$c]['project_manager'] ||
$fullname == $rdata[$c]['team_leader'] ||
$fullname == $rdata[$c]['regional'] ||
$fullname == $rdata[$c]['director'])
{
$manager = 'YES';
}
else {
$manager = 'NO';
}

// Insert the new user into the database
$sql = "
INSERT INTO memberss (
record,
status,
area,
region,
fullname,
manager,
position,
mobilenumber,
project_manager,
ipad_email,
email,
team_leader,
regional,
title,
startdate,
empid

)
VALUES (
?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

$insert_stmt = $mysqli->prepare($sql);

// Insert the new user into the database
if ($insert_stmt)
{
$insert_stmt->bind_param('ssssssssssssssss',$user_rl,$status,$area,
$region,$fullname,$manager,$position,$mobilenumber,
$project_manager,$ipad_email,$email,$team_leader,
$regional,$title,$startdate,$empid);

// Execute the prepared query.
if (! $insert_stmt->execute()) {
header('Location: ../error.php?err=Registration failure: INSERT');
}
}
}

echo '<div>Check to ensure we are grabbing the values</div>';
echo '<td>'. $user_rl .'</td><br>';
echo '<td>'. $status .'</td><br>';
echo '<td>'. $area .'</td><br>';
echo '<td>'. $region .'</td><br>';
echo '<td>'. $fullname .'</td><br>';
echo '<td>'. $manager .'</td><br>';
echo '<td>'. $position .'</td><br>';
echo '<td>'. $mobilenumber .'</td><br>';
echo '<td>'. $project_manager .'</td><br>';
echo '<td>'. $ipad_email .'</td><br>';
echo '<td>'. $email .'</td><br>';
echo '<td>'. $team_leader .'</td><br>';
echo '<td>'. $regional .'</td><br>';
echo '<td>'. $title .'</td><br>';
echo '<td>'. $startdate .'</td><br>';
echo '<td>'. $empid .'</td><br>';


$c++;


echo "<script>document.getElementById('container').innerHTML = 'Registration Completed!<br><b> Active Users will now be visible</b>';</script>";
?>

我什至尝试更改我的 if 语句和 bind_params 但仍然没有运气

if (! $insert_stmt->execute()) {
header('Location: ../error.php?err=Registration failure: INSERT');
}
}
} else {
// Insert the new user into the database
if ($insert_stmt = $mysqli->prepare("INSERT INTO members (email, password, status, empid, name, area, manager, record) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) {
$insert_stmt->bind_param('ssssssss', $email, $repEmail, $status, $employeeID, $FullName, $area, $manager, $record_locator);
// Execute the prepared query.
if (! $insert_stmt->execute()) {
header('Location: ../error.php?err=Registration failure: INSERT');
}
}

添加了一个回显来仔细检查我们正在获取的值。成功!我们是!

echo“检查以确保我们正在获取值”;

echo    '<td>'. $user_rl    .'</td><br>';
echo '<td>'. $status .'</td><br>';
echo '<td>'. $area .'</td><br>';
echo '<td>'. $region .'</td><br>';
echo '<td>'. $fullname .'</td><br>';
echo '<td>'. $manager .'</td><br>';
echo '<td>'. $position .'</td><br>';
echo '<td>'. $mobilenumber .'</td><br>';
echo '<td>'. $project_manager .'</td><br>';
echo '<td>'. $ipad_email .'</td><br>';
echo '<td>'. $email .'</td><br>';
echo '<td>'. $team_leader .'</td><br>';
echo '<td>'. $regional .'</td><br>';
echo '<td>'. $title .'</td><br>';
echo '<td>'. $startdate .'</td><br>';
echo '<td>'. $empid .'</td><br>';

数据库方案

CREATE TABLE IF NOT EXISTS `memberss` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`empid` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
`record` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`area` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`region` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`fullname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`position` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`mobilenumber` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`project_manager` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`ipad_email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`regional` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`startdate` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

它是两件事之一;我的数据库表设置不正确,或者我的主体脚本运行不正确。

我没有收到任何错误。即使当我试图发现发布错误时也是如此。这导致我的表设置不正确??

感谢您提供的所有帮助。

附注对于机智的人来说;是的,我有我的数据库连接文件,只是没有将其包含在代码正文中

最佳答案

我已经弄清楚我做错了什么。

我的表现还不够

值 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

我也没有绑定(bind)足够的字符串。

$insert_stmt->bind_param('ssssssssssssssss' 匹配我的

if ($insert_stmt = $mysqli->prepare("INSERT INTO 成员(值)"))

关于使用/cURL 将数据发布到数据库时的 PHP/SQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33767519/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com