PHP While Loop - Variable will passed on the last entry only - php

When I press the Submit Button the Variable "rating" will only passed from the last line. The others are simply empty.
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data">
<table width="800" border="0" align="center" class="zui-table">
<thead>
<tr>
<td colspan="4">
</td>
</tr>
<tr>
<th width="96">Kategorie</th>
<th width="177">Unterkategorie</th>
<th width="299">Beitrag</th>
<th width="210"><? if ($rolle == '1') { echo 'Bewertung'; } elseif ($rolle== '9') { echo 'Freischalten';} else { echo 'Status';} ?></th>
<? while ($show = mysql_fetch_array($abfrage)) { ?>
</tr>
</thead>
<tbody>
<tr>
<td><? echo $show['category']; ?></td>
<td><? echo $show['subcategory']; ?></td>
<td><img src="timthumb.php?src=<?php echo $show['url'] ?>&w=300&h=150" alt="resized image" BORDER="2"/> </td>
<td><? if ($rolle == '1') { ?>
<select name="rating" class="style28" style="width: 120px" id="rating">
<option value="0">0</option>
<option value="5">5</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
<option value="60">60</option>
<option value="65">65</option>
<option value="70">70</option>
<option value="75">75</option>
<option value="80">80</option>
<option value="85">85</option>
<option value="90">90</option>
<option value="95">95</option>
<option value="100">100</option>
</select> <? echo '<button type="submit" value="' . $show['id']. '" name="submit">Bewerten?</button>'; }
elseif ($rolle == 9) {
echo '<button type="freigeben" value="' . $show['id']. '" name="freigeben">Freigeben?</button>';
}?></td>
</tr>
</tbody>
<? }
}?>
</table>
Only the "rating" in the last line will be passed to
if (isset($_POST['submit'])) {
echo $_POST['rating'];
echo $_POST['submit'];
//mysql_query("SET CHARACTER SET 'utf8'");
$db = mysql_connect("localhost", "xx", "xx");
mysql_select_db("cx",$db);
//Rating eingeben
$sql = "UPDATE wp_awa_upload SET count = count + '$_POST[rating]' WHERE id = '$_POST[submit]'";
$result = mysql_query($sql,$db);
//Session eintragen
$sql_insert = "INSERT into wp_awa_session (upload_id, user_id) VALUES('$_POST[submit]', '$_SESSION[id]')";
$result = mysql_query($sql_insert,$db);
echo $sql_insert;
echo $sql;
}
All the lines will be displayed, but when I press on the Submit button, then the variable from the rating dropdown will only be passed, when the button of the last line has been pressed.
Thanks for any help!
Kind Regards,
Stefan

