gpt4 book ai didi

将数据插入数据库的 PHP 脚本在执行时返回成功,但记录未插入数据库?

转载 作者:行者123 更新时间:2023-11-29 10:05:41 25 4
gpt4 key购买 nike

强文本下面是用于将记录插入数据库的代码。脚本正在执行,因为记录没有插入数据库,但 php 脚本正在执行,没有任何失败。请帮我解决这个问题。

<?php
$servername = "xxx.xx.x.xxx";
$username = "usr";
$password = "pwd";
$dbname = "Dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$postdata = json_decode(file_get_contents('php://input'), true);
$tablename = "tablename";
function guidv4($data)
{
assert(strlen($data) == 16);

$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
if(is_array($postdata)){

$sql = "INSERT INTO ".$tablename." (id, ssid, address, lat, lng, state, country, city, cat, subcat, sname, createdAt,pincode) VALUES ";

$valuesArr = array();
foreach($postdata as $row){
//$loc = explode(", ", $row['loc']);
//$lat = mysqli_real_escape_string($conn, $loc[0]);
//$lng = mysqli_real_escape_string($conn, $loc[1]);
$id = guidv4(random_bytes(16));
//$id = mysqli_real_escape_string($conn, $row['id']);
$ssid = mysqli_real_escape_string($conn, $row['sname']);
$address = mysqli_real_escape_string($conn, $row['address']);
$lat = mysqli_real_escape_string($conn, $row['lat']);
$lng = mysqli_real_escape_string($conn, $row['lng']);
$state = mysqli_real_escape_string($conn, $row['state']);
$country = mysqli_real_escape_string($conn, "India");
$city = mysqli_real_escape_string($conn, $row['city']);
$cat = mysqli_real_escape_string($conn, $row['cat']);
$subcat = mysqli_real_escape_string($conn, $row['subcat']);
$sname = mysqli_real_escape_string($conn, $row['sname']);
$createdAt = mysqli_real_escape_string($conn, $row['createdAt']);
$pincode = mysqli_real_escape_string($conn, $row['pincode']);
$valuesArr[] = "('$id', '$ssid', '$address', '$lat', '$lng', '$state', '$country', '$city', '$cat', '$subcat', '$sname', '$createdAt','pincode')";


}

$sql .= implode(',', $valuesArr);

$conn->query($sql) or exit(mysql_error());
}



$conn->close();
?>

最佳答案

$valuesArr 数组元素似乎没有正确连接

您应该使用连接运算符“.”将 PHP 变量与文本分开,如下所示:

  $valuesArr[] = "('" . $id ."', '" .$ssid . "', '" . $address ."', '" . $lat ."', '" . $lng . "', '" $state . "', '" . $country . "', '" . $city . "', '" . $cat . "', '" . $subcat ."', '" . $sname . "', '" . $createdAt . "','pincode')";

关于将数据插入数据库的 PHP 脚本在执行时返回成功,但记录未插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51954873/

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