I am trying to get id from stores and insert into items table with prepare stmt.
was easy in mysql, but dont know how to do it in stmt.
I did 2 querries separetly but didnt work.
my first querry for inserting rows to table
if(empty($title_err) && empty($details_err) && empty($image_err) && empty($cat_id_err)){
// Prepare an update statement
$sql = "INSERT INTO adverts (cat_id, store_id, user_id, title, details, image, seo_url, hits, date, active) VALUES (?, ?, ?, ?, ?, ?, ?, ?,? ,?)";
if($stmt = $conn->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("iiissssiss", $param_cat_id, $param_store_id, $param_user_id, $param_title, $param_details, $param_image, $param_seo_url, $param_hits, $param_date, $param_active);
// Set parameters
$param_cat_id = htmlspecialchars($cat_id);
$param_store_id = htmlspecialchars($store_id);
$param_user_id = htmlspecialchars($_SESSION['id']);
$param_title = htmlspecialchars($title);
$param_details = htmlspecialchars($details);
$param_image = htmlspecialchars($filedestination);
$param_seo_url = seo_url($title);
$param_hits = htmlspecialchars(1);
$param_date = htmlspecialchars($date);
$param_active = htmlspecialchars(Y);
// Attempt to execute the prepared statement
if($stmt->execute()){
// Records updated successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
$stmt->close();
}
// Close connection
$conn->close();
}
And this is the second one for select from stores (it doesnt list rows).
// Prepare a select statementwhere userid=?
$sql = "SELECT * FROM stores WHERE store_id=? ORDER BY store_id DESC";
if($stmt = $conn->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("i", $param_store_id);
if($stmt->execute()){
$result = $stmt->get_result();
if($result->num_rows == 1){
$row = $result->fetch_array(MYSQLI_ASSOC);
// Retrieve individual field value
$store_id = $row["store_id"];
$store_name = $row["store_name"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
$stmt->close();
// Close connection
$conn->close();
infos coming from a form
example
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>