Smeagol
Smeagol

Reputation: 31

PHP MySQL data insert not working

My php script to insert data into a db is not working. It isn't giving any error messages either, so I do not know what is wrong. What is wrong?

My table:

Number  Name        Type            Null    Default      
1       Timestamp   timestamp       No      CURRENT_TIMESTAMP   
2       BTC         float           Yes     NULL
3       USD         float           Yes     NULL

My script:

<?php
$json_url = "https://crypto-trade.com/api/1/ticker/dvc_btc";
$json_data = file_get_contents($json_url);
$json_feed = json_decode($json_data);
$DVCdata = $json_feed->data;
$DVCask = $DVCdata->min_ask;
$json_url1 = "https://api.bitcoinaverage.com/ticker/USD";
$json_data1 = file_get_contents($json_url1);
$json_feed1 = json_decode($json_data1);
$BTCask = $json_feed1->ask;
$DVC_USD = $BTCask * $DVCask;
$DVCround = round($DVC_USD, 8);
$connection = mysqli_connect("mysql.serversfree.com",user,pass,database); 

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($connection,"INSERT INTO database (BTC, USD)
VALUES ('$DVCask', '$DVCround')");

mysqli_close($connection);    
?>

Upvotes: 0

Views: 200

Answers (1)

DBC
DBC

Reputation: 72

database is a reserve MySQL keyword. You will need to use backticks on the table name in order to use database as a table name.

mysqli_query($connection,"INSERT INTO `database` (BTC, USD)
VALUES ('$DVCask', '$DVCround')");

More info here

Upvotes: 2

Related Questions