开发者

MySQL query failing due to reserved keyword?

开发者 https://www.devze.com 2023-02-21 08:20 出处:网络
Edit: If you\'re coming here from Google, this issue is a result of the word int being a reserved keyword in PHP. See the end of the accepted answer.

Edit: If you're coming here from Google, this issue is a result of the word int being a reserved keyword in PHP. See the end of the accepted answer.

I'm still learning PHP/MySQL and for the life of me I can't figure out what's wrong with my code.

I'm trying to take some data from an html page and add it to a table in my database. I'm passing the data with a GET request, then retrieving it with PHP's $_GET.

I've tested this and the variables are passed correctly to the PHP script 开发者_JAVA百科but they don't appear in the database. The script dies on this line: mysql_query($query) or die('data entry failed');

$database='a9293297_blog';
$con = mysql_connect('mysql2.000webhost.com','my_username','my_password');
mysql_select_db($database,$con) or die('failed to connect to database');

$username = $_GET['username'];
$password = $_GET['password'];
$charName = $_GET['charName'];
$sex = $_GET['sex'];
$class = $_GET['class'];
$race = $_GET['race'];
$str = $_GET['str'];
$sta = $_GET['sta'];
$dex = $_GET['dex'];
$int = $_GET['int'];
$cha = $_GET['cha'];

$query = "INSERT INTO Players (username, password, charName, sex, class, race, str, sta, dex, int, cha)
VALUES ('" . $username . "', '" . $password . "', '" . $charName . "', '" . $sex . "', '" . $class . "', '" . $race . "', '" . $str . "', '" . $sta ."', '" . $dex . "', '" . $int . "', '". $cha . "')";

mysql_query($query) or die('data entry failed'); // Fails here
mysql_close($con);


To know better what's wrong with your SQL query, use mysql_error():

mysql_query($query) or die(mysql_error());

Escape your string variables with mysql_real_escape_string(). Example:

$query = "INSERT INTO MYTABLE(MYFIELD) VALUES ('".mysql_real_escape_string($myVar)."');

EDIT

int seems to be a reserved MySQL keyword. Escape it with backquotes:

INSERT INTO Players (username, password, ..., str, sta, dex, `int`, cha) ...


im not sure but try this

Your Code

$query="INSERT INTO Players (username, password, charName, sex, class, race, str, sta, dex, int, cha)VALUES ('".$username."', '".$password."', '".$charName."', '".$sex."', '".$class."', '".$race."', '".$str."', '".$sta."', '".$dex."', '".$int."', '".$cha."')";

In this code

$query="INSERT INTO Players (username, password, charName, sex, class, race, str, sta, dex, int, cha)VALUES ('$username', '$password', '$charName', '$sex', '$class', '$race', '$str', '$sta', '$dex', '$int', '$cha')";

Maybe this helps if you remove " and .

0

精彩评论

暂无评论...
验证码 换一张
取 消