1 checkbox with 2 value from sql pass the selection - php

Hi all
Now is 2 week that i searching for an answer to my problem and no luck. hope somone can help me on this
i have a chackbox option that i call from the database
<form style="margin-top:-30px" method="POST" action="extradata.php">
<?php
$sql = "SELECT ext_id,ext_price,ext_name,ext_description FROM tbl_extra ORDER by ext_id ASC";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$number = mysql_num_rows($result);
$i = 0;
while ($number > $i) {
$NA= mysql_result($result,$i,"ext_name");
$PR= mysql_result($result,$i,"ext_price");
print "<input type='checkbox' name='extra[]' value='$NA'></td>";
$i++;
}
?>
What i trying to do is to pass the value to extradata.php with 2 value
1.$NA,
2.$PR
when selected box then insert to the database the value of $NA to ex_tra and $PR to ex_price
the extradata.php
<?php
require_once 'library/config.php';
$id=$_POST['pd_id'];
$ssid=$_POST['ct_session_id'];
$total=$_POST['tot'];
$name=$_POST['basedes'];
$qty=$_POST['ct_qty'];
$extra_array = $_POST['extra'];
if ( $extra_array > "0" ) {
foreach ($extra_array as $one_extra) {
$source .= $one_extra.", "; }
$extra = substr($source, 0, -2);
} else {}
$result=mysql_query("INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ex_tra, ex_tra2, ex_price, ct_date) VALUES ('$id','$qty','$ssid','$extra','$name','$total', NOW())");
?>
Best regard to all

Sending two values in one variable is bound to get messy. I would just send the id and get the corresponding values from the database again in extradata.php.
If you really want to send multiple values, I would use fixed indices for the checkboxes (not extra[] but extra[SOME_NUMBER] and add a hidden field next to it (extra_pr[SOME_NUMBER]?) to pass the second value.
Note that you have to use fixed indices as unchecked checkboxes donĀ“t get posted but all hidden fields will get posted.

Related

How do I list each value from my_sqli_fetch_row?

I have two sql statements.
1 lists all columns from my table using mysqli_fetch_assoc
The other statements returns a row where id = specific id using mysqli_num_rows
My problem is that with the mysqli_fetch_assoc, I'm able to use something like:
$rowz['Field'] and it returns the value for every single column name. This is perfect, but, I am unable to get every single value in my row as well. In other words, My code lists all fields: id / name / phone... but when I use myqli_fetch_row, it returns the value for just one column over and over. So every field will just show '193' which is the id. I want all fields to show proper value instead of just the same value from one column over and over.
// listing all columns from table
$sql2 = ("SHOW COLUMNS FROM articles");
$result55 = mysqli_query($link, $sql2);
echo'<form action="update.php" method="POST">';
if (mysqli_num_rows($result55) > 0)
{
// to fetch row of specific id
$sql = "SELECT * FROM articles WHERE ID='".$id."'";
$result5 = mysqli_query($link, $sql);
if (!$result5) {
echo 'no';
}
$row = mysqli_fetch_row($result5);
while($rowz = mysqli_fetch_assoc($result55)){
$z = 0;
echo '
<input class="form-control" type="text" name="'.$rowz['Field'].'"
value="'.$row[$z].'" />';
}
}
echo' </form>';
The issue is the name of the input field, where I have '.$row[$z].' it just keeps returning 0 from array which is just the ID column. Hence why I just keep getting 193 over and over. However, the $rowz['Field'] works much better and loops every single time for each existing column. I hope to get: id = 193, name = john, phone = 123-3345 etc.. But instead I get: id = 193, name = 193, phone = 193.
Please send help!
$z is your column index. and it should be your counter, if you want to generate each column values.
$z = 0;
while($rowz = mysqli_fetch_assoc($result55)){
echo '
<input class="form-control" type="text" name="'.$rowz['Field'].'"
value="'.$row[$z].'" />';
$z = $z + 1;
}
You have declared $z=0 inside the while loop.
Rather you have to declare it before the while loop and increment it inside the loop.
You have used $z as the index for $row which indicates the specific column of the result obtained from the query.
When you fix it to 0, it means that the first column value only is getting accessed.
$row = mysqli_fetch_row($result5);
$z=0;
while($rowz = mysqli_fetch_assoc($result55))
{
echo '
<input class="form-control" type="text" name="'.$rowz['Field'].'"
value="'.$row[$z].'" />';
$z = $z +1;
}

How to check all values present in the column 'pincode'?

This code only reads the first value present in the column. If the value posted in the html form matches the first value, it inserts into the database. But I want to check all the values in the column and then take the respective actions.
For example, if i give input for 'ppincode' and 'dpincode' as 400001, it accepts. but if i gave 400002, 400003,..... it displays the alert even if those value are present in the database
DATABASE:
pincode <== column_name
400001 <== value
400002
400003
400004
...
also i tried this
$query = "SELECT * FROM pincodes";
$result = mysqli_query($db, $query);
$pincodearray = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)){
$pincodearray[] = $row;
}
}
If I understand well - you want to compare value from POST request with all retrieved records saved in DB and if it matches - perform action.
If so, I would recommend using for(each) loop. Example:
if( !empty($row){
foreach( $row as $key ){
if($key['pincode'] == $ppincode && $key['pincode'] == $dpincode){
// your action goes here
}
}
}
Additional tip: use prepared statements :)
SELECT count(*) FROM table WHERE ppincode=ppincode AND bpincode=bpincode
if this return 0 then insert or else show alert.

