duanqian6295 2018-06-10 15:10
浏览 188
已采纳

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

I have to dynamically take the input from the user and insert it into a database. However, when I execute the query from PHP, it inserts "Array" in the column.

 <?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条回答 默认 最新

  • doufei9946 2018-06-10 15:22
    关注

    When you run...

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

    $row is retrieved in...

    $row = mysqli_fetch_assoc($result);
    

    and mysqli_fetch_assoc() returns an array of the fields in the result set. So for your first value in your second bind, you need to fetch the column that is the value in the result set your after...

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

    The only thing is that the select is selecting by the state_name, so not sure how this would be any different to...

    mysqli_stmt_bind_param($stmt, "ss", $name ,$cityname);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题