Jump to content
jaylow

can not INSERT INTO getting a error.

Recommended Posts

Hello, i am having issues adding info to a database

 

i have a html from where a user should fill in charname and select ###### then press submit.

if i do this i get the error

 

ERROR:could not able to execute INSERT INTO character(charname, ######, xp, gold, accdate) VALUES(jay, male, 0, 100000, now()).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 'character(charname, ######, xp, gold, accdate) VALUES(jay, male, 0, 100000, now())' at line 1

<?php    include_once 'db_connect.php';    include_once 'psl-config.php';         $error_msg = "";    $now = time();	    // check connection    if($mysqli === false){        die("ERROR: Could not connect. " . mysqli_connect_error());    }	// Escape user inputs for security    $charname = mysqli_real_escape_string($mysqli, $_POST['charname']);    $###### = mysqli_real_escape_string($mysqli, $_POST['######']);        //attempt inser quesry execution    $sql = "INSERT INTO character (charname, ######, gold, xp, accdate) VALUES ($charname, $######, 100000, 0, now())";    if(mysqli_query($mysqli, $sql)){        echo "Records added successfully.";    } else{        echo " ERROR:could not able to execute $sql." .mysqli_error($mysqli);    }        // close connection    mysqli_close($mysqli);    ?>

Table

CREATE TABLE IF NOT EXISTS `character` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `charname` varchar(30) NOT NULL,  `######` tinyint(4) NOT NULL,  `gold` int(11) NOT NULL,  `xp` int(11) NOT NULL,  `accdate` timestamp NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

I was wondering if some one could explain me what i do wrong, and or have some tips how to make this better.

the idea is to add a check to see in the "charname" is already used.

and some other stuff. but first i like to see the info end up in my DB

 

Thanks

Share this post


Link to post
Share on other sites

Your problem is that you forgot to wrap your strings in quotation marks.

"INSERT INTO character (charname, ######, gold, xp, accdate) VALUES ('$charname', '$######', 100000, 0, now())";
  • Like 1

Share this post


Link to post
Share on other sites

now i am getting the error:

 

ERROR:could not able to execute INSERT INTO character (charname, ######, gold, xp, accdate) VALUES ('sam', 'male', 100000, 0, now()).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 'character (charname, ######, gold, xp, accdate) VALUES ('sam', 'male', 100000, 0, n' at line 1

 

making me think the "now()" has a issue. i tried quotation marks around it but did not help

$sql = "INSERT INTO character (charname, ######, gold, xp, accdate) VALUES ('$charname', '$######', 100000, 0, now())";

Share this post


Link to post
Share on other sites

Perhaps "character" is a reserved word in MySQL.

 

You could wrap it in backticks:

`character`
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×