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