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

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

Related

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);

why google recaptcha did not call back in my process?

EDITED: I have registered my domain in admin panel
blabla.org
but it still not worked
so i have some process with google recaptcha verification.
but when i click the button process.
$response cant detected and it given null content.
here is my code
$site_key = 'secret';
$secret_key = secret';
if(isset($_POST['req_token'])){
if(isset($_POST['g-recaptcha-response']))
{
$api_url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secret_key . '&response='.$_POST['g-recaptcha-response'];
$response = file_get_contents($api_url);
$data = json_decode($response);
if($data['success'])
{
$nama =$_POST['nama'];
$email =$_POST['email'];
$telp_1 =$_POST['telp_1'];
$status_id = 1;
$sql="INSERT INTO data_recruitment
( status_id,
nama,
email,
telp_1
)
VALUES
( '$status_id',
'$nama',
'$email',
'$telp_1'
)";
if(mysqli_query($conn, $sql)){ // jika query insert berhasil dieksekusi
header("location:../index.php?status=1");
}
else{
header("location:../index.php?status=2");
}
} else
{
header("location:../index.php?status=4");
}
}
else{
header("location:../index.php?status=3");
}
}
$response not detected in my server. but when i tested in localhost. the process working fine.
any suggestion?
Did you take a look at this? file_get_contents returns empty string
You might want to do a phpinfo() and see what version your server is running.

Redirect function not redirecting [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 6 years ago.
I built a website locally with WAMP server, and everything worked fine, then ported over to web server on GoDaddy, and my redirect function suddenly stopped working.
Simple site at (www.minute.tech), you can test out the one form I have, goes to the form_processing.php, but should redirect back to the index page with a message. When you go back to the index page after the failed redirect, it still shows the proper message of "Booyah!...", and inputs the email into my database.
Any ideas why it won't redirect on my web server, but will on my local WAMP server with the same code? Cheers!
Here's my redirect function in sessions.php:
function redirect_to($new_location) {
header("Location: " . $new_location);
exit();
}
Here's process_email.php where I redirect:
<?php require_once("sessions.php"); ?>
<?php require_once("db_connection.php"); ?>
<?php require_once("functions.php"); ?>
<?php
if(isset($_POST['submit']) && !empty($_POST['email'])){
//Process form
$email = $_POST['email'];
$email = mysql_prep($email);
$techcheck = (isset($_POST['techcheck'])) ? 1 : 0;
// 2. Perform database query
$query = "INSERT INTO signups (";
$query .= " email, techcheck";
$query .= ") VALUES (";
$query .= "'{$email}', $techcheck";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["good_message"] = "Booyah! We will keep you posted on progress.";
redirect_to("../index.php");
} else {
//Failure
$_SESSION["bad_message"] = "Failed to accept email.";
redirect_to("../index.php");
}
} else {
//This can be an accidental GET request
$_SESSION["bad_message"] = "That is not a valid email! Please try again.";
redirect_to("../index.php");
}
?>
You have to start object for header function sometimes
Add this code to all pages
<?php ob_start(); ?>
Do with JS.
<?php
function redirect_to($new_location) { ?>
<script>window.location="<?php echo $new_location; ?>";</script>
<?php } ?>
Remove all the empty spaces. Some server set-ups will be okay with output before the header redirect but the vast majority of servers will not redirect properly. Turn on error reporting and it will probably tell you have output before the redirect:
<?php
// ^---- Just do one open tag
require_once("sessions.php"); // Remove close tag, possible empty space after
require_once("db_connection.php"); // Remove close tag, possible empty space after
require_once("functions.php"); // Remove close tag, possible empty space after
// Remove the close and open php tags
if(isset($_POST['submit']) && !empty($_POST['email'])){
//Process form
$email = $_POST['email'];
$email = mysql_prep($email);
$techcheck = (isset($_POST['techcheck'])) ? 1 : 0;
// 2. Perform database query
$query = "INSERT INTO signups (";
$query .= " email, techcheck";
$query .= ") VALUES (";
$query .= "'{$email}', $techcheck";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["good_message"] = "Booyah! We will keep you posted on progress.";
redirect_to("../index.php");
} else {
//Failure
$_SESSION["bad_message"] = "Failed to accept email.";
redirect_to("../index.php");
}
} else {
//This can be an accidental GET request
$_SESSION["bad_message"] = "That is not a valid email! Please try again.";
redirect_to("../index.php");
}
// If you have no more content below this point, just remove the close php tag
// it is not required and is a possible source of empty space down the line...
Also, you should not be using mysql_ anymore, it is deprecated and removed in PHP 7. Also, bind parameters instead of doing this parameter right into the sql:
$query .= "'{$email}', $techcheck";

Ruby Sinatra grab POST

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.

Want to redirect to some page automatically in php

When I submit a form.php it call insert.php. And insert.php insert form data in database and echo a message "record successfylly added.". I want that after displaying this message automaticllay retrieve to the form.php. I also commented the line where i have to redirect.
My form.php
<html>
<body>
<form action="insert.php" method="post">
Name: <input type="text" name="myname"><br>
Comment: <input type="text" name="mycomment"><br>
<input type="submit">
</form>
</body>
</html>
My insert.php
<?php
$con=mysqli_connect("localhost","root","","commentdb");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['myname']);
$lastname = mysqli_real_escape_string($con, $_POST['mycomment']);
$sql="INSERT INTO person (Name, Comment, Time)
VALUES ('$firstname', '$lastname', NOW())";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
// Here it should retrieve
mysqli_close($con);
?>
You can do it using header function :
header( "refresh:10;url=form.php" );
page will be redirect after 10 second.
You can redirect to another page with PHP using the Location header.
Be careful to remove any echo before the header. You can't use header() after output started (and this is useless if you plan to redirect visitor).
Example :
header("Location: form.php");
Adding to Kevin's comment concerning use of Location. I have a site built with this where I pass messages from the 'control' (insert.php in your case) to the 'View' (form.php in your case).
In the 'Control' i set a session variable with the message, eg:
$_SESSION["mess"] = "BOOK0015";
and then use:
header ("Location:UIATbooks.php?id=".$authorid)
to move back to my 'View'.
In the 'View' I look for messages and display, eg:
if (isset($_SESSION['mess'])) {
$mess = $_SESSION['mess'];
}
else {
$mess = "";
}
if ($mess != "") {
$objmess = new clsmessage ();
echo '<p><span class="style3">'.$objmess->returnmess ($objscreen->connection,$mess).'</span></p>';
unset($_SESSION['mess']);
}
$objscreen->endscreen ();
I have a class that stores the message based on ID.

Categories