0

私は PDO を使用していますが、ここで未定義の変数のエラーと混同しています:

これは、データベース接続直後の変数 cat_name に関するものです。

変数に値を直接追加しました。エラーを次のように変更しました。

SQLSTATE[42000]: 構文エラーまたはアクセス違反: 1064 SQL 構文にエラーがあります。MySQL サーバーのバージョンに対応するマニュアルで、1 行目の ''categories'('cat_name', 'cat_description') VALUES ('ohnoe','ohnoes')' 付近で使用する正しい構文を確認してください。

  <?php
    try{
    include 'database.php';
    $db =  database:: getConnection();
        $catName = "ohnoe";
        $catDesc = "ohnoes";
    if(isset($_POST['submit'])){
    if(isset($_POST['cat_name'])){
        $catName = $_POST['cat_name'];
    }
    else{
        echo "already set";
    }

    if(isset($_POST['cat_description'])){
    echo"Oh dear";
    }
    else{
    $catDesc = $_POST['cat_description'];
    }}
    $sqlQuery = ("INSERT INTO 'categories'('cat_name', 'cat_description') VALUES (:cat_name,:cat_description)");

    $statement = $db->prepare($sqlQuery);
    $statement->bindValue(":cat_name",$catName);
    $statement->bindValue(":cat_description",$catDesc);

    $count = $statement->execute();

    $db = null;
    }catch(PDOException $e){
    echo $e->getMessage();
    }
    ?>
4

1 に答える 1

0

Are you sure it is "undefined variable", not an "undefined index"? If you run this script in both case render form and process data, then you have to check the request type by just putting if ($_POST) or more specific like if (isset($_POST['cat_name']) && isset($_POST['cat_description']) before any data processing.

于 2013-02-27T23:44:14.120 に答える