1

What I'm trying to do is:

If the age input in my form = 28, 30, 25 or 21 then I want to auto insert value 8 in the column (VE), else keep it empty. Is this the right way to do that?

if($form_data->action == 'Insert')
        {

            $age=array(28, 30, 25, 21);
            $age_str=implode("','", $age);

                if($form_data->age == $age_str){

                $query="INSERT INTO tbl
                        (VE) VALUE ('8') WHERE id= '".$form_data->id."'
                ";
                    $statement = $connect->prepare($query);
                    $statement->execute();
            }
            $data = array(
                ':date'             =>  $date,
                ':first_name'       =>  $first_name,
                ':last_name'        =>  $last_name,
                ':age'          =>  $age
            );
            $query = "
            INSERT INTO tbl
                (date, first_name, last_name, age) VALUES 
                (:date, :first_name, :last_name, :age)
            ";
            $statement = $connect->prepare($query);


            if($statement->execute($data))
            {
                $message = 'Data Inserted';
            }
        }


Also, how do I insert the new row with the row id from the other form data going into tbl?

jkdev
  • 11,360
  • 15
  • 54
  • 77
Max
  • 41
  • 6
  • 1
    as an aside...I think this `$data = array(` needs to use values from `$form_data`, for instance `$date` is not declared...you probably want `$form_data->date`, and the same fire the other values. – WEBjuju Nov 17 '19 at 18:57
  • 1
    nope , i've declared them somewhere else (this isn't my full code ) and everything works fine exept what i asked for , and thank you for your answer , i hope that it will work with your method , i'm now in the train so i'll try your code when i get home , thank you :) – Max Nov 17 '19 at 19:04
  • `$form_data->age == $age_str` given what you just set `$age_str` to, why would this ever be true? Also your first SQL query is unsafe and contains syntax errors, which PHP should be notifying you about once you get it to run. – miken32 Nov 18 '19 at 03:19

1 Answers1

1

Use php's in_array instead of trying to compare a string. To get the id of the query where you insert the form data, you can return the id of the insert row from your prepared statement.

if ($form_data->action == 'Insert') {

    // assuming $age, $date, $first_name, $last_name
    // already declared prior to this block
    $data = array(
      ':date'             =>  $date,
      ':first_name'       =>  $first_name,
      ':last_name'        =>  $last_name,
      ':age'          =>  $age
    );

    $query = "
      INSERT INTO tbl
        (date, first_name, last_name, age) VALUES 
        (:date, :first_name, :last_name, :age)
    ";
    $statement = $connect->prepare($query);

    if ($statement->execute($data)) {
        $message = 'Data Inserted';

        // $id is the last inserted id for (tbl)
        $id = $connect->lastInsertID();

        // NOW you can insert your child row in the other table
        $ages_to_insert = array(28, 30, 25, 21);

        // in_array uses your array...so you don't need
        // if($form_data->age == $age_str){

        if (in_array($form_data->age, $ages_to_insert)) {

            $query="UPDATE tbl SER VE = '8' WHERE id= '".$id."'";
            $statement2 = $connect->prepare($query);
            $statement2->execute();
        }
    }
}
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • i'm getting this error now Notice: Undefined property: stdClass::$id on line 197 ///// which is this line : $query="INSERT INTO tbl (VE) VALUE ('8') WHERE id= '".$form_data->id."' "; so if i remove the condition it works put the value won't be inserted in the same row with the other form data, any advoice ?? – Max Nov 17 '19 at 22:07
  • but you said i've to use if in array instead of if($form_data->age == $age_str){ ? – Max Nov 17 '19 at 22:24
  • 1
    they aren't related. the mysql insert id for your second query isn't available until after you run the second query...so make the second query the first query and only run the second if the first succeeds. see updated post. – WEBjuju Nov 17 '19 at 22:30
  • 1
    thank you from my heart :) , it worked but i had to update something , first one if you want to get the last inserted ID in pdo you should use $id = $connect->lastInsertID(); , SECOND : i had to use UPDATE instead of INSERT to get it work . thank you for your help :)) – Max Nov 17 '19 at 22:59