Can't UPDATE all column values only few columns are getting updated - php

I want to update all column values, as you can see from the code. But only the first three columns get updated and last two values get updated.
Home_number
Office_number
Fax_number
does not get updated.
Please help solve the following problem.
if (isset($_POST["submit"]) && $_POST["submit"] !="")
{
$usersCount = count($_POST["F_name"]);
echo "$userCount";
for ($i = 0; $i < $usersCount; $i++)
{
mysql_query("UPDATE contactss set F_name='" .
$_POST["F_name"][$i] . "', L_name='" . $_POST["L_name"][$i] . "', mob_number='" . $_POST["mob_number"][$i] . "', Home_number='" . $_POST["Home_number"][$i] . "', Office_number='" . $_POST["Office_number"][$i] . "', Fax_number='" . $_POST["Fax_number"][$i] . "', email='" . $_POST["email"][$i] . "', Address='" . $_POST["Address"][$i] . "' WHERE C_id='" . $_POST["C_id"][$i] . "'");
}
header("Location:list_user.php");
}

Related

how to use conditional operator in php

$sql = "INSERT INTO `employee_master` ( em_first_name, em_middle_name, em_last_name, em_gender, em_DOB, em_DOJ, em_mobile_no, em_alternate_mobile_no, em_landline_no, em_permanent_address, em_corresponding_address, em_email_id ) VALUES(
'" . $params["txtFirstName"] . "',
'" . $params["txtMiddleName"] . "',
'" . $params["txtLastName"] . "',
'" . $params["selGender"] . "',
'" . $params["txtDOB"] . "',
'" . $params["txtDOJ"] . "',
'" . $params["txtMobileNo"] . "',
'" . $params["txtAlternateMobileNo"] . "',
'" . $params["txtLandlineNo"] . "',
'" . $params["txtPermanentAddressTextarea"] . "',
'" . $params["txtCorrespondingAddressTextarea"] . "',
'" . $params["txtEmailID"] . "'); ";
//echo $sql;
echo $result = mysqli_query($this->conn, $sql) or die("Error to insert User.");
}
($('#selReligion').val() == -1 ) ? " " : $('#selReligion').val(),
Here I want to use Conditional operator.
Right Now..if selGender is null that It inserts -1
but i want to insert 0 or null
Use the ternary operator. It will insert the 0 instead of -1, as you require.
if $params["selGender"] will not be null then it will insert the value otherwise 0. and you can change the condition in that according to your need.
'" . (!empty($params["selGender"])) ? $params["selGender"] : 0 . "',
EDITED:
Other way to achieve this is:
Take a variable:
$selGender = '0';
if(!empty($params["selGender"]))
$selGender = $params["selGender"];
And then use this variable:
'" . $selGender . "',

Variable Insert in MySql Query

I am fetching some row from mysql table and after fetch it I am inserting it in another database with similar type table. I want little changes in one filed of row and want insert fixed value in it instead of value that I have fetched first.
My current query is like below
while ($row = mysqli_fetch_array($questions))
$cat=1;
{
// escape your strings
foreach($row as $key => $val)
{
$row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
}
mysqli_query($mobcon, "INSERT INTO `questions` (`option1`, `option2`, `option3`, `option4`, `correctans`, `question_text`, `cat_id`, `sub_cat_id`, `level_id`, `quesimage`) VALUES ('" . $row['option1'] . "', '" . $row['option2'] . "', '" . $row['option3'] . "','" . $row['option4'] . "','" . $row['correctans'] . "','" . $row['question_text'] . "','" . $row['cat_id'] . "','" . $row['sub_cat_id'] . "','" . $row['level_id'] . "','" . $row['quesimage'] . "');");
}
Now if I want put value 1 for $row['cat_id'] what should I change it in my code ? I have tried to use variable for it but its not working. Thanks

how to check duplicate records in mysql database

$queryb = "SELECT product, imei, country, warranty, config from PRODUCT WHERE product_slno = '$mnserialno' ";
$resultb = mysql_query($queryb, $gndbconn) ;
if(mysql_num_rows($resultb) > 0)
{
$queryc = "UPDATE PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config' WHERE product_slno = '.$mnserialno.' ";
$resutc = mysql_query($queryc, $gndbconn) ;
}
else{
$querya = "INSERT INTO PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config'";
$resulta = mysql_query($querya, $gndbconn) ;
}
I want to check the serial number if that serial number already exist in database so records get update, otherwise it get insert into the database.
but the code inserting the records only, no updation, what is the fault i am not getting,
how to prevent the duplicate entry?
INSERT INTO PRODUCT SET
(`product`, `product_slno`, `imei`, `country`, `warranty`, `config`)
VALUES
('" . $desc . "', '" . $mnserialno . "', '" . $imei . "', '" . $country . "', '" . $warranty . "', '" . $config . "')
ON DUPLICATE KEY UPDATE
product='" . $desc . "',
product_slno='" . $mnserialno . "',
imei='" . $imei . "',
country='" . $country . "',
warranty='" . $warranty . "',
config='" . $config . "'";

I want to show an error if duplicates are there in database using php and mysql

