The insert data not showing in my database table through postman - php

I am trying to send data (temperature, humidity and time) from postman through API link for testing my code. I upload my php files in hostinger.com and every time I am trying to send data through postman it gives me OK and no errors but the data not showing in my phpmyadmin database table!
my insert.php code:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();
// Check if we got the field from the user
if (isset($_GET['temp']) && isset($_GET['hum']) && isset($_GET['time'])) {
$temp = $_GET['temp'];
$hum = $_GET['hum'];
$time = $_GET['time'];
// Include data base connect class
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");
// Connecting to database
$dbo = new DB_CONNECT();
// Fire SQL query to insert data in weather
$result = "INSERT INTO 'climate'('temp', 'hum', 'time') VALUES('$temp', '$hum', '$time')";
// Check for succesfull execution of query
if ($result) {
// successfully inserted
$response["success"] = 1;
$response["message"] = "climate successfully created.";
// Show JSON response
echo json_encode($response);
} else {
// Failed to insert data in database
$response["success"] = 0;
$response["message"] = "Something has been wrong";
// Show JSON response
echo json_encode($response);
}
} else {
// If required parameter is missing
$response["success"] = 0;
$response["message"] = "Parameter(s) are missing. Please check the
request";
// Show JSON response
echo json_encode($response);
}
?>
and my db_connect.php :
<?php
class DB_CONNECT {
// Connecting to mysql (phpmyadmin) database
// Constructor
function __construct() {
// Trying to connect to the database
$this->connect();
}
// Destructor
function __destruct() {
// Closing the connection to database
$this->close();
}
// Function to connect to the database
function connect() {
//importing dbconfig.php file which contains database credentials
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/dbconfig.php");
// Connecting to mysql (phpmyadmin) database
$con = mysqli_connect($dbhost_name, $username, $password);
// Selecing database
// returing connection cursor
return $con;
}
// Function to close the database
function close() {
// Closing data base connection
mysqli_close($con);
}
}
?>
and this is my link in postman I am using:
http://mydomain in hostinger.com/api/insert.php?temp=25&hum=80&time=2019-05-
09 08:00:00
and the result is:
{
"success": 1,
"message": "climate successfully created."
}
Is there any problem with my code?
Thanks for all

You haven't actually executed the query. $result is actually $query, and should be passed to your db connection $dbo.

Related

mysqli_query() expects parameter 1 to be mysqli, object given in [duplicate]

