Logical Issue ifelse loop - php

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
}
}

Related

POST data to php api

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

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!';
}

when I submit the form no action happen

sorry if my question not scene, but when I submit my form no action happen and even no error
appear and I don't know why so please can you help me !!
here my php code
<?php
if (isset ($_POST["submitted"]))
{
if (isset($_POST["proName"]))
{
$namepro=$_POST["proName"];
}
$filename= $_FILES["imgfile"]["name"];
if ((($_FILES["imgfile"]["type"] == "image/gif")|| ($_FILES["imgfile"]["type"] == "image/jpeg") || ($_FILES["imgfile"]["type"] == "image/png") ||
($_FILES["imgfile"]["type"] == "image/pjpeg")) && ($_FILES["imgfile"]["size"] < 200000))
{
if(file_exists($_FILES["imgfile"]["name"]))
{
echo "File name exists.";
}
else
{
move_uploaded_file($_FILES["imgfile"]["tmp_name"],"uploads/$filename");
}
}
else
{
echo "invalid file.";
}
if (isset($_POST["selectcat"]))
{
$selectpro=$_POST["selectcat"];
}
if (isset($_POST["shortDescr"]))
{
$desc=$_POST["shortDescr"];
}
else
{$desc=NULL;}
if (isset($_POST["cost"]))
{
$cost=$_POST["cost"];
}
else
{$cost=NULL;}
if (isset($_POST["product"]))
{
$product=$_POST["product"];
}
else
{$product=NULL;}
if (isset($_POST["marketing"]))
{
$mark=$_POST["marketing"];
}
else
{$mark=NULL;}
if (isset($_POST["power"]))
{
$p=$_POST["power"];
}
else
{$p=NULL;}
if (isset($_POST["risk"]))
{
$risk=$_POST["risk"];
}
else
{$risk=NULL;}
if (isset($_POST["compititiors"]))
{
$comp=$_POST["compititiors"];
}
else
{$comp=NULL;}
$teamWork='';
if (isset($_POST["team1"]))
{
$team=$_POST["team1"];
}
if (isset($_POST["s"]))
{
$s=$_POST["s"];
$teamWork=$team."\t\t".$s;
}
if (isset($_POST["team2"]))
{
$team2=$_POST["team2"];
$teamWork=$team."\t\t".$s."<br>".$team2;
}
else
{$team2=NULL;}
if (isset($_POST["s2"]))
{
$s2=$_POST["s2"];
$teamWork=$team."\t\t".$s."<br>".$team2."\t\t".$s2;
}
else
{$s2=NULL;}
if (isset($_POST["team3"]))
{
$team3=$_POST["team3"];
$teamWork=$team."\t\t".$s."<br>".$team2."\t\t".$s2."<br>".$team3."\t\t";
}
else
{$team3=NULL;}
if (isset($_POST["s3"]))
{
$s3=$_POST["s3"];
$teamWork=$team."\t\t".$s."<br>".$team2."\t\t".$s2."<br>".$team3."\t\t".$s3;
}
else
{$s3=NULL;}
$dbc = mysqli_connect("localhost", "root", "", "gettogether");
$q = "INSERT INTO project (projectname,projecttype,personid,imgProject,status,createDate) VALUES
('$namepro','$selectpro',1,'uploads/$filename','unsubmitted',now())";
$r = #mysqli_query ($dbc, $q);
if ($r ) {
$sql="select projectid from project where personid=1 order by createDate desc";
$qur=mysql_query($sql) or die(mysql_error());
if($qur){
$row=mysql_fetch_array($qur);
$proID=$row['projectid'];
$result2 = "INSERT INTO plan (projectid,description,products,marketingplan,financialplan,strenght,risk,team,competitor) VALUES
($proID,'$desc',$product','$mark','$cost','$p','$risk','teamWork','$comp')";
$result=#mysqli_query ($dbc,$result2) or die(mysql_error());
if ($result)
{
header( "Location:project.php" );
}
else
{
echo "error";
}
}
}
else
{ echo" <script>
alert('try again');
</script>
";
}
}
?>
Note :
in my Database I have 2 table one called plan and another called project
and projectid is a foreign key in plan table
before if statement where you are check is submitted is setted put this code to see what is received
echo '<pre>';
print_r($_POST);
echo '</pre>';

Header location issue. Cant seem to find the issue in if else statement

I have some issues with the following PHP code:
require '../core/connection.php';
require '../includes/functions.php';
if (isset($_SESSION['user_id'])) {
if (isset($_POST['current_user_password'])) {
$current_user_password = md5($_POST['current_user_password']);
if ($current_user_password != $_SESSION['user_password']) {
header('Location:../edit_credentials.php?edit=password&error=current_password');
} else {
$new_password = $_POST['new_user_password'];
$new_password2 = $_POST['new_user_password2'];
if ($new_password == '' || ($new_password2 == '') {
header('Location:../edit_credentials.php?edit=password&error=new_password_empty');
}
if ($new_password != $new_password2) {
header('Location:../edit_credentials.php?edit=password&error=new_password_not_equal');
} else {
$new_password = md5($new_password);
edit_user_password($user_id,$new_password);
}
}
} else {
echo 'current_user_password not set';
}
} else {
echo 'Session not set';
}
Here is the function as well:
function edit_user_password($user_id,$user_password){
global $db;
$user_password_data = $db->query('
UPDATE `users`
SET `user_password` = "'.$user_password.'" WHERE `user_id` = "'.$user_id.'";');
$_SESSION['user_password'] = $user_password;
$db->query($user_password_data);
header("Location:../edit_credentials.php?saved=password");
}
Everything works besides this part:
if ($new_password == '' || ($new_password2 == '') {
header('Location:../edit_credentials.php?edit=password&error=new_password_empty');
}
When it comes to this part, where the passwords are empty, if i switch out the redirection with a die('test'), it works, but it wont work with the redirect. Any chance you guys know why this is not working?
Thanks in advance for all help.
You should add an exit; after each header('Location:...'); call.
Otherwise the script continues to run.

Categories