2011-09-12 15 views
1

私はこの問題について聞いたことがありますが、それを理解できないようです。私はデータベースとテーブル名が正しいです。私は何かエラーを見つけることはできませんし、phpmyadminにテーブルを挿入しても機能しましたが、私のサイトでそれをやろうとしたときにはうまく動作しませんでした。私もconnection..Not確認が今何をすべきかをテストしPHP MySQLがデータベースに挿入されていません

たぶん誰かが私のコードを見て、彼らは、テーブル名「のゲストブック」からの引用符を削除し、残して何に

<?php 
if(mysql_connect('<db>', '<un>', '<pw>') && mysql_select_db('smiles')) 
{ 

    $time = time(); 
    $errors = array(); 

    if(isset($_POST['guestbook_name'], $_POST['guestbook_message'])){ 
     $guestbook_name = mysql_real_escape_string(htmlentities($_POST['guestbook_name'])); 
     $guestbook_message = mysql_real_escape_string(htmlentities($_POST['guestbook_message'])); 

     if (empty($guestbook_name) || empty($guestbook_message)) { 
      $errors[] = 'All Fields are required.'; 
     } 
     if (strlen($guestbook_name)>25 || strlen($guestbook_message)>255) { 
      $errors[] = 'One or more fields exceed the character limit.'; 
     } 
     if (empty($errors)) { 

      $insert = "INSERT INTO 'guestbook'VALUES('','$time','$guestbook_name','$guestbook_message')"; 
      if($insert = mysql_query($insert)){ 
       header('Location: '.$_SERVER['PHP_SELF']); 
      } else{ 
       $errors[] = 'Something went wrong . Please try again.'; 
      } 
     } else { 
      foreach($errors as $error) { 
      echo '<p>'.$error.'</p>'; 
      } 
     } 

    } 
    //display entries 
} 
else { 
    'Fixing idiot'; 
} 
     ?> 
     <hr /> 
     <form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="POST"> 
    <p>Post Somethign</p> 
     <br /> 
      Name:<br /><input type="text" name="guestbook_name" maxlength="25" /> 
    <br /> 
    Message: 
    <br /> 
      <textarea name="guestbook_message" rows="6" coles="30"maxlength="255"></textarea> 
     <input type="submit" value="Post" /> 
     </form> 
+2

エラーが発生しましたか?出力は何ですか? –

+1

構文や接続など、実際のエラーがないかどうかを確認することもできます。デバッグの場合は、$ errors配列の汎用メッセージの代わりにmysql_error()を追加します。 –

+0

テーブルとカラム名を引用する場合は、通常の引用符ではないタックティックを使用します。 – Johan

答えて

2

に気づくかどうかを確認することができますスペースとそれの間のスペースvalues

2

テーブル名は引用符を必要とせず、あなたはid autoincrementを使用しています。空の文字列を挿入しないでください。したがって、それは次のようになります:

$insert = "INSERT INTO guestbook VALUES('$time','$guestbook_name','$guestbook_message')"; 

さらに、$ timeの値を見てください。 MySQLのデータ型は何ですか?

挿入した後、MySQLのエラーを表示しようとは:

$conn = mysql_connect('<db>', '<un>', '<pw>'); 
mysql_query($insert) 
if (mysql_errno($conn)){ 
    $errors[] = mysql_error($conn); 
}else{ 
    header('Location: '.$_SERVER['PHP_SELF']); 
} 

EDIT:穴スニペットはのようになります。

<?php 
$conn = mysql_connect('<db>', '<un>', '<pw>') 
if($conn && mysql_select_db('smiles')) //Note $conn 
{ 

    $time = time(); 
    $errors = array(); 

    if(isset($_POST['guestbook_name'], $_POST['guestbook_message'])){ 
     $guestbook_name = mysql_real_escape_string(htmlentities($_POST['guestbook_name'])); 
     $guestbook_message = mysql_real_escape_string(htmlentities($_POST['guestbook_message'])); 

     if (empty($guestbook_name) || empty($guestbook_message)) { 
      $errors[] = 'All Fields are required.'; 
     } 
     if (strlen($guestbook_name)>25 || strlen($guestbook_message)>255) { 
      $errors[] = 'One or more fields exceed the character limit.'; 
     } 
     if (empty($errors)) { 
      mysql_query($insert) 
      $insert = "INSERT INTO guestbook VALUES('$time','$guestbook_name','$guestbook_message')"; 
      if (mysql_errno($conn)){ 
       $errors[] = mysql_error($conn); 
      }else{ 
       header('Location: '.$_SERVER['PHP_SELF']); 
      } 
     } else { 
      foreach($errors as $error) { 
      echo '<p>'.$error.'</p>'; 
      } 
     } 

    } 
    //display entries 
} 
+0

私はこれをインサートの下に貼り付けたときに、if(mysql_errno($ conn)){somereason ...の行にT_ifエラーが発生しました。 – smiles

+0

$ conn変数が定義されていますか?あなたのスニペットを下に貼り付けます。 – santiagobasulto

+0

あなたが言及したスニペット。実際に何をしているのか気にしないのですか?私は初心者です。事前に感謝します。 – smiles

0

あなたが挿入するためのクエリの下に試すことができます。

$insert = "INSERT INTO guestbook VALUES('','{$time}','{$guestbook_name}','{$guestbook_message}')"; 
関連する問題