Ruby Sinatra grab POST - php

I have a PHP file which receives a value in a POST request (it is just a test file) and insert it in MySQL. Here's the code:
require_once('config.inc.php');
$str_values = $_POST['Msg'];
$sender=$_POST['SENDER'];
$trans_log = mysqli_query($dbhandle,"INSERT INTO GPRSIN (SMSFR,SMSMSG)
VALUES ('".$sender."','".$str_values."')");
if($trans_log == 0){
//return ref no and status
echo "Insert Failed";
} else {
echo "1";
}
I want to convert it into Ruby or Sintra but I don't know how to get the POST values from the URL and insert it to MySQL.

Related

Why am I getting a null value on insertion?

I have a react app that makes a post request to the following code.
<?php
require_once('dbSURL.php');
$stm = $db->prepare("INSERT INTO urlshortener (longUrl, shortId, userIp, userCountry, userCity, userOS, userAgent, createDate) VALUES (:longUrl, :shortId, :userIp, :userCountry, :userCity, :userOS, :userAgent, :createDate)");
$stm->bindParam(':longUrl', $_POST['longUrl']);
$stm->bindParam(':shortId', $_POST['shortId']);
$stm->bindParam(':userIp', $_POST['userIp']);
$stm->bindParam(':userCountry', $_POST['userCountry']);
$stm->bindParam(':userCity', $_POST['userCity']);
$stm->bindParam(':userOS', $_POST['userOS']);
$stm->bindParam(':userAgent', $_POST['userAgent']);
$stm->bindParam(':createDate', $_POST['createDate']);
if ($stm->execute()) {
echo "success";
} else {
echo "failure: ".$stm->errorInfo()[2];
};
// $stm->execute();
// echo 'Error: %s.\n', $stm->errorCode(), $stm->errorInfo();
?>
After checking the request from dev tools I can see that the data are sent but when I get the data from the database all the fields are null.
Any help?

RUN API inside SQL database upload

I have an API to do an action for a site.
/api/api.php?api=server&ver=1.0&key=**&cmd=ADD_OBJECT,862011228001930
The API works and does what it needs to be done. The API information is populated from a form I have. the form also needs to upload information to MYSQL database using SQL
I have the following SQL
$sql = "INSERT INTO tracking_units SET
status = 'Tech',
unit_nr = '".$unit_nr."',
imei = '".$imei."',
sysdate = '".$date."',
systime = '".$time."',
controller = '".$slname."',
branch = 'JHB',
client = 'eTrack'";
if ($conn->query($sql) === TRUE)
{$last_id = mysqli_insert_id($conn); //Retrieve the last ID of the record
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
header("Location: http://***/api/api.php?api=server&ver=1.0&key=***&cmd=ADD_OBJECT,$imei");
The above works as the header runs the API, but once the API is completed I would like to redirect the page to another html page. Is this possible with me running the header command.
I am very new to API's and not sure how else I can run the API after the SQL command and then use the header to divert to another page
I have found this to work
$ch = curl_init("http://***/api/api.php?api=server&ver=1.0&key=***&cmd=ADD_OBJECT,$imei,$unit_nr,false,2021-01-01");
curl_exec($ch);
if (curl_error($ch)) {
fwrite($ch, curl_error($ch));
}
curl_close($ch);
fclose($ch);

PHP _POST['field'] is empty when using API url

I followed the below online tutorial to build a PHP API.
https://trinitytuts.com/restful-web-services-with-php-mysqli/
The HTTP request works perfectly when I use Chrome's "Advanced Rest Client". I get the following response:
{
"status": 1
"msg": "Your record inserted successfully"
}
I tried to put the following URL directly into my web browser:
http://localhost/API/insert.php/name=paul&email=paul#domain.com&status=active
I expected the same result as I received when I used the Advanced Rest Client however, I received the following error:
Notice: Undefined index: name
I am setting the variable 'name' using the following PHP code:
$name = $_POST['name'];
My understanding is that when I paste the URL into the browser, the $name variable is not set to $_POST['name'] because entering the URL is not actually a post action.
Is it possible for me add a parameter to the URL so as to tell the browser to post? For example:
http://localhost/API/insert.php/type=POST&name=paul&email=paul#domain.com&status=active
In a nutshell, I want to be able to past a URL directly into a web browser and and not have to post it via a HTML form.
This is my PHP code for insert.php
<?php
include 'conn.php';
$name = $_POST['name'];
$email = $_POST['email'];
$status = $_POST['status'];
$id = 1;
$sql = "INSERT INTO `service`.`user` (`id`, `name`, `email`, `status`) VALUES ($id, '$name', '$email', '$status');";
if ($connection->query($sql)) {
$msg = array("status" =>1 , "msg" => "Your record inserted successfully");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
$json = $msg;
header('content-type: application/json');
echo json_encode($json);
#mysqli_close($conn);
?>
Issue has been resolved by changing $_POST to $_GET in insert.php

get the result from mysql+php and shows in titanium

I have developing the one application using titanium.
Here the values are inserted successfully.But i didn't get the success message from webservices code.
I have using following code for insert a databaase :
In titanium side code :
function StaffRegistration(){
if($.staff_firstname.value != "" && $.staff_firstname.value != null){
var request = Ti.Network.createHTTPClient({
onload:alert(this.responseText),
onerror: function(e){
Ti.API.debug(e.error);
alert(this.responseText);
},
timeout:1000,
});
request.open("POST","xxx/xxx.php");
var params = ({"staff_firstname": $.staff_firstname.value,"staff_email": $.staff_email.value,"staff_password": $.staff_password.value,});
request.send(params);
}
else{
alert("Please enter the firstname");
}
Ti.API.info("Result for registration = " + this.responseText);
};
I have using a following php(webservice code) :
<?php
$request = base64_decode($_POST['jsondata']);
$data = json_decode($request,true);
$staff_firstname = $data['staff_firstname'];
$staff_email = $data['staff_email'];
$staff_password = md5($data['staff_password']);
include "db_connect.php";
$db = new DB_CONNECT();
$result = mysql_query("SELECT staff_email,staff_firstname from at_staff WHERE staff_email = '$staff_email'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
while($queryresult=mysql_fetch_array($result)) {
$uname[]=$queryresult['staff_firstname'];
$uemail[]=$queryresult['staff_email'];
}
if(in_array($staff_firstname,$uname) and in_array($staff_email,$uemail)) {
$response='{"Error":"1","Message":"Username and Email already exist"}';
echo $response;
} else if (in_array($staff_firstname,$uname)) {
$response='{"Error":"1","Message":"Username already exist"}';
echo $response;
} else {
$response='{"Error":"1","Message":"Email already exist"}';
echo $response;
}
} else {
$response='{"Error":"1","Message":"Successfully Registered"}';
echo $response;
$data=array("staff_firstname"=>"'".$staff_firstname."'",
"staff_email"=>"'".$staff_email."'",
"staff_password"=>"'".$staff_password."'"
);
echo $response;
}
?>
How can i get the $response in titanium from this webservice url.
#user2218667
Ti.API.info("Result for registration = " + this.responseText);
will NEVER work as you show in the first piece of code .
why ? because you send a request which will take like 1 seconde (for exemple), obviously, your programm won't wait 1 sec after
request.send(params);
i will continue the programm and when the request return a result, it will get into
onload(e) :
and here only you will be able to have your $result.
is this ok? well now, if this.responseData isn't effective, I don't have the solution .Can you check your line : "} else {" ,i supose there is a if above in the code? are you sure $result is defined upper?
Can you try the same request without titanium with a basic html form to be sure $result is write correctly in this case, like this, we will know if the problem come from php or from the link betwin php & titanium.
well , i supose it's asynchronous request, so the folowing may not work
Ti.API.info("Result for registration = " + this.responseText);
coud you try :
onload : function(e) {
Ti.API.info(this.responseText); // maybe Ti.API.info(this.reponsedata) according to your php.
},
onerror : function(e) {...
in my mind, if you receive JSON information (it trully look's like), you need
this.responseData //instead of this.responseText

From PHP to Javascript

I have the following code on my PHP webapp, it uses PHP to look for products in a Sqlite3 Databse file locally. I was tryng to convert it into an Android App with Phonegap, but sadly Phonegap does't work with PHP, so I thought I could make a JavaScript file replacing the PHP.
However, I can't get it to work. In case what I am trying to do is impossible, I guess there should be a way to execute the PHP files on the server and just display the results on the app.
($_GET['busq']){
$busq=$_GET['busq'];
function ultlet($cadena) {
$cant = strlen($cadena);
$cant--;
$let = substr($cadena, $cant);
if ($let == "s") {
$cadena = substr($cadena, 0, $cant);
}
return $cadena;
}
str_replace("-",$busq," ");
$arreg=explode(" ",$busq);
$cuent=count($arreg);
for($a=0;$a<$cuent;$a++){
$arreg[$a] = ultlet($arreg[$a]);
}
$query="SELECT * from MyTable WHERE";
for($i=0;$i<$cuent;$i++){
if ($i>=1){
$query=$query." AND ";
}
$query=$query." (col1 LIKE \"%$arreg[$i]%\" OR col6 LIKE \"%$arreg[$i]%\") ";
}
$db = new PDO('sqlite:MyDatabase.sqlite');
$result = $db->query($query);
foreach ($result as $row) {
if ($row['codigo_cat']!=null){
$class = 'class="image"';
}else{
$class = 'class="row"';
}
echo '<div '.$class.' alt="'.$row['col4'].'">';
echo 'Codigo: '.$row['col2']." / ".$row['col3']."<br>";
echo 'Nombre: '.$row['col1']."<br>";
echo '</div>';
}
}
There is also another file wich based on the user's input on a search bar creates the variable 'busq' which is used to display the products and its prices.
Modify the PHP script to output its contents as an xml file, and use javascript's XMLHttpRequest function to create a request to the file, and get the information you need.

Categories