LoginSignup
tohoku86
@tohoku86

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

PHP My SQL 初心者

解決したいこと

DB勉強中です。UPDATW文とINSERT文で詰まっています

発生している問題・エラー

Invalid parameter number: number of bound variables does not match number of tokens

該当するソースコード

if($edit_flg){
                debug('DB更新です。');
                $sql = 'UPDATE product SET name = :name, category_id = :category, price = :price, comment = :comment, pic1 = :pic1, pic2 = :pic2, pic3 = :pic3 WHERE user_id = :u_id AND id = :p_id';
                $data = array(':name' => $name, ':category' => $category_id, ':price' => $price, ':comment' => $comment, ':pic1' => $pic1, ':pic2' => $pic2, ':pic3' => $pic3, ':u_id' => $_SESSION[$user_id], ':p_id' => $p_id);
            }else{
                debug('DB新規登録です。');
                $sql = 'INSERT INTO product (name, category_id, price, comment, pic1, pic2, pic3, user_id, create_date) VALUES (:name, :category, :price, :comment, :pic1, pic2, :pic3, :u_id, :date)';
                $data = array(':name' => $name , ':category' => $category, ':price'=> $price, ':comment' => $comment, ':pic1'=> $pic1, ':pic2' => $pic2, ':pic3' => $pic3, ':u_id' => $_SESSION['user_id'], ':date' => date('Y-m-d H:i:s'));
            }

自分で試したこと

phpMyadminに直打ちしてもエラーなので、ここのどれかがおかしいと思っていはいるのですが、検索しても分かりません。エラー内容がInvalid parameter number: number of bound variables does not match number of tokensで無効なパラメーター番号:バインドされた変数の数がトークンの数と一致しません と出てるので、何かが一致してないと思われるのですが、私にはこれ以上分かりませんでした。何かヒントいただけると嬉しいです。よろしくお願いいたします。

0

3Answer

INSERT文のVALUES句ですが:pic1, pic2, :pic3のpic2にコロンが抜けているので一致しないのだと思います。

1Like

ほんとですね。。修正しましたが、
Integrity constraint violation: 1048 Column 'user_id' cannot be null

という違うエラーが発生してしまいました。。

0Like

Your answer might help someone💌