Php loop thourgh array of Request ids - php

function LoadProtocols(){
?>
<?php
$result=mysql_query("SELECT id,name,Destination_port, Destination_port_range_start, Destination_port_range_stop FROM protocols;");
echo '<table class="tftable" border="1">';
echo '<tr><th>protocol Name</th><th>Port</th><th></th></tr>';
echo "<form method='POST' action=''>";
if( $row=mysql_fetch_array($result)){
mysql_data_seek($result,0);
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST"){
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id.";");
header("Location: Overview.php?Menu=".$menu."&Overview=1");
}
echo "</form>";
}
echo '<table>';
}
I made a simple table which as got 3 columns, the first column is a name, second one has a textbox which contains a number (with a hidden id that I need) and last one is a button to update that specific row.
The big problem I have that it only updates the last row, therefore I read that you needed to add [] to ur names and loop through it to update that specific row.
It's still not working and I can't figure out how to just let one specific row update with that [] array.
Thank you for reading and helping.

$_request['id'] is nothing but array ,also header should not be in loop
so Write This
mysql_data_seek($result,0);
$counter=0;
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST"){
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id[$counter].";");
$counter++;
// header("Location: Overview.php?Menu=".$menu."&Overview=1");
}
Instead of this
mysql_data_seek($result,0);
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id.";");
header("Location: Overview.php?Menu=".$menu."&Overview=1");
}

Related

Refuses to get data

I am using this code to get the value of checked box in PHP but it doesn't work in the second code. The first one is just a list for testing. The second one will echo data from a database:
When applying the first code on the test list I will get the value of the box I checked but when applying it on the second part (one that gets data from database it returns Empty)
Get the checked boxes:
<?php
if(isset($_POST['DeleteCon']) )
{
if(!empty($_POST['lang']))
{
foreach($_POST['lang'] as $value)
{
echo "value : ".$value.'<br/>';
}
}
else
{
echo "value : Empty <br/>";
}
}
?>
Test list: it works on this section of the code:
<form method="post">
<?php
echo "<span>Select languages</span><br/>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'> PHP <br/>
<input type='checkbox' name='lang[]' value='JavaScript'> JavaScript <br/>
<input type='checkbox' name='lang[]' value='jQuery'> jQuery <br/>
<input type='checkbox' name='lang[]' value='Angular JS'> Angular JS <br/>"
?>
</form>
But doesn't work on this one:
<form method="post">
<?php
include('database.php');
$sql = "SELECT id, ContactID ,FirstName, LastName, Phone FROM Contact WHERE ID='1'";
$result = $conn->query($sql);
if ( !empty($result->num_rows) && $result->num_rows > 0) { // ...
// output data of each row
echo "<form method='post'>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td id='delete'>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'>
</td>
<td>". $row["FirstName"]. "</td>
<td>". $row["LastName"]. "</td>
<td>". $row["Phone"] ."</td></tr>";
}
echo "</form>";
} else {
echo "<tr>
<td id='delete'>
<input type='checkbox' id='row1' class='table-row'>
</td>
<td> 0 results </tr>";
}
$conn->close();
?>
</form>

Get checked checkboxes in PHP

