Jump to content

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

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
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())";
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...