Insert to mysql database

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
hortplus
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Dec 20, 2017 12:27 pm

Wed Dec 20, 2017 12:36 pm

i've spent a frustrating day trying to get this going:

<?php
# check for expected parameters
if(!isset($_REQUEST['logger'],$_REQUEST['temperature'],$_REQUEST['leafwetness'])){
exit("ERROR: There was a problem authenticating your request, if this problem continues contact hortplus@gmail.com [error code: 100]");
}
$username = "root";
$password = "l5iNw5EOJ91";
$hostname = "hort.com";
$db_name = "premet";

//connection to the database
$link = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

// setup script parameters
echo "Getting parameters<br>";
$stationID = $_REQUEST['logger'];
date_default_timezone_set('NZ');
$startdate = date('Y/m/d h:m', time());
$stopdate = date('Y/m/d h:m', (time()- (1 * 60)));
$temperature = $_REQUEST['temperature'];
$leafwetness = $_REQUEST['leafwetness'];
echo "$startdate";
// create met data insert
$table = 'met_metdata';
echo "About to insert<br>";

$sql = "INSERT INTO met_metdata (station_id,meteo_var_id,meas_type_id,interval_id,stop_date,start_date,amount) VALUES ('$stationID', 'TD', 'M','M','$stopdate','$startdate','$temperature')";
echo "$sql";

if (mysqli_query($link, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($link);
}


$link->close();
?>

But i get these error messages:

Connected to MySQL
Getting parameters
2017/12/21 09:12About to insert
INSERT INTO met_metdata (station_id,meteo_var_id,meas_type_id,interval_id,stop_date,start_date,amount) VALUES ('123', 'TD', 'M','M','2017/12/21 09:12','2017/12/21 09:12','10')
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /var/www/htdocs/hortmet/write_met.php on line 32

Warning: mysqli_error() expects parameter 1 to be mysqli, resource given in /var/www/htdocs/hortmet/write_met.php on line 35
Error: INSERT INTO met_metdata (station_id,meteo_var_id,meas_type_id,interval_id,stop_date,start_date,amount) VALUES ('123', 'TD', 'M','M','2017/12/21 09:12','2017/12/21 09:12','10')

Fatal error: Call to a member function close() on a non-object in /var/www/htdocs/hortmet/write_met.php on line 39

What am i doing wrong?

Yours in desperation

Andrew Hodson

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 708
Joined: Mon Feb 22, 2016 5:52 pm

Wed Dec 20, 2017 4:39 pm

Your error is caused by line 11 which is where your problems start

Code: Select all

$link = mysql_connect($hostname, $username, $password) 
as you open the database as MySQL and try to use it as MySQLi.

As said, that's just the start of the problems in your code, have a read of this.

gopalkumar315
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Feb 14, 2018 8:05 pm

Wed Feb 14, 2018 8:27 pm

you are making connection with mysql and executing query with mysqli.
try one either mysql or mysqli.

Post Reply