I can't get the input of checkboxes in PHP.
Here is my code:
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' id='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
#this is the part that probably isn't correct.
if(!empty($_POST['check_list'])){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
I would like to get the amount of checkboxes checked.
The checkboxes are created in a loop with the input of a database.
Even if this would work, how would I get the id of all of the checked checkboxes?
It seems you are not using the form to submit. Place your table inside the form
<form action="" method="post">
<?php
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' value='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</form>
You can get the post values
if($_POST){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
Simple form with checkboxes:-
<form name="" action="" method="post">
<input type="checkbox" name="gender[]" value="Male" />Male
<input type="checkbox" name="gender[]" value="Female" />Female
<input type="submit" name="submit" value="Submit" />
</form>
The PHP code to get the selected:-=
if(isset($_POST['gender'])){
$options = $_POST['gender'];
echo implode(',', $options);
}
If you want to pass the id you can do it like
<input type="checkbox" name="gender[2]" value="Male" />Male
<input type="checkbox" name="gender[3]" value="Female" />Female
You can loop through each option
foreach($options as $key => $value){
echo $key.'---'.$value;
}
//$key is the id sepcified, $values is the seected value

On submit i need to pass anchor tag value in php

while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo

Edit sql data in php page

I need to edit my database table using submitted data.
This is the form:
mysql_query("set names 'utf8'");
$query = "SELECT * FROM sec1octa";
$result = mysql_query($query);
?>
<div align="center">
<form method="get" action="edit_data.php">
<table width="104" border="1" class="center1">
<tr>
<th width="94">first</th>
<th width="94">second</th>
<th width="94">status</th>
</tr>
<tr>
<?php
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><input type="text" name="id" value="<?php echo $row ['stu_no']; ?> " size=10></td>
<td><input type="text" name="name" value="<?php echo $row ['stu_name']; ?> " size=10></td>
<td><?php
echo '<select name="status">'; {
echo '<option value="open">'.$row['stu_status'].'</option>';
echo '<option value="close">'.prevent.'</option>';
}
echo '</select>';
?></td>
</tr>
<?php
}
}
?>
</tr>
</table>
<input type="submit" name="submit" value="done" />
</form>
The problem is in the edit_data.php page.
I can't UPDATE.
I use this code but it's not working.
require_once('../Connections/config.php');
$id= $_GET['id'];
$status= $_GET['status'];
$query= mysql_query("UPDATE `goh`.`sec1octa` SET `stu_status` = '$status'
WHERE stu_no='".$id."'") or die (mysql_error ());
if($query){echo $status ."done ";}
The reason you are only getting the last values in your edit_data.php $_GET is because you are not setting the input/select names as arrays.
<input type="text" name="id" value="some_stu_no">
is happening over and over and over and every new one overwrites the previous.
Instead, you should use:
<input type="text" name="id[]" value="some_stu_no">
This will allow you to pass multiple id's in a single form submission.
Your form:
<form method="POST" action="edit_data.php">
....
echo "<tr>";
echo "<th>id</th>";
echo "<th>name</th>";
echo "<th>status</th>";
echo "</tr>";
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input type=\"text\" name=\"id[]\" value=\"{$row['stu_no']}\" size=\"10\"></td>";
echo "<td>{$row['stu_name']}</td>";
echo "<td>";
echo "<select name=\"status[]\">"; // I don't like your option set up here, but I don't fully understand it either.
echo "<option value=\"open\">{$row['stu_status']}</option>";
echo "<option value=\"close\">.prevent.</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
}
....
<input type="submit" value="Submit All">
</form>
edit_data.php
// create a mysqli connection called $db
if(isset($_POST['id'])){
$tally=0;
// build all queries for the batch
foreach($_POST['id'] as $index=>$id){
$queries[]="UPDATE `goh`.`sec1octa` SET `stu_status`='".mysqli_real_escape_string($db,$_POST['status'][$index])."' WHERE `stu_no`='".mysqli_real_escape_string($db,$id)."'";
}
// run all queries
if(mysqli_multi_query($db,implode(';',$queries)){
do{
$tally+=mysqli_affected_rows($db);
} while(mysqli_more_results($db) && mysqli_next_result($db));
}
// assess the outcome
if($error_mess=mysqli_error($db)){
echo "Syntax Error: $error_mess";
}else{
echo "$tally row",($tally!=1?"s":"")," updated";
}
mysqli_close($con);
}

how to pass a mysql row variable with a form

I have this page where it displays some info from a database
here is some code:
while($row = mysql_fetch_array($result))
{ echo "<tr>";
echo "<td><form action='deleteitem.php' method='get'><input type='submit'></form> </td>";
echo "<td><textarea name='description' cols='40' rows='8'>" . $row['some_info']"
}
I'm not sure how to pass the particular row's id to the action script so that an item can be deleted by the user if they want
You can use hidden input:
<input type="hidden" name="id" value="<?php echo $row['id'] ?>" />

Categories