...

View Full Version : Submit comment



martynball
02-27-2010, 03:50 AM
I am having a problem with this script. For some reason it is returning an error saying (Error 1):


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
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");
//}
?>

abduraooft
02-27-2010, 09:48 AM
CREATE TABLE comments
Change the above to
CREATE TABLE IF NOT EXISTS `comments`

martynball
02-28-2010, 03:50 PM
Does that mean I don't need the function and if statements which check for this?

ffmast
02-28-2010, 03:56 PM
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.

martynball
02-28-2010, 04:41 PM
Okay cheers.

Another problem.



//#############################\\
//###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:


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

abduraooft
02-28-2010, 04:51 PM
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.

martynball
02-28-2010, 04:58 PM
Okay cheers :).

ffmast
02-28-2010, 07:11 PM
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...
:D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum