insert a foreign key using prepared statement

i want to insert with foreign key resident_id using prepared statement in table_complaints.

here is my picture of :

enter image description here

also i get the $ides = $_POST["resident_id"]; in view page

enter image description here

$servername = "localhost";
$username = "root";
$password = "";
$database = "myDb";

$conn = mysqli_connect($servername, $username, $password, $database);
if(!$conn){
    die("Connection Failed: " . mysqli_connect_error());
}

if(isset($_POST["submits"])){
    $comp_text = $_POST["comp"];
    $complaints = $_POST["complaints"];
    $ides =$_POST["resident_id"];

    $statementi = mysqli_stmt_init($conn);
    mysqli_stmt_prepare($statementi, "INSERT INTO table_complaint (nature_of_complaints, status) 
     VALUES (?, ?) WHERE resident_id = ?");
    mysqli_stmt_bind_param($statementi, "ssi", $comp_text, $complaints);

    mysqli_stmt_execute($statementi);

    mysqli_stmt_close($statementi);
}
 mysqli_close($conn);

1 answer

  • answered 2018-02-13 01:57 Miggy

    Your insert query is incorrect.

    You can use this:

    INSERT INTO table_complaint (resident_id,nature_of_complaints, status) VALUES (?,?,?)
    

    and then bind the parameters:

    mysqli_stmt_bind_param($statementi, "iss", $ides,$comp_text, $complaints);