user1108224
user1108224

Reputation: 159

php insert mysql not working

.I don't know if it's syntax or what. I've tried a variety of ways this is the simplest I thought would work.

I send info to the userData.php using:

http://mydomain.com/adverts/userStats.php?name=001EC946C2F4&adNum=1&playClick=1

On the userData.php I have:

<?php 
    $db = mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error()); 
   $db_selected =  mysql_select_db('databaseName', $db) or die('Could not select database');
if (!$db_selected)
  {
  die ("Can\'t use test_db : " . mysql_error());
  }

    $name = mysql_real_escape_string($_GET['name']); 
    $date = date("d/m/Y");
    $adClick = mysql_real_escape_string($_GET['adNum]);
    $playN = mysql_real_escape_string($_GET['playClick']);

$query = mysql_query("INSERT INTO playerData VALUES ('$name', '$date','$adClick','$playN')");
$result = mysql_query($query) or die('Query failed: ' . mysql_error())); 

mysql_close($db);
?>

I manually added 2 records to the table from phpMyAdmin, and I can display or update them just fine but adding a new record isn't working. I simply want to start a new record each time the link is called from another program, and store the mac address, date, adNum, and playClick.

EDIT2:: echo $query; for

http://simplehotkey.com/adverts/userStats.php?name=001EC946C2F4&adNum=1&playClick=1

outputs:
INSERT INTO playerData(mac,date,AdClick,PlayNum) VALUES ('001EC946C2F4', '26/07/2012','1','1')

Which is what I want it's just not adding it to the DB.

Upvotes: 1

Views: 124

Answers (4)

swapnesh
swapnesh

Reputation: 26722

Correct syntax is --

mysql_select_db("databaseName", $db);

And its better if u use something like this for connection errors--

$db_selected= mysql_select_db("databaseName", $db);
if (!$db_selected)
  {
  die ("Can\'t use test_db : " . mysql_error());
  }

EDIT

You are writing all wrong :(

$query = mysql_query("INSERT INTO playerData VALUES ('$name', '$date','$adClick','$playN')");
$result = mysql_query($query)  <--------------WRONG

Try Something like this----

$query = "INSERT INTO playerData(CORRECT_COL_NAMES) VALUES ('$name', '$date','$adClick','$playN')";
$results = mysql_query($query, $connection);

NEW EDIT

AREA OF ERROR---- WRONG DATATYPE

','1','1' <--- this is passing as string while u have have this as an int in your db structure ..now run the same query as it is to figure out the error..also u can figure out using $result = mysql_query($query) or die(mysql_error());

Upvotes: 2

Daedalus
Daedalus

Reputation: 7722

It's pretty easy to see what's wrong here, especially with syntax highlighting.

$adClick = mysql_real_escape_string($_GET['adNum]);

This line is missing a single quote mark; it should be:

$adClick = mysql_real_escape_string($_GET['adNum']);

This is a syntax error that ruins everything else.

Not to mention that your database selection is missing your database handler, ie:

mysql_select_db('databasename',$db); 

As pointed out by @swapnesh, and as noted here.

Edit

I have been unable to reproduce your lack of an error, what I have gotten however, are errors. Firstly, you have an extra ) at line 12:

$result = mysql_query($query) or die('Query failed: ' . mysql_error()));

Should be:

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

Lastly, you actually improperly execute your query twice, so the second time, the query is empty. What you have:

$query = mysql_query("INSERT INTO playerData VALUES ('$name', '$date','$adClick','$playN')");
$result = mysql_query($query) or die('Query failed: ' . mysql_error())); 

Should instead be:

$query = "INSERT INTO playerData VALUES ('$name', '$date','$adClick','$playN')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 

Upvotes: 2

Sumesh TG
Sumesh TG

Reputation: 450

You have the syntax error on this line

Wrong :

$adClick = mysql_real_escape_string($_GET['adNum]);

Correct :

$adClick = mysql_real_escape_string($_GET['adNum']);

Upvotes: 0

Conrad Lotz
Conrad Lotz

Reputation: 8818

Instead of using the insert statement the way you do add the fields that will receive entries explicitly. The database table might have more fields and the insert statement does not explcitly state which fields will receive data.

$query = mysql_query("INSERT INTO playerData (Name,Date,AdClick,PlayN) VALUES ('$name', '$date','$adClick','$playN')");

Upvotes: 0

Related Questions