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

xsuvu9jc  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(410)

下面是用于将记录插入数据库的代码。脚本正在执行,因为记录没有插入到数据库中,但是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();
?>
dy1byipe

dy1byipe1#

似乎$valuesarr数组元素没有正确连接
您应该使用连接运算符“.”将php变量与文本分开,如下所示:

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

相关问题