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 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题