PHP Form IF Issues - php

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.

Related

AJAX update and delete error but still successful error

**
I have a problem when updating and deleting, when I incorrectly enter the column id, although the database does not change, the system still reports
success **
$loai = trim($_POST['loai']);
$id = trim($_POST['id']);
$ten = trim($_POST['ten']);
$idghichu = trim($_POST['idghichu']);
$sql;
$conn = new mysqli('localhost','root','12345678','chamcomvantay');
if($loai == "xoa") {
$sql = "DELETE FROM chamcomvantay.tb_bophan WHERE ID='$id' ";
}
elseif ($loai == "sua") {
$sql = "UPDATE chamcomvantay.tb_bophan SET TENBP='$ten', GHICHU='$idghichu' WHERE ID='$id' ";
}
if(mysqli_query($conn, $sql)) {
echo "success"; // This is an error //
} else {
echo "Lỗi";
}
$conn->close();
?>
--my ajax code----
$idbophan = $('#idbophan').val();
$idten = $('#idten').val();
$idghichu = $('#idghichu').val();
$loai = "sua";
if($idbophan.trim() != "" ){
$.post('thaotacbophan.php',{'id':$idbophan.trim(),'ten': $idten.trim(),'idghichu':$idghichu.trim(),'loai':$loai.trim()}, function(data){
alert(data); // error but still alert("success"); ///
});
}
else{
alert("Vui lòng điền đầy đủ thông tin");
}

"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.

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

Warning: Missing argument 2 for addProfile()

I know there are already quite a few articles on SE about "Warning: Missing argument 2 for" questions, although I couldn't really seem to find an answer (even after looking over all of the other questions several times).
First Error Set: http://i.stack.imgur.com/HTySO.png
Second Error Set: http://i.stack.imgur.com/wDwxm.png
(I tried posting as SE images, but since I'm new it wouldn't let me)
Those are the two errors I'm currently getting 20 times (I have 20 different fields for the database, it's a "profile" section).
I've spent the better half of two hours trying to figure out why it's not working but I'm clueless.
addProfile.php :
<?php
include('../includes/functions.php');
if(isset($_POST['submit'])) {
if(isset($_POST['profile_name'])) {
addProfile($_POST['profile_name']);
} else {
echo "Please Enter A Profile Name!";
include('manage_settings.php');
}
if(isset($_POST['profile_description'])) {
addProfile($_POST['profile_description']);
} else {
echo "Please Enter A Profile Description!";
include('manage_settings.php');
}
if(isset($_POST['first_name'])) {
addProfile($_POST['first_name']);
}
if(isset($_POST['last_name'])) {
addProfile($_POST['last_name']);
}
if(isset($_POST['company'])) {
addProfile($_POST['company']);
}
if(isset($_POST['office_phone'])) {
addProfile($_POST['office_phone']);
}
if(isset($_POST['cell_phone'])) {
addProfile($_POST['cell_phone']);
}
if(isset($_POST['fax_num'])) {
addProfile($_POST['fax_num']);
}
if(isset($_POST['email_addr'])) {
addProfile($_POST['email_addr']);
}
if(isset($_POST['website'])) {
addProfile($_POST['website']);
}
if(isset($_POST['motto'])) {
addProfile($_POST['motto']);
}
if(isset($_POST['street_addr'])) {
addProfile($_POST['street_addr']);
}
if(isset($_POST['city'])) {
addProfile($_POST['city']);
}
if(isset($_POST['state'])) {
addProfile($_POST['state']);
}
if(isset($_POST['zip'])) {
addProfile($_POST['zip']);
}
if(isset($_POST['country'])) {
addProfile($_POST['country']);
}
if(isset($_POST['facebook_url'])) {
addProfile($_POST['facebook_url']);
}
if(isset($_POST['places_url'])) {
addProfile($_POST['places_url']);
}
if(isset($_POST['twitter_url'])) {
addProfile($_POST['twitter_url']);
}
if(isset($_POST['linkedin_url'])) {
addProfile($_POST['linkedin_url']);
}
} else {
header("Location: manage_settings.php");
}
?>
functions.php :
<?php
include('connect.php');
function getProfiles() {
$query = mysql_query("SELECT * FROM global_profiles") or die(mysql_error());
if(mysql_num_rows($query) == 0) {
echo "<center><h3><b><u>No Profiles Currently Available</u></b></h3></center>";
} else {
while($profile = mysql_fetch_assoc($query)) {
echo "<tr><td><input type=\"checkbox\" /></td><td>" . $profile['profile_name'] . "</td><td>Coming Soon</td><td>" . $profile['profile_description'] . "</td><td>" . $profile['pid'] . "</td><td><img src=\"images/pencil.png\" alt=\"Edit\" /> <img src=\"images/cross.png\" alt=\"Delete\" /> <img src=\"images/hammer_screwdriver.png\" alt=\"Duplicate\" /></td></tr>";
}
}
}
function deleteProfile($pid) {
$pid = (int) $pid;
mysql_query("DELETE FROM global_profiles WHERE pid = '$pid'") or die(mysql_error());
header("Location: manage_settings.php");
}
function addProfile($pid, $profile_name, $profile_description, $first_name, $last_name, $company, $office_phone, $cell_phone, $fax_num, $email_addr, $website, $motto, $city, $state, $zip, $country, $facebook_url, $places_url, $twitter_url, $linkedin_url) {
$query = mysql_query("INSERT INTO global_profiles VALUES(null,'$profile_name','$profile_description','$first_name','$last_name','$company','$office_phone','$cell_phone','$fax_num','$email_addr','$website','$motto','$city','$state','$zip','$country','$facebook_url','$places_url','$twitter_url','$linkedin_url')") or die(mysql_error());
}
?>
What I'm trying to do is basically "save" new information to my database. I've been able to manually add it into the database via phpMyAdmin, then display the information inside my admin area.
Any assistance is much appreciated!
Modify your addProfile() to:
function addProfile($post) {
// Here check wether you have certain post array key set and add it to query
}
and use it as :
if(isset($_POST['profile_name'])) {
addProfile($_POST);
}

Categories