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

时间:2018-06-10 15:10:22

标签: php mysql

我必须动态地从用户那里获取输入并将其插入到数据库中。但是,当我从PHP执行查询时,它会插入"数组"在专栏中。

 <?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);
}
}

?>

1 个答案:

答案 0 :(得分:1)

当你跑...

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

$row在...中检索

$row = mysqli_fetch_assoc($result);

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

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

唯一的一点就是选择是由state_name选择的,所以不确定这与...有什么不同......

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