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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失