I'm trying to show an error while entering duplicates using php and mysql, but i'm not getting how to complete, please give an solution........
this is my code:
mysql_query(
"INSERT INTO productcost (product, productCategory, model, purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer)
VALUES ('" . $_POST["product"] . "','" . $_POST["productCategory"] . "','" . $_POST["model"] . "','" . $_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','" . $_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','" . $_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");
$current_id = mysql_insert_id();
if(!empty($current_id)) {
$message = "New Product Added Successfully";
}
}
You have to create unique key in productcost table , using unique fields like (product, productCategory, model). Now execute insert query, if there is a recode in the table return error . now you can handle error and give message.
try{
mysql_query("INSERT INTO productcost (product_key_id,product, productCategory,model,purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer)
VALUES
('" . $_POST["created_product_id"] . "','" . $_POST["product"] . "','".$_POST["productCategory"] . "','" . $_POST["model"] . "','".$_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','".$_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','".$_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");
return TRUE;
}
catch(Exception $e){
return FALSE;
}
or you can check is there a recode in table before insert
select count(*) as cc from doc_upload where product_key_id = $_POST["created_product_id"];
To show an error message while entering duplicates:
// First check there are same data available or not using a query by counting the row
$sqlCheck = "SELECT COUNT(`id`) WHERE product = '" . $_POST["product"] . "' AND productCategory = '" . $_POST["productCategory"] . "' AND model = '" . $_POST["model"] . "'"; // You have to add mroe thing in where clause
$CheckQuery = mysql_query($sqlCheck);
// if there is no duplicate data
//
if ($CheckQuery > 0) {
# code...
mysql_query(
"INSERT INTO productcost (product, productCategory, model, purchasePrice, mrp, customerPrice, marginCustomer, dealerPrice, marginDealer)
VALUES ('" . $_POST["product"] . "','" . $_POST["productCategory"] . "','" . $_POST["model"] . "','" . $_POST["purchasePrice"] . "','" . $_POST["mrp"] . "','" . $_POST["customerPrice"] . "','" . $_POST["marginCustomer"] . "','" . $_POST["dealerPrice"] . "', '" . $_POST["marginDealer"] . "')");
$current_id = mysql_insert_id();
if(!empty($current_id)) {
$message = "New Product Added Successfully";
}
} else {
$message = "Data is Duplicated";
}
Note : I'm Giving you an Example . this is how you have to check
duplicate data

Can PHP foreach statements have if/else inside them? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am doing an OpenCart modification to an order total. This is the snippet of the code:
<?php
foreach ($data['totals'] as $total) {
if (
$this->db->escape($total['code'])=="sub_total" ||
$this->db->escape($total['title'])="Sub-Total"
) {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$data['total'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . $_SESSION['GCFinalOrderTotalIncludingDelivery'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
}
}
?>
This is the pseudo code. Can an IF/Else be nested inside the foreach statement?
<?php
foreach ($d['totals'] as $total) {
if ($query=="x" || $query=="y") {
a();
} else {
b();
}
}
?>
Sure they can.
And it is quite hard to use 30 characters to say that.
Sure, it can. Not clear what is your problem, but suppose you have a typo in this line:
$this->db->escape($total['title']) = "Sub-Total"
which should be like this:
$this->db->escape($total['title']) == "Sub-Total"
I think you know by now that: "Yes, they can!". However: if your loop isn't that big, you might want to rethink what the actual difference is between the two branches. Take your pseudo-code example, for instance: if the branch is decided on depending on a value that is not derived from the variables provided by the loop itself, you're better of moving the branch:
foreach($someArr as $k=>$v)
{
if ($someOtherVar === true)
{
echo 'The value is '.$v;
}
else
{
echo 'The key is '.$k;
}
}
Can (and should) be written as:
$str = 'The '.($someOtherVar === true ? 'value' : 'key').' is ';
foreach($someArr as $k=>$v)
{
echo $str.$v;
}
Yes you can do this. It is pretty commonly done too. Make sure you use == when testing though; you have = as your test.
In OpenCart the file you're trying to edit I assume is the Order Model?
/catalog/model/checkout/order.php
You have a problem with missing two == which was mentioned by FAngel but the problem lies with your float. The data attribute gets taken away in this function. Because the Data array is no longer inside that snippet. It relies on TOTAL:
So your line:
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$data['total'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
should be: (notice the NEW (float)$total['value'])
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
Which means it would be:
#######
// WAS (float)$data['value']
// NOW (float)$total['value']
#######
foreach ($data['totals'] as $total) {
if (
$this->db->escape($total['code'])=="sub_total" ||
$this->db->escape($total['title'])=="Sub-Total"
) {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . (float)$total['value'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "order_total SET order_id = '" . (int)$order_id . "', code = '" . $this->db->escape($total['code']) . "', title = '" . $this->db->escape($total['title']) . "', text = '" . $this->db->escape($total['text']) . "', `value` = '" . $_SESSION['GCFinalOrderTotalIncludingDelivery'] . "', sort_order = '" . (int)$total['sort_order'] . "'");
}
}
Yes absolutely.Else what would have been the point in having a Programing Language.

Categories