This question already has answers here:
mysqli_query() expects parameter 1 to be mysqli, object given
(3 answers)
Closed 2 years ago.
I got this error when i try to save database in mysql using wampServer.
mysqli_query() expects parameter 1 to be mysqli, object given in
there are 3 PHP files, db_config.php and db_connect.php and create_person.php and i already reated table PERSON in phpmyadmin.
db_config.php
<?php
/*
* All database connection variables
*/
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "othmane"); // database name
define('DB_SERVER', "localhost"); // db server
?>
db_connect.php
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());
// Selecing database
$db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysqli_close();
}
}
?>
create person.php :
<?php
/*
* Following code will create a new person row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['nom']) && isset($_POST['pass_world'])) {
$name = $_POST['nom'];
$pass = $_POST['pass_world'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result =mysqli_query($db,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
Your
$db = new DB_CONNECT();
returns an instance of DB_CONNECT class.
What you need in your
mysqli_query(...)
call is the object returned by your mysqli_connect call.
Try changing your:
function __construct() {
// connecting to database
$this->connect();
}
with:
function __construct() {
// connecting to database
$this->conn = $this->connect();
}
Then, change:
$result =mysqli_query($db,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");
with:
$result =mysqli_query($db->conn,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");
Also, I think you should read the PHP manual for the functions you are using:
mysqli_connect
mysqli_select_db

Is there any easy way to convert PHP code for communicating to a MySQL database to code that is for SQL Server?

I am creating an android app that was previously used with a MySQL database, however now I have to use a SQL Server database. Are there any applications or resources that can convert one to the other?
Here is an example of the php code I was using to connect to the MySQL database:
db_connect.php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
//require_once __DIR__ . '/db_config.php';
define('__ROOT__',dirname(dirname(__FILE__)));
require_once __ROOT__ . '/android_connect/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
}
}
?>
create_product.php
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>

Insert query not working in WAMP Server

We have one android application which access WAMP Server using PHP. All queries like, Select* etc working fine. But insert query not working properly in specific condition,
When WAMP already have some data in database we failed to insert new data in database. But when WAMP server don't have any existing data then insert query work successfully. following is PHP code we are using to insert a data,
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['UserName']) && isset($_POST['UserID']) && isset($_POST['Password']) && isset($_POST['Age'])&& isset($_POST['ContactNumber']) && isset($_POST['expiryDate'])) {
$UserName = $_POST['UserName'];
$UserID = $_POST['UserID'];
$Password = $_POST['Password'];
$Age = $_POST['Age'];
$ContactNumber = $_POST['ContactNumber'];
$expiryDate = $_POST['expiryDate'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO normaluser(UserName, UserID, Password,Age,ContactNumber,expiryDate) VALUES('$UserName', '$UserID', '$Password','$Age','$ContactNumber','$expiryDate')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "New user successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! error is there.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required fields is missing";
// echoing JSON response
echo json_encode($response);
}
?>
Can you guys please help use to solved this problem?
Ignoring the using of mysql_* which is deprecated and lack of sanitization of data.
You do not check if this User is already on the database. Assuming at least one of your columns is a unique or Primary key then this query is likely to fail if you try and create the same user twice.
But regardless of that possible issue if you add some useful error processing to your code you will be told exactly what the problem is. This is a little simplistic as you probably dont want these errors sent back to a user but it will tell you the exact error being returned from MySQL:
$result = mysql_query("INSERT INTO normaluser(UserName, UserID, Password,Age,ContactNumber,expiryDate)
VALUES('$UserName', '$UserID', '$Password','$Age','$ContactNumber','$expiryDate')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "New user successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = 'Database error ' . mysql_errno() . ' ' . mysql_error();
// echoing JSON response
echo json_encode($response);
}

How do i get my android application to send and receive data/information to the sqlserver

I am having difficulty getting my Android application to transmit the input it has received to myphpadmin server.
I am wondering what do i need to add to my Andriod coding so that my application is able to get the php script to run and transmit the information to the server.
What i have done so far is creating php scripts for configuration and connection purpose.
<?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', "password"); // db password (mention your db password here)
define('DB_DATABASE', "Wifi"); // database name
define('DB_SERVER', "http://andy-009.dlink.net/"); // db server
?>
and
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();}
// destructor
function __destruct() {
// closing db connection
$this->close();}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con; }
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
} }
as well as a insertion php script
update_product.php
<?php
/*
* Following code will update a product information
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['WifiMacAddress']) && isset($_POST['WifiSSID']) && isset($_POST['WifiLatitude']) && isset($_POST['WifiLongtitude']) && isset($_POST['WifiLocation'])) {
$WifiMacAddress = $_POST['WifiMacAddress'];
$WifiSSID = $_POST['WifiSSID'];
$WifiLatitude = $_POST['WifiLatitude'];
$WifiLongtitude = $_POST['WifiLongtitude'];
$WifiLocation = $_POST['WifiLocation'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("UPDATE Wifi SET WifiSSID = '$WifiSSID', WifiLatitude = '$WifiLatitude', WifiLongtitude = '$WifiLongtitude' , WifiLocation = '$WifiLocation' WHERE WifiMacAddress = $WifiMacAddress");
// check if row inserted or not
if ($result) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Product successfully updated.";
// echoing JSON response
echo json_encode($response);
} else {
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
i think communicating between android and php, JSON would be helpful.
you can parse the data into JSON at both ends and transmit them over internet using web-services

_POST method returns ??? when request utf8 characters

i have the following problem... i am building an android app and i regeuest text formated in utf8 (greek chars) from editTexts by using the POST method. The POST method gets the greek chras from the editTexts as '???' and insert them in mysal again as '???'
How will the POST recognize my greek chars???
<?php
// PHP variable to store the host address
$db_host = "localhost";
// PHP variable to store the username
$db_uid = "lolen";
// PHP variable to store the password
$db_pass = "lolen";
// PHP variable to store the Database name
$db_name = "lolen";
// PHP variable to store the result of the PHP function 'mysql_connect()' which establishes the PHP & MySQL connection
$db_con = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect');
mysql_query("SET character_set_results=utf8", $db_con);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($db_name, $db_con);
mysql_query("set names 'utf8'",$db_con);
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['k_p']) )
{
$Kwdikos_Proiontos=$_POST['k_p'];
$Proion=$_POST['p'];
$Kwdikos_Tupou=$_POST['k_t'];
//$Tupos=$_POST['t'];
$sql=mysql_query("SELECT * FROM tupoi WHERE Kwdikos_Tupou LIKE '". $_POST["k_t"]."'", $db_con);
while($row = mysql_fetch_array($sql))
{
$output[]=$row['Tupos'];
$re= json_encode($output[0]);
$dd= json_decode($re, true);
}
$result =mysql_query("INSERT INTO proionta(Kwdikos_Proiontos, Proion, Kwdikos_Tupou, Tupos) VALUES('$Kwdikos_Proiontos', '$Proion', '$Kwdikos_Tupou', '$dd')");
//check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
}
else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
else
{
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
ps. my db is already in utf8 cause i display and add other data in greek, by using SELECT , INSERT, in the db.
Finally i found a solution to my problem, i used GET method instead of POST, and now it works correctly! Thnx for the sql injection tips, i will read more about this issue!

Categories