I'm trying to create an Array from multiple checkboxes that are created via a while loop. the check boxes are named 'to_delete[1]', 'to_delete[2]', etc. etc.
the array statements i've tried are these:
$toDelete = array($_POST['to_delete']);
$toDelete = array($_POST['to_delete'][]);
to veryify there is an array, i go to print but find it is empty print_r($toDelete). What am i doing wrong?
the code is below. Yes, i'm doing this procedurally, and then will re-write as OOP.
big thanks for any help on this!,
$showQ = "SELECT * FROM urls";
$result = mysql_query($showQ);
$numRows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
if(!$result) {
mysql_error();
} else {
echo "<form action='".$_SERVER["PHP_SELF"]."' method='post'>";
$i=0;
while($row = mysql_fetch_array($result)) {
echo "<input type='text' class='manId' name='manId[]' value=" . $row['id'] . " />";
echo "<input type='text' class='url' name='url[]' value=" . $row['url'] . " />";
echo "<input type='checkbox' name='to_delete[" .$i. "]' /><br/>\n";
$i++;
}
echo "<input type='submit' value='delete' name='submit' />";
echo "</form>";
}
$toDelete = array($_POST['to_delete[]']);
print_r($toDelete);
Ultimately, i want to traverse the array, see which ones are checked and then delete the corresponding row from the table.
You don't want to create an array with the array() call, but rather to access one. $_POST will receive input names ending with [] as arrays, so the corresponding array keys in $_POST will already be arrays. The way you've written your code, you are assigning a one-element array containing a sub-array from $_POST. Instead, try the following:
// Test if $_POST['to_delete'] is set and non-empty
// If it's empty, create an empty array, otherwise assign it to $toDelete
$toDelete = empty($_POST['to_delete']) ? array() : $_POST['to_delete'];
print_$($toDelete);
Related
I've written this code for a user to edit one row and update it in MySQL, but it always posts the last row no matter which row you have selected (there are 3 rows).
What's the problem?
<?php include("includes/db_connection.php"); ?>
<?php
global $connection;
$sid="s5";
/**select all salesman from store 5**/
$sql ="SELECT * FROM employees WHERE e_type='Salesperson' AND store_assigned='".$sid."';";
/**get the result and put into table, which can be edited by user**/
$result = mysql_query($sql);
echo "<form method='post' action='update_salesman.php'>";
echo "<table border='1'><tr><th>Employee ID</th><th>Name</th><th>Address</th><th>Email</th><th>Job Title</th><th>Store</th><th>Salary</th></tr>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td><input type='text' name='eid' value='".$row['eid']."' readonly /></td>";
echo "<td><input type='text' name='e_name' value='".$row['e_name']."' /></td>";
echo "<td><input type='text' name='e_addr' value='".$row['e_addr']."' /></td>";
echo "<td><input type='text' name='e_email' value='".$row['e_email']."' /></td>";
echo "<td><input type='text' name='e_type' value='".$row['e_type']."' /></td>";
echo "<td><input type='text' name='store_assigned' value='".$row['store_assigned']."'/></td>";
echo "<td><input type='text' name='e_salary' value='".$row['e_salary']."' /></td>";
echo "<td><input type ='submit' value='update' /></td></tr>";
}
echo "</table>";
echo "</form>";
print($sql);
?>
Get the posted data, and update it in MySQL database:
<?php include("includes/db_connection.php"); ?>
<?php
$eid = $_POST['eid'];
$ename = $_POST['e_name'];
$eaddr = $_POST['e_addr'];
$eemail = $_POST['e_email'];
$etype = $_POST['e_type'];
$estore = $_POST['store_assigned'];
$esalary = $_POST['e_salary'];
$sql = "UPDATE employees SET e_name='" . $ename . "', e_addr='" . $eaddr . "', e_email='" . $eemail . "', e_type='" . $etype . "', store_assigned='" . $estore . "', e_salary='" . $esalary . "' WHERE eid='" . $eid . "' ;";
$result = mysql_query($sql);
print("</br>" . $sql);
?>
The result is always this:
UPDATE employees SET e_name='Norah ', e_addr='111 Melwood,PA', e_email='anorahm#gmiil.com', e_type='Salesperson', store_assigned='s5', e_salary='4000.00' WHERE eid='e334' ;
Your problem is twofold. First, when generating the HTML code, you use a while loop to echo the fields. Note that the names of these fields are the same every time the loop runs. (You can see this in the generated HTML (source code). Note that on submitting, one one of the multiple same-named fields will be posted.
Second, in the PHP form handler code, you read the post data and then do one update query, while you may want to update more than one field.
The easiest way to solve this is to make sure that the field names in the HTML form are different for each of the rows, and to use a loop structure when updating the sql table such that there's an update for each row.
even though it may appear fine on the html side, it's clear what's happening on the server side when it gets the form
When the server gets the form it will only see the last record because each record will overwrite the values that come before it resulting in only getting the data from the last record
What you can do is give each set of values its own form (Wouldn't suggest). But with this method, you can leave your code almost as is, just move the form tags into the while loop. OR write the input names as e_name[], etc.
This way it will be passed as an array to the server and you can loop through to get all your values
On the server end, to get the array you would do something like
$e_names = $_POST['e_name']; //Value will be an array
I have 12 keys inside my $_post[].
I can reach them without any issue but I want to make it easier, as there will be more keys (I am thinking of 30+).
Here is an example of what I have inside my $_post:
$_post['var1']
$_post['qty1']
$_post['var2']
$_post['qty2']
$_post['var3']
$_post['qty3']
This continues till 12 (at the moment).
It is being posted this way:
$count = 0;
foreach ($results as $mat) {
$count++;
echo "<tr><td>{$count}</td>";
echo "<td><input type='text' name='var{$count}' value='{$mat['var']}' /></td>";
echo "<td><input type='text' name='qty{$count}' value='{$mat['qty']}' /></td></tr>";
}
I need to use those variables later to update my sql table, and to have it done one-by-one is a nightmare ( as I am still not sure how many it will be ).
What can I do to achieve it in an easier way ?
you can make name as array like this. var[] and qty[]
echo "<td><input type='text' name='var[]' value='{$mat['var']}' /></td>";
echo "<td><input type='text' name='qty[]' value='{$mat['qty']}' /></td></tr>";
and receive in php like this
$arr_var = $_POST['var'];
$arr_qty = $_POST['qty'];
foreach($arr_var as $key=>$value)
{
$var = $value;
$qty = $arr_qty[$key];
}
foreach($_POST as $key=>$value)
{
$$key = $value; // notice the $$, it's known as variable variable
// remove the below comment if you want to see how they are coming out
// echo "$".$key." = ". $value."<br />";
}
This will create variables names based on the name attribute of the input box.
I am trying to get the $w['id'] value for a mysql row and pass it to another page using a hidden input field and variable $blog_id. Here is the code for the first page:
$query = "SELECT * FROM blog WHERE username = '$username' ORDER BY created_date DESC;";
$result = $mysqli->query($query);
$rows = resultToArray($result);
// var_dump($rows);
foreach($rows as $r => $w) {
echo "<tr>\n";
echo "<td>{$w['title']}\n";
echo "<td>{$w['id']}\n";
echo "<td>{$w['created_date']}</td>\n";
echo "<td>{$w['updated_date']}</td>\n";
echo "<td style=\"border:none\">{$w['template']}</td>\n";
$blog_id = $w['id'];
echo "<input type=\"hidden\" name=\"id\" value=\"$blog_id\" />\n";
echo "<td style=\"border:none\"><button type=\"submit\" name=\"view\">View</button></td>\n";
echo "<td style=\"border:none\"><button type=\"submit\" name=\"edit\">Edit</button></td>\n";
echo "<td style=\"border:none\"><button type=\"submit\" name=\"delete\"><font color=\"red\">Delete</font></button></td>";
echo "</tr>\n";
}
?>
In my html table the values echo correctly but when I try to grab the $w['id'] value and pass it using, say, the edit button to the next page the value is always the lowest id value in the mysql table.
The code for the critical part in the second page is:
sec_session_start();
$username = $_SESSION['username'];
// $blog_id = $_SESSION['blog_id'];
if(isset ($_POST['edit'])) {
$blog_id = $_POST['id'];
$result = $mysqli->query("SELECT * FROM blog WHERE id = '$blog_id'");
if($result->num_rows > 0) {
$rows = resultToArray($result);
foreach($rows as $r => $w) {
?>
<body id="blog_editor">
<?php var_dump($_POST); ?>
The value of var_dump($_POST) is always ["id"]=>string(2) "43" whereas the foreach loop on the first page produces lots of different IDs.
Does anybody know what I am doing wrong or have an alternative way of doing the same kind of thing which might work?
You use a foreach and echo many different id.
I don't see any SUBMIT button or <form> tag.
What you can do is, inside the foreach loop, create a form with is own button:
<?php
foreach($rows as $r => $w) {
echo '<form action="page.php" ...>
echo ....
echo '<input type="submit">
}
?>
OR if you have multiple results inside one only form then you have to change the name of your element (which will be later give the value to `$_POST['id']) every time the loop loops.
To do this change this line:
echo "<input type=\"hidden\" name=\"id\" value=\"$blog_id\" />\n";
1) to this:
echo "<input type='hidden' name='id".$blog_id."' value='$blog_id' />\n";
and then when you call $_POST you'll have $_POST['idXX'] where XX = number of the ID
2) or TO this to create an array with all IDs on it:
echo "<input type='hidden' name='id[]' value='$blog_id' />\n";
and then $_POST['id'] will be an array
I have this array populating from mysql. But it is not showing me the first record. I can't figure out what the problem is. It should be simple. Here is the code.
$result=mysql_query("SELECT user_instance.instance_name, user_instance.host_name FROM
dba_account, user_instance WHERE dba_account.account_id = user_instance.account_id AND
dba_account.account_id = '1'");
echo mysql_error();
$nume = mysql_fetch_row($result);
while($note = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='instance_name' class='instance_name'
disabled='disabled' value='$note[instance_name]' size='25' /></td>";
echo "<td><input type='text' name='host_name' class='host_name' disabled='disabled'
value='$note[host_name]' size='25' /></td>";
echo "</tr>";
}
You're fetching the first row in $nume = mysql_fetch_row. That pulls the first record. Then you're iterating through the rest of the records in your while loop. Remove that first line.
The first time you call mysql_fetch_row(), it advances the result resource pointer to the second result. Don't do that. Especially considering you are not using the variable $nume in your loop in any way, it is unnecessary and harmful to your logic.
// Don't do this!
//$nume = mysql_fetch_row($result);
// Instead just fetch the loop
while($note = mysql_fetch_array($result)) {
// etc...
}
I'm a php/mysql newbie, working on an invoicing application. I have a summary.php page, in which I'm using php to query a db and render a table with the retrieved data:
< ?php
mysql_connect ... [ snip ] ...
// retrieve all data in the 'Invoices' table ...
$result = mysql_query("SELECT * FROM Invoices") or die(mysql_error());
// ... and store each record in a variable:
$row = mysql_fetch_array($result);
and my table is:
<table class="record-summary">
<form action="/edit.php" method="post">
<tr>
<th ...
[ snip: more table headers ] ...
<?php
$controls = "<input type=\"submit\" value=\"details.php\" /><input type=\"submit\" value=\"edit.php\" />";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class=\"id\">". $row['ID']. "</td>";
echo "<td class=\"inv\">". $row['invoiceNumber']. "</td>";
[ snip: more data cells ] ...
echo "<td class=\"controls\">". $controls . "</td>";
The last cell (td.controls) in each row contains two submits, one linking to a detail view (details.php), the other to a form page (edit.php). My objective is to populate either page with the values of the record/row. Where I need help is how exactly to use the $row variable to carry this array to e.g., edit.php, and populate the fields in the form (actually I'm fairly certain I have the syntax for the form inputs' value attributes). Both details.php and edit.php have all the data fields in the record represented, though in summary.php only a portion of the fields are displayed (ergo, 'summary.php'). So my questions are:
How do I ensure that when either button is clicked, the $row contains the values of the given record, and how to push the array into either details.php or edit.php?
Many thanks in advance,
src
You can pass the ID to edit.php and details.php but without the form just with two links to edit.php?id=and details.php?id=:
<?php
// $controls = "<input type=\"submit\" value=\"details.php\" /><input type=\"submit\" value=\"edit.php\" />"; <-- is not necessary
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class=\"id\">details.php | edit.php</td>";
echo "<td class=\"inv\">". $row['invoiceNumber']. "</td>";
// [ snip: more data cells ] ...
// echo "<td class=\"controls\">". $controls . "</td>"; <-- is not necessary
You can grab the data using the id:
//....
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
}
else
{
die("There is no id.");
}
$sql = sprintf("SELECT * FROM Invoices WHERE id = %s", $id);
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<input type=\"hidden\" name=\"row\" value=\"" . $row . "\"/>";
Include this in your loop and your form's "action" script will be able to access the corresponding $row variable via $_POST['row'].