Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Submit comment

  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    Submit comment

    I am having a problem with this script. For some reason it is returning an error saying (Error 1):
    Code:
    Error 1: creating table mysql error - Table 'comments' already exists
    Error 2: creating table - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    Error 3: validation
    Although, it shouldn't even read that part of the script if the table exists as it is only activated if false is returned from the checkTB function...

    There are also two other errors. (There is data in the fields :S)

    PHP Code:
    PHP Code:
    <?php 
    session_start
    ();
    include 
    "connect.php";

    //Global
    $timezone "GMT";
    $mess "Thank-you for your comment $name! It will be displayed once checked by an administrator!"
    $gal $_SESSION['gal'];
    //Thank-you message to be displayed

    //#############################\\
    //###Check comment table exists###\\
    //#############################\\
    //Function to check that table exists, should create it when script is first run
    function checkTB() {
        
    $sql "SHOW TABLES FROM $dbname";
        
    $result mysql_query($sql);
        if (!
    $result) {
            return 
    false
        } elseif (isset(
    $result)) {
            return 
    true;
        }
        unset(
    $result,$query);
    }

    //Check comment table exists, if not then create it...
    if (checkTB()==false) {
    mysql_select_db($dbname$con);
    $query "CREATE TABLE comments(
        message VARCHAR(1000) NOT NULL ,
        name VARCHAR(20) NOT NULL ,
        ip VARCHAR(20) NOT NULL ,
        email VARCHAR(30) NOT NULL ,
        date VARCHAR(20) NOT NULL ,
        gal VARCHAR(20) NOT NULL ,
        id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Comment ID')"
    ;
        
    $result mysql_query($query,$con);
        if (!
    $result) {
            
    $case[] = "creating table mysql error - ".mysql_error();
        }
    }
    if (
    checkTB()==false) {
        
    $case[] = "creating table - ".mysql_error();
    }
    //mysql_close($con);

    //Variables for adding and validating comment
    date_default_timezone_set(strip_tags($timezone));
    $name strip_tags($_POST['vname']);
    $message strip_tags($_POST['vmessage']);
    $email strip_tags($_POST['vemail']);
    //$date = date(D, m Y);
    $ip strip_tags($_SERVER['REMOTE_ADDR']);
    $gal strip_tags($_GET['gal']);

    //Mininum characters
    $min 3;

    //Finnish variables///////////////////////////////////////

    //#############################\\
    //#########Field validation########\\
    //#############################\\
    function validate() {
        global 
    $name,$message,$date,$ip,$min;
        if (
    $name == "") {
            return 
    false//Returns false if field data length is not more than 3 or is equal to 0
        
    }
        elseif (
    $message == "" || count($message <= $min)) {
            return 
    false;
        } 
        elseif (
    $date == "") {
            return 
    false;
        }
        elseif (
    $gal == "") {
            return 
    false;
        }
        else {
        return 
    true// Returns true, changes to false if validation is incorrect
        
    }
    }

    //#############################\\
    //######Add data to database#####\\
    //#############################\\
    if (validate()==true) {
        
    $query "INSERT INTO comments (name, message, date, ip, gal, email) VALUES 
        ('$name','$message','$date','$ip','$gal','$email')"
    ;
        
    $result mysql_query($query$con);
        if (!
    $result) {
            
    $case[] = "adding comment: ".mysql_error();
        }

    else {
        
    $case[] = "validation";
    }
    //Get data from array
    $i 1;
    foreach (
    $case as $error) {
        echo 
    "Error $i: ".$error."<br />";
        
    $i++;
    }
    //substr($case, 0, 1) == 1) {
    //    header("location: ../profile.php?gal=".$gal.".&mess=".$case."&messt=1");
    //} elseif ($case == 0) {
    //    header("location: ../profile.php?gal=".$gal."&mess=".$mess."&messt=0");
    //}
    ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    CREATE TABLE comments
    Change the above to
    Code:
    CREATE TABLE IF NOT EXISTS `comments`
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    martynball (02-28-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Does that mean I don't need the function and if statements which check for this?

  • #4
    New Coder
    Join Date
    Feb 2010
    Posts
    29
    Thanks
    0
    Thanked 3 Times in 3 Posts
    In any case, your checkTB() function only checks for "if there is any table in database". There might be another one in current database (and probably is, thats why the check fails)
    But yes , if you add "IF NOT EXISTS" , you can skip the check.

  • Users who have thanked ffmast for this post:

    martynball (02-28-2010)

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Okay cheers.

    Another problem.

    PHP Code:
    //#############################\\
    //###Make table in database####\\
    //#############################\\
    //Check comment table exists, if not then create it...
    mysql_select_db($dbname$con);
    $query "CREATE TABLE IF NOT EXISTS comments
        (
        message VARCHAR(1000),
        name VARCHAR(20),
        ip VARCHAR(20),
        email VARCHAR(30),
        date VARCHAR(20),
        gal VARCHAR(20),
        id INT(10) AUTO_INCREMENT,
        PRIMARY KEY (id)
        )"
    ;
        
    $result mysql_query($query,$con);
        if (!
    $result) {
            
    $case[] = "creating table mysql error - ".mysql_error();
        }

    Error:
    Code:
    Error 1: creating table - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    date VARCHAR(20),
    DATE is a reserved mysql keyword to represent a datatype, see http://dev.mysql.com/doc/refman/5.1/en/datetime.html. You may avoid the error by adding back-ticks around your field name, like `date`, though using keywords as the name of table/field is a bad practice.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Okay cheers .

  • #8
    New Coder
    Join Date
    Feb 2010
    Posts
    29
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by abduraooft View Post
    DATE is a reserved mysql keyword to represent a datatype
    Than one gave me a great headache once. Couldn't understand whats wrong, emphasized by the fact, that phpadmin somehow created such a field, but all INSERTs failed. Eh...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •