POST data to php api - php

I am new to PHP and SQL coding and have been trying to POST data to my database, but when using Postman to check responses I get my else response and I really don't know why because my POST data is exactly what is needed.
Here is a screenshot
Thank you very much for helping me out!
As per request here is the code
<?php
require("../database_android.php");
if(isset($_POST["package_name"]) && $_POST["package_name"] != "") {
$package_name = mysql_real_escape_string($_POST["package_name"]);
} else {
echo "package_name missing";
return;
}
if(isset($_POST["app_name"]) && $_POST["app_name"] != "") {
$app_name = mysql_real_escape_string($_POST["app_name"]);
} else {
echo "app_name missing";
return;
}
if(isset($_POST["activity"]) && $_POST["activity"] != "") {
$activity = mysql_real_escape_string($_POST["activity"]);
} else {
echo "activity missing";
return;
}
if(isset($_POST["activity_name"]) && $_POST["activity_name"] != "") {
$activity_name = mysql_real_escape_string($_POST["activity_name"]);
} else {
echo "activity_name missing";
return;
}
if(isset($_POST["component_info"]) && $_POST["component_info"] != "") {
$component_info = mysql_real_escape_string($_POST["component_info"]);
} else {
echo "component_info missing";
return;
}
$application_id = already_exists_application($package_name);
if($application_id < 0) {
mysql_query("INSERT INTO androidactivities_application (package_name, app_name) VALUES ('". $package_name ."', '". $app_name ."')");
$application_id = already_exists_application($package_name);
}
$componentinfo_id = already_exists_componentinfo($application_id, $component_info);
if($componentinfo_id < 0 && $application_id > 0) {
mysql_query("INSERT INTO androidactivities_componentinfo (application_id, activity, activity_name, component_info) VALUES ('". $application_id ."', '". $activity ."', '". $activity_name ."', '". $component_info ."')");
echo "successful";
} else {
echo "exists";
}
?>
<?php
function already_exists_application($package_name) {
$result = mysql_query("SELECT * from androidactivities_application WHERE package_name = '".$package_name."'");
if($row = mysql_fetch_object($result)) {
return $row -> application_id;
}
else {
return -1;
}
}
function already_exists_componentinfo($application_id, $component_info) {
$result = mysql_query("SELECT * from androidactivities_componentinfo WHERE application_id = '".$application_id."' AND component_info = '".$component_info."'");
if($row = mysql_fetch_object($result)) {
return $row -> componeninfo_id;
}
else {
return -1;
}
}
?>

Please post your data in Body tag as form data in postman. the way you are sending it gose like this ApiURL?param1=value1&param2=value2
see this image : https://pasteboard.co/JJt9RB4.png

Related

"insert_record($table, $dataObjects)" not inserting email into database table

I am trying to insert email into the table newsletter but it is not inserting into database
This is the code:
PHP
$table = 'news_letters';
if(isset($_REQUEST['mode'])) {
extract($_REQUEST);
$email=$_POST['email'];//print_r($email);die;
if (isset($_POST['email']) && trim($_POST['email'])=="submitNewsletter") {
$condition = " WHERE email='$email'";
$res = $obj_common->select_record('news_letters', $condition);
if (count($res) > 0) {
echo 'already register';
header("location:index.html?msg=already_register");
die;
}
}
if(trim($_POST['mode'])=="submitNewsletter")
{
$condition = " WHERE email='$email'";
$res = $obj_common->select_record('news_letters', $condition);
if (count($res) > 0) {
echo 'already register';
header("location:thankyou.html?msg=already_register");
die;
}
else
{
$data=array('email'=>"$email",'status'=>'$status');
$res=$obj_common->insert_record('news_letters', $data); //print_r($res);die;
header("location:thankyou.html?msg=newsletter_add_success");
die;
}
}
}
?>
Email is not inserting into the table newsletter.

mysqli_num_rows is no working?

I am trying to do if, which will check if value is stored in the mysql database but I do not understand why it does not work. Is this how you can save it?
$przelew_nrkonta = $_POST['przelew_nrkonta'];
if (mysqli_num_rows($przelew_nrkonta) <= 0) { ... }
My all code:
$przelew_nrkonta = $_POST['przelew_nrkonta'];
$przelew_pieniadze = $_POST['przelew_pieniadze'];
$sql1 = sprintf("UPDATE kontabankowe SET pieniadze = pieniadze + '%s' WHERE nrkonta= '%s'",
mysqli_real_escape_string($polaczenie, $przelew_pieniadze),
mysqli_real_escape_string($polaczenie, $przelew_nrkonta));
$sql2 = sprintf("UPDATE kontabankowe SET pieniadze = pieniadze - '%s' WHERE nrkonta = '%s'",
mysqli_real_escape_string($polaczenie, $przelew_pieniadze),
mysqli_real_escape_string($polaczenie, $_SESSION['nrkonta']));
if ($polaczenie->connect_errno != 0) {
echo "Error: " . $polaczenie->connect_errno;
} else {
if (mysqli_num_rows($przelew_nrkonta) <= 0) {
echo "zly numer konta";
//header('Location: errors/error2.php');
} else {
if ($_SESSION['pieniadze'] < $przelew_pieniadze || $_SESSION['pieniadze'] <= 0) {
header('Location: errors/error1.php');
} else {
if ($polaczenie->query($sql1) && ($polaczenie->query($sql2)) === TRUE) {
header('Location: errors/favorably.php');
} else {
header('Location: errors/error3.php');
}
}
}
}
The input of the function mysqli_num_rows should be a mysqli_query.

PHP API returning wrong response for android app

I am creating a API for android developer in PHP in which he want to delete some values from database and want to show a message after that.
Now the problem is this data is deleting successfully but this API always shows else part message after complete the process. If I remove the else part its return the null which crash the android app. So I just want to give a proper json message to the android developer
Here is the code which I am trying
if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes" && $user_name_id == $user_name_id1)
{
$tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_count as $table_count)
{
$user_count = mysql_query("select * from $table_count where user_name = '$user_name'");
$total_user_count = mysql_num_rows($user_count);
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
if($user_sql)
{
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}
}
}
else
{
$response['success'] = 0;
$response['user']['error_msg'] = 'Record Not Found!';
}
}
}
I know there is something wrong with this logic. But I need expert advise where my logic is wrong and what I have to do make it success
Problem with your original code, is that you are setting success/failure inside the loop. One of the four table may/may not contain the username. And if the last table don't have that, then as per your logic you are getting "record not found" even if previous iteration of the loop deleted data from the tables where username exists.
<?php
$conn = mysqli_connect(.....);
if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes" && $user_name_id == $user_name_id1) {
$tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
$userHistoryDeleted = 0;
foreach($tables_count as $table_count) {
//if history is found, then it will be deleted otherwise not
mysql_query("delete from $table_count where user_name = '$user_name'");
if(mysqli_affected_rows($conn)) {
$userHistoryDeleted = 1;
}
}
$msg = 'Record Not Found!';
if($userHistoryDeleted) {
$msg = 'Clear Successfully All History!';
}
$response['success'] = $userHistoryDeleted;
$response['user']['error_msg'] = $msg;
}
Change your code :
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
if($user_sql)
{
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}
}
}
else
{
$response['success'] = 0;
$response['user']['error_msg'] = 'Record Not Found!';
}
to this one
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
}
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}

Logical Issue ifelse loop

I am trying to post data in updated section but I can't. I am using ajax method for posting. The problems in ifelse loop because if i remove all three if else loop then the data will be posted.
why i use these ifelse logic because if user didn't select date value from input than value should be NULL
<?PHP
if(isset($_POST['action']) && $_POST['action']=="add")
{
add data
}
if(isset($_POST['action'])&& $_POST['action']=="update") //data update
{
update data
}
if(isset($_POST['action']) && $_POST['action']=="delete") //Action for delere
{
delete data
}
elseif(isset($_POST['action'])&& $_POST['action']=="updated")
{
$id=$_POST['edit_id'];
$istatus=$_POST['edit_istatus'];
$idpicker= $_POST["sdatepicker"][0];
$cstatus=$_POST['edit_cstatus'];
$cdpicker= $_POST["sdatepicker"][1];
$rstatus=$_POST['edit_rstatus'];
$rdpicker= $_POST["sdatepicker"][2];
if($i_date=strtotime($idpicker) == false)
{
$iinsertDate=NULL;
}
else
{
$iinsertDate = date("Y-m-d",strtotime($idpicker));
}
if($c_date=strtotime($cdpicker) == false)
{
$cinsertDate=NULL;
}
else
{
$cinsertDate = date("Y-m-d",strtotime($cdpicker));
}
if($r_date=strtotime($rdpicker) == false)
{
$rinsertDate=NULL;
}
else
{
$rinsertDate = date("Y-m-d",strtotime($rdpicker));
}
$test = mysqli_query($conn,"UPDATE status SET istatus='$istatus',idate='$iinsertDate',cdate='$cinsertDate',rdate='$rinsertDate',rstatus='$rstatus',cstatus='$cstatus' WHERE sid='$id'") or die ("Query Wrong");
echo '{"status":"3"}';
exit;
}
?>
Try using this:
if(isset($_POST['action']) && $_POST['action']=="add")
{
// add data
}
if(isset($_POST['action']) && $_POST['action']=="update")
{
//update data
}
if(isset($_POST['action']) && $_POST['action']=="delete")
{
//delete data
}
if(isset($_POST['action']) && $_POST['action']=="updated")
{
$id=$_POST['edit_id'];
$istatus=$_POST['edit_istatus'];
$idpicker= $_POST["sdatepicker"][0];
$cstatus=$_POST['edit_cstatus'];
$cdpicker= $_POST["sdatepicker"][1];
$rstatus=$_POST['edit_rstatus'];
$rdpicker= $_POST["sdatepicker"][2];
$iinsertDate = NULL;
$cinsertDate = NULL;
$rinsertDate = NULL;
if($i_date=strtotime($idpicker) != false)
{
$iinsertDate = date("Y-m-d",strtotime($idpicker));
}
if($c_date=strtotime($cdpicker) != false)
{
$cinsertDate = date("Y-m-d",strtotime($cdpicker));
}
if($r_date=strtotime($rdpicker) != false)
{
$rinsertDate = date("Y-m-d",strtotime($rdpicker));
}
if($iinsertDate != NULL && $cinsertDate != NULL && $rinsertDate != NULL)
{
$test = mysqli_query($conn,"UPDATE status SET istatus = '$istatus', idate = '$iinsertDate', cdate = '$cinsertDate', rdate = '$rinsertDate', rstatus = '$rstatus', cstatus = '$cstatus' WHERE sid = '$id'") or die ("Query Wrong");
echo json_encode(array("status"=>"3"));
}
else
{
//what ever else you want to do
}
}

PHP Form IF Issues

Ok, so I have this in the top part of my code
require(__DIR__ . "/lib/db.php");
if (!empty($_POST["action"])) {
if ($_POST["action"] == "addCat") {
echo "Please wait...";
$sql1 = "INSERT INTO categories (name) VALUES ('".$_POST["name"]."')";
if (mysql_query($sql1)) {
$success = true;
}
if (!isset($success)) {
echo "Error Occured Adding Category!";
} else {
echo "Category added!";
}
}
if ($_POST["action"] == "delCat") {
echo "Please wait...";
$catNames = $_POST['catName'];
if(empty($catNames)) {
return;
} else {
$N = count($catNames);
for($i=0; $i < $N; $i++) {
$sql = "DELETE FROM categories WHERE id='$catNames[$i]'";
if (mysql_query($sql)) {
$success = true;
}
$sql2 = "SELECT * FROM catrel WHERE categoryID = '$catNames[$i]'";
if (mysql_query($sql2)) {
mysql_query("DELETE FROM catrel WHERE categoryID = '$catNames[$i]'");
}
}
}
if (!isset($success)) {
echo "Could not delete category! Error Occured!";
} else {
echo "Category deleted!";
}
}
}
I have a 2 different forms in the same page, with hidden values that will push addCat or delCat. For some reason it seems like the SQL is not getting parsed, however it is not displaying any errors. Any help would be highly appreciated. I can post the entire page if needed.

Categories