i have a form.php with an array posting to update.php to update a mysql db.
The relevant parts of each are:
form.php
if ($type == "1") {echo '
<input type="hidden" id="assettype" name="atype[]" value="'. $row['AType'] .'"/>
<input type="hidden" id="assetid" name="aid[]" value="'. $row['AID'] .'"/>
<input type="text" name="serialnum[]" value="'. $row['SerialNumber'] .'"/>
<input type="text" name="unitsize[]" value="'. $row['UnitSize'] .'"/>
<input type="text" name="prop1[]" value="'. $row['Prop1'] .'"/>
<input type="text" name="latitude[]" value="'. $row['Latitude'] .'"/>
<input type="text" name="longitude[]" value="'. $row['Longitude'] .'"/>
';}
if ($type == "2") {echo '
<input type="hidden" id="assettype" name="atype[]" value="'. $row['AType'] .'"/>
<input type="hidden" id="assetid" name="aid[]" value="'. $row['AID'] .'"/>
<input type="text" name="serialnum[]" value="'. $row['SerialNumber'] .'"/>
<input type="text" name="unitsize[]" value="'. $row['UnitSize'] .'"/>
<input type="text" name="prop2[]" value="'. $row['Prop2'] .'"/>
<input type="text" name="latitude[]" value="'. $row['Latitude'] .'"/>
<input type="text" name="longitude[]" value="'. $row['Longitude'] .'"/>
';}
Update.php
foreach ($_POST['aid'] as $key=>$assetid) {
$atype = $_POST['atype'][$key];
if ($atype == "1") {
$stmt = $mysqli->stmt_init();
$query = "UPDATE asset SET
SerialNumber=?,UnitSize=?,Prop1=?,Latitude=?,Longitude=? WHERE AID = ?";
if($stmt->prepare($query)) {
$stmt->bind_param('sdddds', $serialnum, $unitsize, $prop1, $lat, $long, $assetid);
$assetid = $_POST['aid'][$key];
$serialnum = $_POST['serialnum'][$key];
$unitsize = $_POST['unitsize'][$key];
$prop1 = $_POST['prop1'][$key];
$lat = $_POST['latitude'][$key];
$long = $_POST['longitude'][$key];
$stmt->execute();
$stmt->close();
}
}
elseif ($atype == "2") {
$stmt = $mysqli->stmt_init();
$query = "UPDATE asset SET
SerialNumber=?,Prop2=?, Latitude=?,Longitude=? WHERE AID = ?";
if($stmt->prepare($query)) {
$stmt->bind_param('ssdds', $serialnum, $prop2, $lat, $long, $assetid);
$assetid = $_POST['aid'][$key];
$serialnum = $_POST['serialnum'][$key];
$prop2 = $_POST['prop2'][$key];
$lat = $_POST['latitude'][$key];
$long = $_POST['longitude'][$key];
$stmt->execute();
$stmt->close();
}
}}
This works great for all but one of the inputs in the array.
I keep getting the following:
Notice: Undefined offset: 3
relating to the following line:
$prop2 = $_POST['prop2'][$key];
All the other DB fields are updating fine except for this one. Can anyone point me in the right direction please?
You should use same name for this array input field. ie prop[] , but you used prop1[] and prop2[].
if ($type == "1") {echo '
<input type="hidden" id="assettype" name="atype[]" value="'. $row['AType'] .'"/>
<input type="hidden" id="assetid" name="aid[]" value="'. $row['AID'] .'"/>
<input type="text" name="serialnum[]" value="'. $row['SerialNumber'] .'"/>
<input type="text" name="unitsize[]" value="'. $row['UnitSize'] .'"/>
<input type="text" name="prop[]" value="'. $row['Prop1'] .'"/>
<input type="text" name="latitude[]" value="'. $row['Latitude'] .'"/>
<input type="text" name="longitude[]" value="'. $row['Longitude'] .'"/>
';}
if ($type == "2") {echo '
<input type="hidden" id="assettype" name="atype[]" value="'. $row['AType'] .'"/>
<input type="hidden" id="assetid" name="aid[]" value="'. $row['AID'] .'"/>
<input type="text" name="serialnum[]" value="'. $row['SerialNumber'] .'"/>
<input type="text" name="unitsize[]" value="'. $row['UnitSize'] .'"/>
<input type="text" name="prop[]" value="'. $row['Prop2'] .'"/>
<input type="text" name="latitude[]" value="'. $row['Latitude'] .'"/>
<input type="text" name="longitude[]" value="'. $row['Longitude'] .'"/>
';}
And in action page update like this:
$prop2 = $_POST['prop'][$key];
I don't know what logic you used, If there is a logical error use,
$prop2 = !empty($_POST['prop2'][$key]) ? $_POST['prop2'][$key] : '';
Related
<?php
require_once "config.php";
if(isset($_POST['save'])){
$values = array();
for($i=0 ; $i <count($_POST['working']); $i++) {
$values[] = '("' . $_POST['working']['day'][$i] . '","' .
$_POST['working']['status'][$i] . '")'; }
$sql4 = "INSERT INTO working_day(day,status) VALUES " . implode(',',
$values);
$result4=mysqli_query($connection,$sql4);}
?>
<form id="sendform" method="post" action="">
<input type="text" value="Monday" id="day" name="working[day][]" readonly />
<input type="text" value="" id="status" name="working[status][]" />
<input type="text" value="Tuesday" id="day" name="working[day][]" readonly />
<input type="text" value="" id="status" name="working[status][]" />
<input type="text" value="w" id="day" name="working[day][]" readonly />
<input type="text" value="" id="status" name="working[status][]" />
<input type="text" value="T" id="day" name="working[day][]" readonly />
<input type="text" value="" id="status" name="working[status][]" />
<input type="text" value="F" id="day" name="working[day][]" readonly />
<input type="text" value="" id="status" name="working[status][]" />
<input type="text" value="S" id="day" name="working[day][]" readonly />
<input type="text" value="" id="status" name="working[status][]" />
<input type="text" value="S" id="day" name="working[day][]" readonly />
<input type="text" value="" id="status" name="working[status][]" />
<input type="submit" value="Send" name="save" />
</form>
**why this code only can insert first day&status and second day&status into the database ? The for loop count[i] cannot count another value. how to solve this problem? **
Correct way is given below :-
<?php
require_once "config.php";
if(isset($_POST['save'])){
$values = array();
for($i=0 ; $i <count($_POST['working']['day']); $i++) {
$values[] = '("' . $_POST['working']['day'][$i] . '","' . $_POST['working']['status'][$i] . '")';
}
$sql4 = "INSERT INTO working_day(day,status) VALUES " . implode(',', $values);
$result4=mysqli_query($connection,$sql4);
}
I would like to ask why every time i try to call programme it doesn't show the results? I'm trying to create edit user page which means i will call back the database but mine doesn't show up.
Here's my Edit User code i have problem with row4
// Retrieve the user's information:
$q = "SELECT first_name, last_name, phone, email , programme FROM users WHERE user_id=$id";
$r = #mysql_query ($q);
if (mysql_num_rows($r) == 1) { // Valid user ID, show the form.
// Get the user's information:
$row = mysql_fetch_array ($r, MYSQL_NUM);
// Create the form:
echo '<div align="center"/div><form action="edit_user.php" method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '" /></p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p>
<p>Phone No: <input type="text" name="phone" size="20" maxlength="60" value="' . $row[2] . '" /> </p>
<p>Email Address: <input type="text" name="email" size="20" maxlength="60" value="' . $row[3] . '" /> </p>
<p>Programme: <name="programme" size="20" maxlength="60" value="' . $row[4] . '" /> </p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="id" value="' . $id . '" />
</form>';
Here's my Registration code because i used drop down so i can't call drop down back in edit user page that include database for programme registered
<?php # Script 3.5 - Registration Form
$page_title = 'Registration Form';
include ('./includes_css/header.html');
?>
<div align="center"><h2>Registration Form</h2>
<form action="register.php" method="post">
<legend>Enter your information in the form below:</legend><br/>
<p><b>First Name:</b> <input type="text" size="21" name="first_name" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>"/></p>
<p><b>Last Name:</b> <input type="text" size="21"name="last_name" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>"/></p>
<p><b>Phone No:</b> <input type="text" size="22"name="phone" value="<?php if (isset($_POST['phone'])) echo $_POST['phone']; ?>"/></p>
<p><b>Email Address:</b> <input type="text" size="18"name="email" /value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>"/></p>
<p><b>Programme:</b> <name="programme" value="<?php if (isset($_POST['programme'])) echo $_POST['programme']; ?>"/>
<?php
$programme = array ('null' => 'SELECT','BIMD', 'BSE', 'BCEM', 'BCA', 'DIM', 'DCNET',
'DIT', 'DIA');
// Make the programme pull-down menu.
echo '<select name="programme">';
foreach ($programme as $key => $value) {
echo "<option value=\"$value\">$value</option>\n";
}
echo '</select>';
?>
<div align="center"><input type="submit" name="submit" value="Register" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
I have problem with Edit User page (Program doesn't show up).
How to calculate sum of PHP multiple checkboxes values array - Total Price of checked checkboxes?
For example, result should be displayed like:
Total Price of Selected Programming Languages : C++,Java = 1200$
<form method="post" action="#">
0<input type="checkbox" name="count[]" id="count[]" value="0"/>
<input type="hidden" name="language[]" id="language" value="C"/>C [$800]
<input type="hidden" name="price[]" id="price" value="800"> <br/>
1<input type="checkbox" name="count[]" id="count[]" value="1"/>
<input type="hidden" name="language[]" id="language" value="C++"/>C++ [$700]
<input type="hidden" name="price[]" id="price" value="700"> <br/>
2<input type="checkbox" name="count[]" id="count[]" value="2"/>
<input type="hidden" name="language[]" id="language" value="Assembler"/>Assembler [$600]
<input type="hidden" name="price[]" id="price" value="600"><br/>
3<input type="checkbox" name="count[]" id="count[]" value="3"/>
<input type="hidden" name="language[]" id="language" value="Java"/>Java [$500]
<input type="hidden" name="price[]" id="price" value="500"> <br/>
4<input type="checkbox" name="count[]" id="count[]" value="4"/>
<input type="hidden" name="language[]" id="language" value="PHP"/>PHP [$400]
<input type="hidden" name="price[]" id="price" value="400"> <br/>
<input type="submit" name="sbt" id="sbt" value="SUBMIT">
</form>
This is the PHP code:
<?php
if(isset($_POST['sbt'])){
$count = $_POST['count'];
$sub_menu = $_POST['sub_menu'];
$sub_price = $_POST['sub_price'];
$sub_price1 = $_POST['sub_price'];
//$total_price = array($sub_menu => $sub_price);
foreach($count as $j)
echo $sub_menu[$j] . '['.$sub_price[$j]. ']' ;
}
?>
$items = array();
$total = 0;
foreach($_POST['price'] as $k => $price) {
if(in_array($k, $_POST['count'])) {
$items[] = $_POST['language'][$k];
$total += intval($price);
}
}
$items = implode(", ", $items);
echo $items . " = $" . $total;
You need to make sure the array indexes are the same for each group:
0<input type="checkbox" name="count[0]" value="0"/>
<input type="hidden" name="language[0]" value="C"/>C [$800]
<input type="hidden" name="price[0]" value="800"> <br/>
1<input type="checkbox" name="count[1]" value="1"/>
<input type="hidden" name="language[1]" value="C++"/>C++ [$700]
<input type="hidden" name="price[1]" value="700"> <br/>
Then you can get the price and language for each count:
$price = array_intersect_key($_POST['price'], $_POST['count']);
$language = array_intersect_key($_POST['language'], $_POST['count']);
Then implode the text and sum the price:
echo "Total Price of Selected Programming Languages: "
. implode(',', $language) . ' = $'
. array_sum(price);
I have this table:
emailtype:
emailtypeID emailtype
1 primary
2 secondary
3 old
I have this code to show emails in the input form:
$sql = "SELECT * from emailtype";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />'; }
GOAL:
I would like to repeat twice the emailtypeID = 2 or emailtype = secondary, so that I can enter two email addresses with the secondary ID.
Is it possible in the while loop?
Thanks!
SOLUTION:
For anybody who needs it, this is the new code as per the Marc B suggestion.
while($row = $result->fetch_assoc()) {
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />';
if($row["emailtype"] == 'secondario'){
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />';
}
}
Please use this:
while($row = $result->fetch_assoc()) {
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />';
if($row["emailtype"] == 'secondario' || $row["emailtypeID"]==2){
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />';
}
}
I have implemented MIGS payment service in my magento installation and it is using a vpc_php_serverhost_do.php. These are the values Im passing to that file
<input type="hidden" name="virtualPaymentClientURL" size="63" value="https://migs.mastercard.com.au/vpcpay" maxlength="250">
<input type="hidden" name="vpc_Version" value="1" size="20" maxlength="8">
<input type="hidden" name="vpc_Command" value="pay" size="20" maxlength="16">
<input type="hidden" name="vpc_MerchTxnRef" value="<?php echo $orderId; ?>" size="20" maxlength="40">
<input type="hidden" name="vpc_AccessCode" value="<?php echo $access_code; ?>" size="20" maxlength="8">
<input type="hidden" name="vpc_Merchant" value="<?php echo $merchant; ?>" size="20" maxlength="16">
<input type="hidden" name="vpc_OrderInfo" value="<?php echo $orderId; ?>" size="20" maxlength="34">
<input type="hidden" name="vpc_Amount" value="<?php echo $amountInFils; ?>" size="20" maxlength="10">
<input type="hidden" name="vpc_Locale" value="en" size="20" maxlength="5">
<input type="hidden" name="vpc_ReturnURL" size="63" value="<?php echo $url;?>" maxlength="350">
<input type="hidden" name="vpc_user_SessionId" size="63" value="<?php echo $sessionId;?>" maxlength="350">
I have give secure secret provided by the client and the rest of the code looks like the one below
$vpcURL = $_POST["virtualPaymentClientURL"] . "?";
unset($_POST["virtualPaymentClientURL"]);
unset($_POST["SubButL"]);
$md5HashData = $SECURE_SECRET;
ksort ($_POST);
$appendAmp = 0;
foreach($_POST as $key => $value) {
if (strlen($value) > 0) {
if ($appendAmp == 0) {
$vpcURL .= urlencode($key) . '=' . urlencode($value);
$appendAmp = 1;
} else {
$vpcURL .= '&' . urlencode($key) . "=" . urlencode($value);
}
$md5HashData .= $value;
}
}
if (strlen($SECURE_SECRET) > 0) {
$vpcURL .= "&vpc_SecureHash=" . strtoupper(md5($md5HashData));
}
header("Location: ".$vpcURL);
It is redirecting to the payment gateway as it should be. The problem is that the response i get after payment is not encoded. the response link is like this (for security reasons i have changed the numerals with x)
https://xxxxxx/site_test/vpc_php_serverhost_dr.php?vpc_Amount=xx&vpc_BatchNo=x&vpc_Command=pay&vpc_Locale=en&vpc_MerchTxnRef=xxxxx&vpc_Merchant=xxxxx&vpc_Message=Cancelled&vpc_OrderInfo=xxxxx&vpc_SecureHash=xxxxxxxx&vpc_TransactionNo=x&vpc_TxnResponseCode=C&vpc_Version=xx
What should I do to make the response url encoded?
Have you tried using:
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
http://php.net/manual/en/function.urlencode.php