0

I have a mySQL issue that has me stumped for hours!

Context: I am creating a web-application to teach children different musical symbols on touch devices

Problem: after running this individual script, no row is created although last echo argument works.

Wierd: this is modeled after other scripts I have working.

EDIT: I will re-furnish the code with prepared statements after we find the problem.

Here is the code:

<?php

$database_connect = mysql_connect('localhost','ragstudi','*****');

if (!$database_connect) {die ('Not connected to SQL' . mysql_error());};

mysql_select_db('ragstudi_musicGameScores') or die ('db doesnt exist');


$userName = "Jimmy";//$_POST["username"];
$gameLevel = 1;//$_POST["level"];
$gameScore = 2300;//$_POST["score"];
$userGroup = "RagWway";

$highScores = mysql_query("INSERT INTO highScores (name, groupBy, level, score) values('".$userName."','".$userGroup."','".$gameLevel."','".$gameScore."')");
if (!$highScores){
    die(
        mysql_error()
        )
};

The database _musicGameScores is set up like this Id, primary, auto increment, integer name, varchar groupBy, varchar level, integer score, integer

The password is Star'ed out. Thank you all in advance!

  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Apr 05 '13 at 00:58
  • Thank you for this. I have not dealt with a lot of mySQL yet, but when I first did, it was not this way. I will implement this after this issue is resolved. Thanks! – Stephen Raghunath Apr 05 '13 at 01:29

3 Answers3

4

GROUP is a reserved keyword. You must properly escape it so it will not generate an error.

INSERT INTO highScores (id, user, `group`, level, score) VALUES(...)

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

Try to add `` in every column, maybe theres a reserve keyword out there.

mysql_query("INSERT INTO highScores (`id`, `user`, `group`, `level`, `score`) values('','".$userName."','".$userGroup."','".$gameLevel."','".$gameScore."')");
Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
0

It should be like this. Cheers! :D

$highScores = mysql_query("INSERT INTO highScores (id, user, group, level, score) values('','".$userName."','".$userGroup."','".$gameLevel."','".$gameScore."')");

Ain Ronquillo
  • 197
  • 1
  • 3
  • 6