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.

6
  • 1
    stackoverflow.com/questions/1346209/… Commented Dec 13, 2016 at 10:47
  • 1
    wow, you using this on a live server? Commented Dec 13, 2016 at 10:47
  • You are missing a space before VALUES. Commented Dec 13, 2016 at 10:47
  • I would advice you to use mysqli, not mysql functions Commented Dec 13, 2016 at 10:48
  • If you want to use php variable then use php valriable without quotes or you can write like values ('".$ip."','".$key."') Commented Dec 13, 2016 at 10:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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
@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.
@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 stackoverflow.com/questions/23142689/…
@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.
Thanks everyone! Hallur's, Rahul Dambare's, and of course Rahul's answers helped solve the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.