If I understand what you try to achieve, you want the rating variable to contain the rating that corresponds to the button that was pressed. But if you think about it, there's no way for the browser to figure out which drop down the button corresponds to. It makes sense for you (because they appear to be next to each other), but HTML has no concept of "being next to each other", from perspective of HTML you have multiple drop downs, for some reason all with the same name, and multiple buttons. When you press one of the buttons, it needs to decide which value to send as the rating. It chooses to use the last one, because it has no way to guess better.
The simplest solution for you would be to actually make each drop down and a button to be its own form. Currently you have one huge form (see the <form> tag at the beginning of your script). Instead, open the form at the beginning of your while loop body and close at the end, along the lines of
<? while ($show = mysql_fetch_array($abfrage)) { ?>
</tr> <form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data">
...
This way the browser can figure out which drop down corresponds to which button, because for each button there's only one drop down in the corresponding form.

Related

PHP - edit multiple entries [duplicate]

This question already has an answer here:
Post form and update multiple rows with mysql
(1 answer)
Closed 3 days ago.
Given this form, shown as a table:
<form action="multi.php" name="multi[]" method="POST" class="forms">
<table class="table-hovered">
<tr>
<th class="text-left highlight">attività svolta</th>
<th class="text-left highlight">categoria</th>
</tr>
<?php
foreach ($_POST['item'] as $k => $v)
{
$q_item = "SELECT * FROM eventi WHERE id = '".$v."'";
$qu_item = mysql_query($q_item);
while($res = mysql_fetch_array($qu_item))
{
?>
<tr>
<td><?php echo $res['descrizione'];?></td>
<td>
<select name="categoria">
<option value="<?php echo $res['categoria'];?>" selected><?php echo $res['categoria'];?>
<option value="80"> 80
<option value="40"> 40
<option value="70"> 70
</select>
</td>
<input type="hidden" name="idd" value="<?php echo $res['id'];?>">
</tr>
<?php
}
}
?>
</table>
<input type="submit" name="submit" value="modify" />
</form>
I am trying to edit multiple entries, using the code below:
<?php
$utente = $_SESSION["username"];
$id = $_POST["idd"];
$categoria = $_POST["categoria"];
if (!$id or !$categoria){
echo "Error";
}
else
if ($categoria!=='80' && $categoria!=='40' && $categoria!=='70'){
echo "Error";
}
else{
$sql="UPDATE eventi SET categoria='$categoria' WHERE id='$id'";
$update=mysql_query($sql);
echo "Entry modified correctly";
}
?>
As you see, this code changes just one item. I have tried making it recursive. Maybe using a "foreach" is the way to go.
Any hints are appreciated. And sorry for using an old version of PHP (I haven't switched to version 7 yet).
As you have same names for both input and select the last value of each of them overwrites previous values. For passing multiple values in inputs with same names - use [] notation:
<select name="categoria[]">
<option value="<?php echo $res['categoria'];?>" selected><?php echo $res['categoria'];?>
<option value="80"> 80
<option value="40"> 40
<option value="70"> 70
</select>
<input type="hidden" name="idd[]" value="<?php echo $res['id'];?>">
After that - check your $_POST values with print_r - you will see that
$_POST[categoria] and $_POST[idd] are arrays and you can iterate over them with for or foreach.
Btw, inserting an <input> right after </td> produces invalid html.
There's no need to create any hidden input element in the first place, you just have to change the name attribute of <select> element in the following way,
name="categoria[<?php echo $res['id'] ?>]"
So your code should be like this,
<form action="multi.php" name="multi[]" method="POST" class="forms">
<table class="table-hovered">
<tr>
<th class="text-left highlight">attività svolta</th>
<th class="text-left highlight">categoria</th>
</tr>
<?php
foreach ($_POST['item'] as $k => $v){
$q_item = "SELECT * FROM eventi WHERE id = '".$v."'";
$qu_item = mysql_query($q_item);
while($res = mysql_fetch_array($qu_item)){
?>
<tr>
<td><?php echo $res['descrizione'];?></td>
<td>
<select name="categoria[<?php echo $res['id'] ?>]">
<option value="<?php echo $res['categoria'];?>" selected><?php echo $res['categoria'];?>
<option value="80"> 80
<option value="40"> 40
<option value="70"> 70
</select>
</td>
</tr>
<?php
}
}
?>
</table>
<input type="submit" name="submit" value="modify" />
</form>
And this is how you can process your form to perform UPDATE operation,
foreach($_POST['categoria'] as $id => $categoria){
$sql="UPDATE eventi SET categoria='". $categoria . "' WHERE id='" . $id . "'";
// execute your query
}
Note: If you want to see the complete array structure, do var_dump($_POST);

select option selection in php

I have a form for input order, there are 2 fields, branch and customer model. Before we click button "search", we must select branch and customer fields. After I filled the field, then click button search, the data appears. The problem is, after the data appears, where the condition branch and customer models are filled, I want to create another search, then I changed branch field only (customer model field was same with the previous search), change branch field from 'A' to 'B' and click button search, the data not appears / empty (whereas the data exists in database). Branch field,the field when I changed into B, after I click button search, the branch filled changed into
'SELECT' (branch option are SELECT -> A , B).
I'm confused with this problem.. here is the code :
<?php
if ($_POST)
{
$_SESSION['data_id'] = $_POST['data_id'];
$_SESSION['branch'] = $_POST['branch'];
$_SESSION['customer_model'] = $_POST['customer_model'];
}
?>
<form id="search" class="formular" method="post" action="search_data.php" style="z-index:1;">
<table width="100%">
<tr>
<td>Data ID :</td>
<td><input type="text" name="data_id" value="<?php echo $_SESSION['data_id'];?>"></td>
</tr>
<tr>
<?php
$branch = "'".str_replace(",", "','", $_SESSION['moi_status_UserBranch'])."'";
$check_data = $db->GetAll("select cab_id, cabang_name from company_profile where cab_id in(".$branch.")");
?>
<td>Branch :</td>
<td>
<select id="branch" name="branch">
<option value="0">Select</option>
<?php foreach($check_data as $row): ?>
<option <?php echo ($_SESSION['branch'] == $row['CAB_ID']) ? "selected" : ""; ?> value="<?php echo $row['CAB_ID']; ?>"><?php echo $row['CABANG_NAME']; ?></option>
<?php endForeach; ?>
</select>
</td>
</tr>
<tr>
<td width="110">Customer Model</td>
<td>
<select id="customer_model" name="customer_model" class="readonly validate[required]">
<option <?php echo ($_SESSION['customer_model'] == '0') ? "selected" : ""; ?> value="0">Pilih</option>
<option <?php echo ($_SESSION['customer_model'] == 'I') ? "selected" : ""; ?> value="I">Personal</option>
<option <?php echo ($_SESSION['customer_model'] == 'C') ? "selected" : ""; ?> value="C">Corporate</option>
</select>
</td>
</tr>
</table>
<input type="submit" id="btnsubmits" name="btnsubmit" class="button" value="Search" />
<a onclick="mypopup();" style="text-decoration:underline; cursor:pointer; font-weight:bolder">Add New</a>
</form>
<script>
function mypopup()
{
<?php unset($_SESSION['regional_saved']); ?>
mywindow = window.open("choose_custtype.php" ,"Item","width=700px,height=350px,resizable=1,scrollbars=1,top=150,left=200");
mywindow.moveTo(200, 200);
}

PHP Drop Down Selected

i've got 3 drop down boxes which populate each drop down from the one above but on the 3rd drop down i want it to bring back a value that i selected. The code is below
<tr>
<td width="119">Category</td>
<td width="371">
<select name="cat_id" id="cat_id" onChange="showCompany(this.value);">
<option value="">--Select--</option>
<?php
$sql="select * from category";
$sql_row=mysql_query($sql);
while($sql_res=mysql_fetch_assoc($sql_row))
{
?>
<option value="<?php echo $sql_res["id"]; ?>" <?php if($sql_res["id"] ==$_REQUEST ["cat_id"]) { echo "Selected"; } ?>><?php echo $sql_res["category_name"]; ? ></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Company</td>
<td id="td_company">
<select name="company_id" id="company_id">;
<option value="">--Select--</option>
<?php
$sql="select * from company where cat_id='$_REQUEST[cat_id]'";
$sql_row=mysql_query($sql);
while($sql_res=mysql_fetch_assoc($sql_row))
{
?>
<option value="<?php echo $sql_res["id"]; ?>"><?php echo $sql_res["company_name"]; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Item</td>
<td id="td_item">
<select name="item_id" id="item_id">
<option value="">--Select--</option>
<?php
$sql="select * from item where comp_id='$_REQUEST[cat_id]'";
$sql_row=mysql_query($sql);
while($sql_res=mysql_fetch_assoc($sql_row))
{
?>
<option value="<?php echo $sql_res["id"]; ?>"><?php echo $sql_res["item_name"]; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Price</td>
<td id="td_item">
<?php
$sql="select unit_cost from item where comp_id='$_REQUEST[cat_id]'";
$sql_row=mysql_query($sql);
while($sql_res=mysql_fetch_assoc($sql_row))
{
echo "<td>" . "&pound" . $sql_res['unit_cost'] . "</td>";
}
?>
</td>
</tr>
<tr>
</tr>
</table>
</form>
so basically i want it to bring back the price from the option i selected, at the moment its bringing back all prices.
e.g. first drop down i select cellings then the company drop down i select Access Panels then 3rd drop down they is 3 items (Arm Strong, Track and Screw) and it brings back all 3 prices where i want to be able to select one and only bring back that price. any ideas?
First off you shouldn't be using mysql_* functions, they're deprecated. Look into either mysqli or PDO. Second, you have a vulnerability in your code where you are inserting $_REQUEST straight into your SQL. That's called SQL Injection. You should probably do some reading on it.
With all that in mind the problem is your query is trying to select all of the items where company id = category id. This is your query:
$sql="select * from item where comp_id='$_REQUEST[cat_id]'";
Notice the comp_id='$_REQUEST[cat_id]' should it instead be something like comp_id = $_REQUEST[comp_id]'?

filter data to show using dropdown option in php

I try to show some data by dropdown option from mySQL
when the user choose option United states and click submit, the page will go to the next page and show the data only for United states
here is my code for test.html
<body>
<table border="0">
<tr>
<th>test
</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select>
<option value="US">United States</option>
<option value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<button type="submit">submit</button>
</td>
</table>
</body>
here is my second page showDB.php
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");
//connect to database
mysql_select_db("asdasd");
//query the database
//$query = mysql_query("SELECT * FROM auip_wipo_sample");
if($_POST['value'] == 'US') {
// query to get all US records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='US'");
}
elseif($_POST['value'] == 'AUD') {
// query to get all AUD records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='AUD'");
} else {
// query to get all records
$query = mysql_query("SELECT * FROM auip_wipo_sample");
}
//fetch the result
Print "<table border cellpadding=3>";
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$row['invention_title'] . "</td> ";
Print "<th>Pet:</th> <td>".$row['invention-title'] . " </td></tr>";
}
Print "</table>";
?>
I try the above code, but I got 2 error which are
Undefined index: value in C:\xampp\htdocs\folder\showDB.php on line 10
Undefined index: value in C:\xampp\htdocs\folder\showDB.php on line 14
line 10 is
if($_POST['value'] == 'US') {
and line 14 is
elseif($_POST['value'] == 'AUD') {
anyone can give solution
thanks
You need to add a name parameter to test.html - either set it to name="value" to make showDB.php to work without any alterations, or set lines 10 and 14 to match the name parameter you set. Example below:
edit:
#adeneo is right, you also need to add a form and a valid submit button
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="value">
<option name="country" value="US">United States</option>
<option name="country" value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<input type="submit" name="btn_submit" />
</td>
</table>
</form>
</body>
and the php:
if($_POST['country'] == 'US') {
// query to get all US records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='US'");
}
elseif($_POST['country'] == 'AUD') {
You have no element with the name value in your form, so you don't have a $_POST['value'] either! Actually, you don't even have a form to submit at all, you just placed a link to another page inside the submit button ?
<body>
<form action="showDB.php">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="value">
<option value="US">United States</option>
<option value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<button type="submit">submit</button>
</td>
</table>
</form>
</body>

how do i apply Criteria::LIMIT for upper and lower or is there another way?

i have this code in indexSucces.php
//some html for the main page
<td width='48%' align='right'>
<?php include_partial('login', array('form' => $form)) ?>
</td>
in the actions.class.php for index i have:
public function executeIndex()
{
$this->form = new RcLoginForm();
$this->setTemplate('index');
$this->search_form = new RcSearchForm();
$this->age_form = new RcAgeTableForm();
}
public function executeListmatches(sfWebRequest $request)
{
}
then the partial _login.php code:
<form action="<?php echo url_for('password/listmatches' ) ?>" method="post" >
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>I am a:</span>
<select id="gender1" name="gender1">
<option value="male1" >Male</option>
<option value="female1" selected="selected">Female</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>Seeking a:</span>
<select id="gender2" name="gender2">
<option value="male2" >Male</option>
<option value="female2" selected="selected">Female</option>
</select>
</td>
</tr>
<tr>
<td>
<span class='spn_med_lightblue_rbc'>Age:</span>
<select id="age1" name="age1">
<?php $ages = RcAgeTablePeer::getById(18);
foreach ($ages as $age)
{
//echo $age->getId();
echo "<option>";
echo $age->getId();
echo "</option>";
} ?>
</select>
</td>
<td>
<span class='spn_med_lightblue_rbc'>To:</span>
<select id="age2" name="age2">
<?php foreach ($ages as $age)
{
//echo $age->getId();
echo "<option>";
echo $age->getId();
echo "</option>";
} ?>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<span class='spn_med_lightblue_rbc'>Location:</span>
<select id="province" name="province">
<option value="all" selected="selected">All</option>
<option value="ec">Eastern Cape</option>
<option value="nc">Northern Cape</option>
<option value="wc">Western Cape</option>
<option value="fs">Free State</option>
<option value="gp">Gauteng</option>
<option value="kzn">Kzn</option>
<option value="lim">Limpopo</option>
<option value="mpu">Mpumulanga</option>
<option value="nw">North West</option>
</select>
</td>
<td><input class='submit_img' type="image" src="/images/rainbow/gobuttonbluesmall.png" value="Submit" alt="Submit"></td>
</tr>
<tr></tr>
<tr></tr>
</form>
the above will give a home page with a section(the partial _login) that contains the login to the website and also a quick search when you hit the go button the form action will go to
action="
this is my dilemma..i am not in any class, i just pass on the selected values through POST and use those values to get my rows
in listmatchesSuccess.php
$matching_rows = RcProfileTablePeer::getAllBySelection($gender_id2,$age1,$age2,$province_id,$from,$limit);
where above in RcProfileTablePeer.php:
static public function getAllBySelection($gender2,$age1,$age2,$province_id,$from,$limit)
{
$criteria = new Criteria();
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->add(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
$criteria->add(RcProfileTablePeer::GENDER_ID,$gender2, Criteria::EQUAL);
if ($province_id <> 10)
$criteria->addAnd(RcProfileTablePeer::PROVINCE_ID,$province_id, Criteria::EQUAL);
$criteria->setLimit($limit);
return self::doSelect($criteria);
}
hope all this makes better sense now :)
thank you
I hope I understand what you are asking,
I you want to add a limit to your query
$criteria->setLimit(10);
and also if you want to create a pager in symfony, I would suggest the propel pager class
e.g.
$this->page = $request->getParameter('page', 1);
$c = new Criteria();
$c->add(UserPeer::Status_ID,1);
$this->pager = new sfPropelPager('User', $limit);
$this->pager->setCriteria($c));
$this->pager->setPeerMethod('doSelect');
$this->pager->setPage($this->page);
$this->pager->init();
You can reference this link http://www.symfony-project.org/cookbook/1_0/en/pager for more information .
I hope that answers your question;
There is an error in your query:
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->add(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
Resulting query: rc_profile.age >= :age1.
If you are looking for the objects with age between $age1 and $age2, this should do the trick:
$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->addAnd(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
Resulting query: rc_profile.age >= :age1 AND rc_profile.age <= :age2
Note that you should use $criteria->addAnd or $criteria->addOr to combine the conditions FOR THE SAME COLUMN.

Categories