gpt4 book ai didi

php - 数组到字符串转换 "Array"存储到数据库

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

我必须动态地获取用户的输入并将其插入数据库。但是,当我从 PHP 执行查询时,它会在列中插入“Array”。

 <?php
include 'connect.php';

if (isset($_POST['submitCity']))
{
$name = $_POST['state'];
$select = "SELECT state_name FROM state WHERE state_name = ?";
$selectstmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($selectstmt, $select))
{
echo "SQL ERROR OCCURED";
}
else
{
mysqli_stmt_bind_param($selectstmt, "s", $name);
mysqli_stmt_execute($selectstmt);
$result = mysqli_stmt_get_result($selectstmt);
$row = mysqli_fetch_assoc($result);
}


$cityname = mysqli_real_escape_string($conn,$_POST['city']);
$sql = "INSERT INTO city (state_name, city_name) VALUES (?,?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql))
{
echo "SQL ERROR OCCURED";
}
else
{
mysqli_stmt_bind_param($stmt, "ss", $row ,$cityname);
mysqli_stmt_execute($stmt);
}
}

?>

最佳答案

当你运行时...

mysqli_stmt_bind_param($stmt, "ss", $row ,$cityname);

$row 检索于...

$row = mysqli_fetch_assoc($result);

mysqli_fetch_assoc()返回结果集中的字段数组。因此,对于第二次绑定(bind)中的第一个值,您需要获取之后结果集中的值的列...

mysqli_stmt_bind_param($stmt, "ss", $row['state_name'] ,$cityname);

唯一的事情是选择是按 state_name 选择的,所以不确定这与...有何不同

mysqli_stmt_bind_param($stmt, "ss", $name ,$cityname);

关于php - 数组到字符串转换 "Array"存储到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50785065/

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