I have a field ,if user enters data it should go and check db if it is present it should redirect to next page.
But here i m not sure whether it is checking the db but mysql query is correct.
$ThirdPartyCategoryName =$_POST['ThirdPartyCategoryName'];
$activate = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` = 'y' ");
if($activate=='y')
{
header("location:catact.php");
}
else
{
//$activate=='NULL';
header("location:tp_home.php");
}
mysql_query returns a ressource that you need to parse with function such as mysel_fetch_array. Your first condition will always be false, you have to parse the ressource before comparing the result contained in it.
$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` = 'y'");
$firstLine = mysql_fetch_array($ressource);
if ($firstLine && $firstLine['delete'] == 'y') {
// There is an entry with delete = y
} else {
// There is no entry with delete = y
}
This code could also be simplify to just this :
$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` = 'y'");
if (mysql_fetch_array($ressource)) {
// There is an entry with delete = y
} else {
// There is no entry with delete = y
}
Or also :
$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` = 'y'");
if (mysql_num_rows($ressource) > 0) {
// There is an entry with delete = y
} else {
// There is no entry with delete = y
}
Related
I'm having an issue trying to update my database. I'm able to insert if it doesn't exist, but it doesn't update the record if exists. I think is not finding the current values of the table. Any ideas?
<?php
function add_log($input_output, $l_sales, $l_enroll, $l_offers)
{
global $database;
$date = date('m-d-Y');
$l_sales = safety_filter($l_sales);
$l_enroll = safety_filter($l_enroll);
$l_offers = safety_filter($l_offers);
$cusersup = get_the_current_user('u_manager');
$cuseropm = get_the_current_user('u_opsmanager');
$cuserid = get_the_current_user('id');
$cuser = get_the_current_user('user_name');
if($input_output == 'input')
{
$query_call = mysql_query("SELECT 1 FROM $database->log WHERE l_date='$date' AND l_user_name='$cuser'");
if(mysql_num_rows($query_call) > 0)
{
while($list_calls = mysql_fetch_assoc($query_call))
{
$old_calls = $list_calls['l_calls'];
$old_sales = $list_calls['l_sales'];
$old_enroll = $list_calls['l_enroll'];
$old_offers = $list_calls['l_offers'];
}
$update = mysql_query("UPDATE $database->log SET
l_call=[$old_calls] + [1],
l_sales=[$old_sales] + [$l_sales],
l_enroll=[$old_enroll] + [$l_enroll],
l_offers=[$old_offers] + [$l_offers]
WHERE l_date='$date' AND l
_user_name='$cuser'");
if(mysql_affected_rows() > 0){return true; }
else { if($update == true){ return true; } else { return false; } }
}
else
{
mysql_query("INSERT INTO $database->log (l_date, l_user_name, l_calls, l_sales, l_enroll, l_offers) VALUES ('$date', '$cuser', '1', '$l_sales', '$l_enroll', '$l_offers')");
if(mysql_affected_rows() > 0){return true; }
else{ return false; }
}
}
}
?>
I think the problem is that you are selecting 1 ("SELECT 1 FROM..."), so you are never getting any of the values from the table (just the number "1"). This tells you something is there, but doesn't actually return any values.
Try selecting all fields ("SELECT * FROM..."), or at least list the fields you need to do the update.
My current code seems to delete every row in the table.
Need to delete only the ones that are unchecked.
here is my code:
$assigned_array = array($_POST['employee_assigned']);
$assigned_employees = implode (",", $assigned_array);
// update unchecked assignments
// get all employees and delete the ones not in the checked list
$get_emp_sql = "SELECT * FROM `accounts` WHERE `permissions` > 0";
$get_emp_result = mysqli_query($dblink, $get_emp_sql);
while ($get_emp_row = mysqli_fetch_array($get_emp_result)) {
// delete rows that are not checked
$delete_assignment = "DELETE FROM `provider_assignments` WHERE `provider_id`='".$pro_id."' AND '".$get_emp_row['id']."' NOT IN ('".$assigned_employees."')";
if (mysqli_query($dblink, $delete_assignment)) {
} else {
die ("Unassignment error.");
}
}
This currently deletes every row, including the boxes that are checked (which are then added, but then deleted by the issue).
Need to add the checked, and remove any unchecked if they exist.
Created an array of all checked employees ID's.
if (strlen($_POST['employee_assigned']) > 0) {
$assigned_employees = implode (",", $_POST['employee_assigned']);
}
// update unchecked assignments
// get all employees and delete the ones not in the clicked list
$get_emp_sql = "SELECT * FROM `accounts` WHERE `permissions` > 0";
$get_emp_result = mysqli_query($dblink, $get_emp_sql);
while ($get_emp_row = mysqli_fetch_array($get_emp_result)) {
if (strlen($assigned_employees == 0)) {
// delete all rows for this provider
$delete_assignment = "DELETE FROM `provider_assignments` WHERE `provider_id`='".$pro_id."'";
} else {
// delete rows that are not checked
$delete_assignment = "DELETE FROM `provider_assignments` WHERE `provider_id`='".$pro_id."' AND '".$get_emp_row['id']."' NOT IN ('".$assigned_employees."')";
}
if (mysqli_query($dblink, $delete_assignment)) {
} else {
die ("Unassignment error.");
}
}
// update checked assignments
if (strlen($_POST['employee_assigned']) > 0) {
foreach ($_POST['employee_assigned'] as $assign_this_employee) {
// check for existing assignment
$check_exist = "SELECT * FROM `provider_assignments` WHERE `provider_id` = '".$pro_id."' AND `employee_id` = '".$assign_this_employee."'";
$exist_result = mysqli_query($dblink, $check_exist);
$exist_count = mysqli_num_rows ($exist_result);
// if assignment doesn't exist, insert assignment
if ($exist_count == 0) {
// insert sql
$add_assignment_sql = "INSERT INTO `provider_assignments` (`provider_id`, `employee_id`) VALUES ('".$pro_id."', '".$assign_this_employee."')";
// execute sql
if (mysqli_query($dblink, $add_assignment_sql)) {
} else {
die ("assignment error");
}
}
}
}
This code works great.. Any comments are welcome!
I am having a problem when I want to echo "The stock is less than what you want". The problem is the user still can update the cart when the stock in my database less that what the user wants. It should show an error "The stock is less than what you want".
This is my code.
<?php session_start();
require("config.php");
$user = $_SESSION['userlogin'];
$cek = mysql_query("SELECT * FROM transaksitbl WHERE username = '$user' AND status ='0'") or die(mysql_error());
$result = mysql_num_rows($cek);
$data = mysql_fetch_array($cek);
if ($result > 0)
{
$faktur =$data['notransaksi'];
for ($i=1; $i<=$_POST['n']; $i++)
{
$idp = $_POST['id'.$i];
$cari2 = mysql_query("SELECT * FROM barangtbl WHERE id='$idp'") or die(mysql_error());
$row2 = mysql_fetch_array($cari2);
$har = $row2['harga'];
$stock = $row2['stock'];
if($_POST['n'] <= $row2['stock'])
{
echo "The stock is less than what you want";
}
if ($cari2)
{
$jmlubah = $_POST['jumlah'.$i];
$beratnew = $jmlubah*$row2['berat'];
$totubah = $jmlubah*$har;
$query = mysql_query("UPDATE transaksirincitbl SET jumlah = $jmlubah, jumlah_berat = $beratnew, total_berat = $beratnew, subtotal=$totubah
WHERE id ='$idp' and username = '$user' And notransaksi =$faktur") or die(mysql_error());
}
}
}
header ("location:shopping_cart.php");
?>
If i understood you properly the
if($cari2){}
function is executing?
All you are checking there is if the $cari2 variable is true.
Simply make a else statement out of the if($cari2){} statement so that if the stock is less than you wan't the second if statement won't get executed.
So, like this:
if($_POST['n'] <= $row2['stock']){
echo "The stock is less than you want";
}
else {
if($scari2){
$jmlubah = $_POST['jumlah'.$i];
$beratnew = $jmlubah*$row2['berat'];
$totubah = $jmlubah*$har;
$query = mysql_query("UPDATE transaksirincitbl SET jumlah = $jmlubah, jumlah_berat = $beratnew, total_berat = $beratnew, subtotal=$totubah
WHERE id ='$idp' and username = '$user' And notransaksi =$faktur") or die(mysql_error());
} else {
die('Woop, there seems to be a problem with the $scari2 variable. The value is:' . $scari2);
} // END OF INNER ELSE
} // END OF ELSE
And one more thing NEVER forget to use the mysql_real_escape_string() function on a variable before submiting it's value to the database.
I would like to know what code I would need to use to get the values from a row (which would naturally have data from different columns)
I would like to check if the IP address is the same as the Poll ID. As you would expect the IP address is stored in one column, and the poll id in a next column.
$q = mysql_query("SELECT * FROM logs");
while($row = mysql_fetch_array($q))
{
if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
{
$duplicateIP = true;
}//end if
if(($row['poll_id'] == 1))
{
$duplicatePoll = true;
}//end if
}//end while
I realised this code won't work as it will return the poll id to be true as long as it is equal to 1. How do I ensure that it will only return to be true if that poll id matches the IP address? Thanks.
Instead of:
if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
{
$duplicateIP = true;
}//end if
if(($row['poll_id'] == 1))
{
$duplicatePoll = true;
}//end if
You should write:
if($_SERVER['REMOTE_ADDR'] == $row['ip'] && $row['poll_id'] == 1){
$duplicateIP = true;
}//end if
Or change your query:
$q = mysql_query(
"SELECT * FROM logs WHERE ip = '".$_SERVER['REMOTE_ADDR'] .
"' and poll_id = '".$iPollid."' "
);
I have output from a select query as below
id price valid
1000368 69.95 1
1000369 69.94 0
1000370 69.95 0
now in php I am trying to pass the id 1000369 in function. the funciton can execute only if the valid =1 for id 1000368. if it's not 1 then it will throw error. so if the id passed is 1000370, it will check if valid =1 for 1000369.
how can i check this? I think it is logically possible to do but I am not able to code it i tried using foreach but at the end it always checks the last record 1000370 and so it throws error.
regards
Use a boolean variable:
<?php
$lastValid=false;
while($row = mysql_fetch_array($result))
{
if ($lastValid) {
myFunction();
}
$lastValid = $row['valid'];
}
?>
(Excuse possible errors, have no access to a console at the moment.)
If I understand correctly you want to check the if the previous id is valid.
$prev['valid'] = 0;
foreach($input as $i){
if($prev['valid']){
// Execute function
}
$prev = $i;
}
<?php
$sql = "SELECT * FROM tablename";
$qry = mysql_query($sql);
while($row = mysql_fetch_array($qry))
{
if ($row['valid'] == 1)
{
// do some actions
}
}
?>
I really really recommend walking through some tutorials. This is basic stuff man.
Here is how to request a specific record:
//This is to inspect a specific record
$id = '1000369'; //**some specified value**
$sql = "SELECT * FROM data_tbl WHERE id = $id";
$data = mysql_fetch_assoc(mysql_query($sql));
$valid = $data['valid'];
if ($valid == 1)
//Do this
else
//Do that
And here is how to loop through all the records and check each.
//This is to loop through all of it.
$sql = "SELECT * FROM data_tbl";
$res = mysql_query($sql);
$previous_row = null;
while ($row = mysql_fetch_assoc($res))
{
some_action($row, $previous_row);
$previous_row = $row; //At the end of the call the current row becomes the previous one. This way you can refer to it in the next iteration through the loop
}
function some_action($data, $previous_data)
{
if (!empty($previous_data) && $condition_is_met)
{
//Use previous data
$valid = $previous_data['valid'];
}
else
{
//Use data
$valid = $data['valid'];
}
if ($valid == 1)
{
//Do the valid thing
}
else
{
//Do the not valid thing
}
//Do whatever
}
Here are some links to some good tutorials:
http://www.phpfreaks.com/tutorials
http://php.net/manual/en/tutorial.php