ian
ian

Reputation: 49

PDO prepared statements PHP issue

Part of this prepared insert statement works, the other does not and I cannot see why because I am not getting an error.

I tried the different options, even mysqli just not winning. PDO does contain the statement $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION )

try {
    $dbh = new PDO("mysql:host=localhost;dbname=project","root","");
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    echo "Connected successfully";

    if ( $_SERVER['REQUEST_METHOD'] == 'POST' &&  isset($_POST['submit']));

    if(
        !empty($_POST['skill_level_1'])
        && !empty($_POST['skill_level_2'])
        && !empty($_POST['skill_level_3'])

        && isset($_POST['skill_1'])
        && isset($_POST['skill_experience_1'])
        && isset($_POST['skill_2'])
        && isset($_POST['skill_experience_2'])
        && isset($_POST['skill_3'])
        && isset($_POST['skill_experience_3'])

    ){

        foreach($_POST['skill_level_1'] as $selected) {
            $GLOBALS['skill_level_1'] = $selected;
        }
        foreach($_POST['skill_level_2'] as $selected) {
            $GLOBALS['skill_level_2'] = $selected;
        }
        foreach($_POST['skill_level_3'] as $selected) {
            $GLOBALS['skill_level_3'] = $selected;
        }
        $skill_1 = strip_tags(trim($_POST['skill_1']));
        $skill_experience_1 = strip_tags(trim($_POST['skill_experience_1']));
        $skill_2 = strip_tags(trim($_POST['skill_2']));
        $skill_experience_2 = strip_tags(trim($_POST['skill_experience_2']));
        $skill_3 = strip_tags(trim($_POST['skill_3']));
        $skill_experience_3 = strip_tags(trim($_POST['skill_experience_3']));

        $stmt = $dbh ->prepare("INSERT INTO dbtable
(
skill_level_1, skill_level_2, skill_level_3,
skill_1, skill_experience_1, 
skill_2, skill_experience_2, 
skill_3, skill_experience_3
)VALUES(
:skill_level_1, :skill_level_2, :skill_level_3,
:skill_1, :skill_experience_1, 
:skill_2, :skill_experience_2, 
:skill_3, :skill_experience_3
)");

// $stmt->execute(array(':skill_level_1' => $skill_level_1, ':skill_level_2' => $skill_level_2, ':skill_level_3' => $skill_level_3));

        $stmt->bindParam(':skill_level_1',$skill_level_1);
        $stmt->bindParam(':skill_level_2',$skill_level_2);
        $stmt->bindParam(':skill_level_3',$skill_level_3);

        $stmt->bindParam(':skill_1',$skill_1);
        $stmt->bindParam(':skill_experience_1',$skill_experience_1);

        $stmt->bindParam(':skill_2',$skill_2);
        $stmt->bindParam(':skill_experience_2',$skill_experience_2);

        $stmt->bindParam(':skill_3',$skill_3);
        $stmt->bindParam(':skill_experience_3',$skill_experience_3);

        $stmt->execute();

        echo "Success";

    }//end if

}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage();
}

So it works for the part skill_level_1, skill_level_2, skill_level_3 which are check boxes. But the input boxes do not work. I thought that taking away strip tags might make a difference but it does not. I tried a mysqli connection and statement and it does not work either.

Upvotes: 0

Views: 51

Answers (1)

ian
ian

Reputation: 49

Thanks Guys I found the issue. I had a capital letter in the HTML name for the input field.

I also found it helped to order the columns in the database to match the order for the PHP being written to it. This enabled me to then go back and double check the HTML.

Upvotes: 0

Related Questions