-2

I would like to insert data from two variables to MySQL.

Here is my code:

<?php

// Prepare variables for database connection

$dbusername = "arduino";  // enter database username, I used "arduino" in step 2.2
$dbpassword = "arduinotest";  // enter database password, I used "arduinotest" in step 2.2
$server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"

// Connect to your database

$dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
$dbselect = mysql_select_db("test",$dbconnect);

// Prepare the SQL statement

$sql = "INSERT INTO test.sensor (homero, parat) VALUES ('".$_GET["homero"].", ".$_GET["parat"]."')";

// Execute SQL statement

mysql_query($sql); ?>

And when I click on http://localhost/write_data.php?homer=32&parat=43 it wont send any data to my database. Please can you help me? Is $sql line correct? thanks

  • 1
    **1.** Your quotes are missing. The right one should be: `"INSERT INTO test.sensor (homero, parat) VALUES ('".$_GET["homero"]."', '".$_GET["parat"]."')"`. **2.** Do not use `mysql_*` functions as they are deprecated. – Praveen Kumar Purushothaman Mar 18 '16 at 09:46

3 Answers3

0

There is an error in your SQL statement, you don't have the correct quotes.
Also: please use backticks around variables.

Before I give the solution, I need to warn you about some other stuff.
First of all: your code is vulnerable to SQL injection.
More information about SQL injection: What is SQL injection?

Second of all: Mysql_* is deprecated in PHP5, and removed in PHP7. You can switch to Mysqli_* of even better: PDO. Please do some research about this.
More information about this: Why shouldn't I use mysql_* functions in PHP?

Anyway: here is the line of code that will fix your snippet, but it isn't that good:

$sql = "INSERT INTO `test`.`sensor` (`homero`, `parat`) VALUES ('".$_GET["homero"]."', '".$_GET["parat"]."')";
Blaatpraat
  • 2,829
  • 11
  • 23
0

Your have two missing quotes first is after ".$_GET["homero"]." and second is before ".$_GET["parat"].". So try using below query:

$sql = "INSERT INTO test.sensor (homero, parat) VALUES ('".$_GET["homero"]."', '".$_GET["parat"]."')";
Mubeen Ali
  • 2,150
  • 2
  • 18
  • 26
0

Try this one:

<?php

// Prepare variables for database connection

$dbusername = "arduino";  // enter database username, I used "arduino" in step 2.2
$dbpassword = "arduinotest";  // enter database password, I used "arduinotest" in step 2.2
$server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
$db =   "test";
// Connect to your database

$mysqli = mysqli_connect($server,$dbusername,$dbpassword,$test) or die("Error " . mysqli_error($mysqli)); 

// Prepare the SQL statement

$sql = "INSERT INTO test.sensor (homero, parat) VALUES ('".$_GET['homero']."', '".$_GET['parat']."')";

// Execute SQL statement

mysqli_query($mysqli, $sql) ?>

Use mysqli instead of mysql because the later is depricated

Liu Jin Long
  • 83
  • 1
  • 10