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.
Related
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¶m2=value2
see this image : https://pasteboard.co/JJt9RB4.png
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.
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
}
}
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>';
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.