Worqy Posted February 20, 2010 Share Posted February 20, 2010 Hello. I'm doing a tool for a game, and I need to insert some data to my MySQL database. I use this code, but it dont work <?php session_start(); include 'config.php'; include 'checklogin.php'; include 'main.php'; if($_SESSION['Login'] == false) { echo 'Login first!'; } else { $t1= $_POST['t1']; //In main.php // $username 's value comes from checklogin.php // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql = "INSERT INTO troops (username, t1) VALUES ("$username", "$t1");"; mysql_query($sql); ?> Link to comment https://forums.phpfreaks.com/topic/192736-help-with-mysql-insert/ Share on other sites More sharing options...
jl5501 Posted February 20, 2010 Share Posted February 20, 2010 change this $sql = "INSERT INTO troops (username, t1) VALUES ("$username", "$t1");"; to $sql = "INSERT INTO troops (username, t1) VALUES ('$username, '$t1');"; Link to comment https://forums.phpfreaks.com/topic/192736-help-with-mysql-insert/#findComment-1015302 Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 You mixed single and double quotes: $sql = "INSERT INTO troops (username, t1) VALUES ('$username', '$t1');"; Also, for sake of protection agains SQL injections do this: $t1= mysql_real_escape_string($_POST['t1']); and move this below mysql_connect() Link to comment https://forums.phpfreaks.com/topic/192736-help-with-mysql-insert/#findComment-1015303 Share on other sites More sharing options...
Worqy Posted February 20, 2010 Author Share Posted February 20, 2010 Now the code look like this: <?php session_start(); include 'config.php'; include 'checklogin.php'; include 'main.php'; if($_SESSION['Login'] == false) { echo 'Login first!'; } else { // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $t1= mysql_real_escape_string($_POST['t1']); $sql = "INSERT INTO troops (username, t1) VALUES ('$username', '$t1');"; mysql_query($sql); ?> But it still dont add the values Username and s1 to MySQL Link to comment https://forums.phpfreaks.com/topic/192736-help-with-mysql-insert/#findComment-1015353 Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 change mysql_query($sql); to if(!mysql_query($sql)) { echo mysql_error().": $sql"; die(); } and tell us what it says Link to comment https://forums.phpfreaks.com/topic/192736-help-with-mysql-insert/#findComment-1015355 Share on other sites More sharing options...
Worqy Posted February 20, 2010 Author Share Posted February 20, 2010 Nothing. test it out yourself at http://www.nox-trojan.hyperphp.com Username: Kevin Password: 12345 And then fill in a amount of "Klubbmen" and press "Submit" Link to comment https://forums.phpfreaks.com/topic/192736-help-with-mysql-insert/#findComment-1015360 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.