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 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突