1

I have heard of this issue but can't seem to figure it out. I have the database and table names correct. I am not finding any errors and i even inserted a table myself on phpmyadmin that worked but when I tried to do it on my site it doesnt work. I even tested the connection..Not sure what to do now

Maybe someone can take a look at my code and see if they notice anything

<?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>
markus
  • 40,136
  • 23
  • 97
  • 142
smiles
  • 9
  • 1
  • 2
  • 2
    Do you see any errors?. What is the output? – Deep Kapadia Sep 12 '11 at 21:02
  • 1
    You may want to also check for the exact errors you are getting, whether it would be syntax, connections, etc. For debugging, add mysql_error() instead of a generic message for your $errors array. – Chris C Sep 12 '11 at 21:06
  • if you want to quote table and columnnames, use a tacktick ` not normal quotes. – Johan Sep 12 '11 at 21:49

3 Answers3

2

Remove quotes from table name 'guestbook' and leave a space between it and values

Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98
2

Table name doesn't need quotes and supossing you're using id autoincrement, don't insert an empty string. So it should be:

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

Also, take a look at your $time value. What MySQL data type is?

After the insert, try to display the mysql error:

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

EDIT: The hole snippet should be similar to:

<?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
} 
santiagobasulto
  • 11,320
  • 11
  • 64
  • 88
0

you can try below query for insertion:

$insert = "INSERT INTO guestbook VALUES('','{$time}','{$guestbook_name}','{$guestbook_message}')"; 
User45
  • 74
  • 1
  • 10