How to make a double loop to populate an echo statement

I have values written in a database. These value come from a checkbox form.
I try to get these values from the database and write them in a new page.
I use the following code that works :
$con = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$pw") or die ("Error in Selecting ");
$sql = pg_query($con,"SELECT language FROM texte where bhl='17'");
if(pg_num_rows($sql) > 0){
$result = pg_fetch_assoc($sql);
$checked_arr = explode(";",$result['language']);
}
// Gives "item1"
// Create checkboxes
foreach ($checked_arr as &$value) {
$array1 = array("item1", "item2", "item3");
// insertion of the id's corresponding to item1, item2, item3
$id = array("id-item1", "id-item2", "id-item3");
$checked = "";
if(in_array($value, $array1)){
$checked = "checked";
}
echo '<input type="checkbox" name="lang[]" value="'.$value.'" '.$checked.' > '.$value.' <br/>';
}
But actually something is missing : the id value that characterizes the checkbox. So I inserted in the code an array composed of the id values.
The goal is to associate id-item1 to item1, id-item2 to item 2 and so on.
To make it possible I guess I have to create a double loop to write in the echo statement both values and id's.
But after many attempts I didn't manage to make it possible and I really don't know how I could do.
Any help would be appreciated.
Thanks !

Passing multiple checkbox values to different columns of database

I am pretty new to PHP, but have tried searching for other questions similar to mine and been unable to find anything that is close enough to my situation to help me solve this.
I am trying to code a web page that allows users to select as many or as few items as they would like to order. The item values are identical to their Primary Key in the Item table.
Once submitted, each different item value should be input into the same row of a database table based on the date{pk}. Within that row, there are numerous columns: Item1ID, Item2ID, Item3ID, etc.
So far, the value of each item selected is assigned to a new array. However, I cannot simply input the array values into a column -- I need each array index to be placed into a sequential column. The code is below:
$date = new DateTime();
$td = $date->format('Y-m-d');
$x = 1;
$checkedItems = $_POST['Item'];
$count = count($checkedItems);
echo $count;
$foodID = "Item".$x."ID";
While($x<=$count){
if(isset($_POST['Item'])){
if (is_array($_POST['Item'])) {
foreach($_POST['Item'] as $values){
$selectedFoods = substr($values,0,4);
$addFoodOrderQuery= sprintf("UPDATE WeeklyBasketFoodOrder SET '%s' = %s WHERE `foodOrderDate` = '%s'",
$foodID, $selectedFoods, $td);
$result= mysqli_query($db, $addFoodOrderQuery);
}
}
} else {
$values = $_POST['Item'];
echo "You have not selected any items to order.";
}
$x++;
}
If you need any further clarification, please let me know. After submitting the code, the database item#ID tables are different, but they are now empty instead of "NULL."

How to access an Arrays value by the value of another array

$query = "select Code , count(ListID) as nums from accesstable where Cust=" . $_SESSION ['Cust'] . " and App=" . $_SESSION ['App'] . " group by Code";
$result = mysql_query ( $query );
while ($row = mysql_fetch_array ( $result )){
$Codes[] = $row['Code'];
$Values[] = $row['nums'];
}
This is the structure of my code that I am trying to learn how to properly access... Here is my dilemma... I am trying to figure out how to explicitly find the associated count of nums dependent on the value of a Code.
Let me explain in better detail where my issue is....
Lets say the list of codes is
Code nums
1 624
7 825
571 450
9 393
2 739
9 590
The above code does successfully allow me to separate those values strictly into keys and values but I cannot figure out how to grab the nums value if the code is = to a certain value... I have currently been trying to declare a variable above the entire snippet of code and then declare it within the while statement but cannot figure out how to get the value to bind properly.... I will repaste the above code with one of my many failures in the while statement to give a better idea.
$Answer1 = 0;
$query = "select Code , count(ListID) as nums from accesstable where Cust=" . $_SESSION ['Cust'] . " and App=" . $_SESSION ['App'] . " group by Code";
$result = mysql_query ( $query );
while ($row = mysql_fetch_array ( $result )){
$Codes[] = $row['Code'];
$Values[] = $row['nums'];
($Codes == 1){
$Answer1 = // Right Here I want to Get the value 624 related to Code 1... Dont want to embarass myself with examples of what I have tried...
}
So how do I make a condition to output the value associated with a Code? I want to explicitly define these values as the list of codes can change with each customer... Luckily there are only a certain amount of codes so its not like I need to define too many of them... I just want to make sure I can get the nums value associated with a code and display it.
Hope I did a good job explaining this. :)
I'd do:
while ($row = mysql_fetch_array ( $result )){
$Codes[] = $row['Code'];
$Values[$row['Code']] = $row['nums'];
}
and, to access the value associated to a code:
$code = 1;
$value = $values[$code];
Since they would share the same array key, something like this would work-
if ($Codes[$key] == 1){
$Answer1 = $Values[$key];
}

Categories