0
//--------------------------------------------------------------------------
// php script for adding data from mysql database
//--------------------------------------------------------------------------

$ip = $_GET['ip']; //for debugging sake, will be POST from Ajax
$key = substr(md5(microtime()),rand(0,26),5); //random referral ID - will implement exist analysis
echo $ip; //debugging
$dbhost = 'localhost';
$dbuser = user;
$dbpass = 'pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $conn);
$tbl_name = "refs";
$sql="INSERT INTO $tbl_name(ip, key)VALUES('frfr', 'grgr')";

if (!mysql_query($sql,$con)) {
    die('Error: ' . mysql_error());
}
echo "1 record added";

I'm not sure if it's my Digital Ocean server or what, but the only syntax my PhpMyAdmin will accept as a query is as INSERT INTOrefs(ip,key) VALUES ("insert","432") with the double quoted values. I cannot seem to get this implemented in the PHP without getting a flat out error or an Unknown column in 'field list' error.

Similar questions suggest junk non-printable characters from copy-paste, however I've retyped my code within the editor. Thanks for all the help

I'm creating a basic referral system by the way, storing requested IP's in 'refs' table with a key, or id.

jean-max
  • 1,640
  • 1
  • 18
  • 33
ledar
  • 5
  • 2

1 Answers1

3

key is a reserve word and thus needs to be escaped using backtique. Along with that you have spacing issue as well. Your query should looks like below

INSERT INTO refs(ip,`key`) VALUES ('insert','432')

Never use a reserve word as column or table name. if in doubt, then escape all the columns present in query.

Start referring MySQL Documentation for more inforamtion. It's way easier than posting it as question in stackoverflow.

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Ok, this code is still giving me an error. I tried changing the "key" column to id and trying with and without the ` but it doesn't seem to make a difference. The error in my log is [13-Dec-2016 10:55:53 UTC] PHP Warning: mysql_query() expects parameter 2 to be resource, null given in /home/username/public_html/newref.php on line 21 – ledar Dec 13 '16 at 10:57
  • @user3026679 that is because you're using mysql and not mysqli. therefore, you don't need to assign the connection to the query. `mysql_query($sql,$con)` => `mysql_query($sql)` ALSO: `$con` is not defined... you possibly made a typo there, should have been `$conn`, if necessary. –  Dec 13 '16 at 10:59
  • @user3026679, that's a different issue now and again search that error message in google or in stackoverflow. You will get thousands of example like this one http://stackoverflow.com/questions/23142689/warning-mysql-query-expects-parameter-2-to-be-resource – Rahul Dec 13 '16 at 11:00
  • @user3026679, and a final piece of suggestion: start putting a bit of effort than just asking a question. that way you will learn a lot. – Rahul Dec 13 '16 at 11:01
  • Thanks everyone! Hallur's, Rahul Dambare's, and of course Rahul's answers helped solve the issue. – ledar Dec 13 '16 at 11:02
  • @user3026679 please mark this answer as an accepted answer so that this question will be marked as solved. –  Dec 13 '16 at 11:03