Jump to content

Help with MySQL "INSERT..."


Worqy

Recommended Posts

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

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()

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.