I am trying to create a table showing multiple users data. The data in the table will then be able to be edited and updated. Below is an example of the way the form is laid out:
echo "<form action=AdminUpdateLecInfo.php method=post>";
while ($return = mysql_fetch_assoc($result)) {
$phonenumber = "$return[PhoneNumber]";
$number = str_pad($phonenumber, 11, "0", STR_PAD_LEFT);
echo " <tr class='data'>
<input type='hidden' name='id'".$return['ID']."'' value= '".$return['ID']."' />
<td class = 'title'><input class = 'title' type='text' name='title'".$return['ID']."'' value= '".$return['Title']."' /></td>
}
echo "</table>
<input class='submit' type='submit' value='Update Info' />
</form>
Once the table is created the information is passed to the 'update.php' script.
$sql="UPDATE completeinfo SET Title='".$_POST['title'][$return['ID']]."'
WHERE ID = '".$_POST['id'][$return['ID']]."'";
header("Location:Home.html");
The problem I'm having is that I need to add the '".$return['ID']."' to the name of each input field so that not all users details are updated with the same values. I am unsure if I need to apply a foreach loop around this query so that it applies to each user and updates their details. Currently however the update query is not working presumably because the post method is not fetching the values from the form correctly.
Your problem is the name of your fields, use that :
$return_id = $return['ID'];
echo "
<tr class='data'>
<td class = 'title'>
<input type='hidden' name='id$return_id' value='$return_id' />
<input class='title' type='text' name='title$return_id' value='$return_id' />
</td>
</tr>";
Before doing your mysql update, do var_dump($_POST);, you'll be shown the content of the HTTP POST parameters so you can see what they are and how to use them in the query.
You have to use the names of your text fields as array so you can iterate your array
name='title'".$return['ID']."'
name='id'".$return['ID']."'
Related
I have a table created based on the rows in a mysql database table. So the rows are not fixed but depend on records from the source database table.
HTML and part of DB query:
$query=mysqli_query($db_server, "SELECT DateTime, RefNumber, COUNT(*) AS count FROM requests GROUP BY DateTime");
$i=1;
foreach($query as $row)
{?>
<tr>
<td> <?php echo $i ?> </td>
<td><input type='text' name='date_sent[]' value='<?php echo $row['DateTime'];?>' style='border-radius:0; margin:0' /></td>
<td><input id='ref<?php echo $i?>' type='text' name='ref[]' value='<?php echo $row['RefNumber'];?>' style='border-radius:0; margin:0' /></td>
<td ><input id='noofitems<?php echo $i?>' type='text' class='prices' name='noofitems[]' value='<?php echo $row['count'];?>' style='text-align:center; border-radius:0; margin:0' /></td>
<td align='center'><input id='action1<?php echo $i?>' type='checkbox' class='prices' name='action2[]' style='width:20px; border-radius:0; margin:0' /></td>
</tr>
<?php
$i++;
}
Table:
Dynamic table - from DB
What I want:
A pure PHP code that will pick REFENCE NUMBERS of the exact corresponding checked rows. I need these reference numbers to build another DB query to create a second table on another page using PHP sessions.
I have tried calling the following to store the ref numbers. Unfortunately, it only picks the first consecutive reference numbers. I need to pick only those whose rows are checked.
<?php
session_start();
$rowCount = count($_POST['action2']);
for($i=0; $i<$rowCount; $i++){
if(isset($_POST['action2'][$i])){
$_ref='ref'.$i;
$_SESSION["$_ref"]=$_POST['ref'][$i];
}
}
$_SESSION['rowCount']=$rowCount;
Or even this:
<?php
session_start();
if(isset($_POST['action2'])){
$rowCount=count($_POST['action2']);
$i=0;
foreach($_POST['action2'] as $i){
$ref='ref'+$i;
$_SESSION["$ref"]=$_POST['ref'][$i];
$i++;
}
}
You have generated checkboxes that look like this in HTML
<input id="action14" type="checkbox" class="prices" name="action2[]" style="width:20px; border-radius:0; margin:0">
How can you tell which ID this checkbox refers to? You need to assign a value to each checkbox so that when it's sent to PHP you can tell which row was marked.
Use value='<?=$row['SomeID']?>' to populate the value attribute that will be sent to PHP. Then you can do foreach ($_POST['action2'] as $ID)
I'm having a trouble finding a solution to this. Here is the case, assume that I have 5 books in the database, i will display them by doing a while then mysql_fetch_array assume that I create a table. This is the plot
echo"
<td>
<form action='bookext.php' method='post'>
<input type='submit' value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
the $row['book_title'] works fine because it must display what is the title of the book in the database. But, how can I get it's unique value which is book_id and send it to bookext.php?
PS: Sorry for my title, I can't pull the right english
First of all your current input needs a name attribute:
"<input type='submit' name="title" value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
Since you're using the POST method this will insure the value is available in the $_POST array, like $_POST['title'].
For the id you can add a hidden input to your form:
"<input type='hidden' name='book_id' value='".$row['book_id']."'>"
This will be available to you in $_POST['book_id'] when you submit.
Just insert hidden field to your form
<input type="hidden" name="POST_NAME" value="YOUR_VALUE_TO_IDENTIFY">
Don't use forms in tables.
<form action='bookext.php' method='post'>
<table>
<tr>
<?php foreach($books AS $book){ ?>
<td>
<button type="submit" name="bookId" value="<?php echo $book['id']; ?>">
Title of the book
</button>
</td>
<?php } ?>
</tr>
</table>
</form>
bookext.php
<?php
$id = $_POST['bookId'];
The following code is in tag_show.php.I have to show all the data from the database and I am trying to edit the database from php code so i form the edit button at the end of all row in html table.
echo"<br><br><br><br>
<table border='6' style= 'background-color: #FFFFE0; color: #761a9b; margin: 2 auto;'>
<thead>
<tr>
<th>tag_title</th>
<th>description</th>
<th>show_in_welcome</th>
<th>status</th>
</tr>
</thread>
<tbody>";
while($row = mysqli_fetch_assoc($get_detail))
{
$_SESSION['tag_title'] =$row['tag_title'];
$_SESSION['description'] =$row['description'];
$_SESSION['show_in_welcome'] =$row['show_in_welcome'];
$_SESSION['status']=$row['status'];
echo
"<tr>
<td>{$_SESSION['tag_title']}</td>
<td>{$_SESSION['description']}</td>
<td>{$_SESSION['show_in_welcome']}</td>
<td>{$row['status']}</td>
<td><form action='edit.php' method='POST'><input type='hidden' name='tag_title' value='".$row["tag_title"]."'/><input type='submit' name='submit-btn' value='edit' /></form></td>
</tr>\n";
}
and the edit.php is
<?php
session_start();
echo"<form action='tag_show.php' method='post'>
<br><br><br>
Tag Title<br><input name='tag_title' type='text' value='{$_SESSION['tag_title']}'><br><br>
Description <br><input name='description' type='text' value='{$_SESSION['description']}'><br><br>
Show in welcome<br><input name='show_in_welcome' type='text' value='{$_SESSION['show_in_welcome']}'><br><br>
Status<br><input name='status' type='text' value='{$_SESSION['status']}'> <br><br>
<input name='submit1' type='submit' value='Update'>
</form>";
?>
but the $_SESSION returns the last row value in HTML table. But i want the receptive row value when i click on edit button.
You don't need a session variable to pass the value to edit.php file.
Hope there is a unique key to identify the record from database. I assume it as recordid. Pass this value to edit.php as a query string
tag_show.php
$output = '';
while($row = mysqli_fetch_assoc($get_detail)){
$output '<tr>
<td>'.$row['tag_title'].'</td>
<td>'.$row['description'].'</td>
<td>'.$row['show_in_welcome'].'</td>
<td>'.$row['status'].'</td>
<td>edit</td>
</tr>';
}
In edit.php, you can get the recordid from query string and retrieve the data from database to populate the form
<?php
if($_GET['editid']!=''){
//get record corresponding to the 'editid' from database
//I assume the record is for and store in a variable $editdata
?>
<form action="tag_show.php" method="post">
<br><br><br>
Tag Title<br><input name="tag_title" type="text" value="<?=$editdata['tag_title']?>"><br><br>
Description <br><input name="description" type="text" value="<?=$editdata['description']?>"><br><br>
Show in welcome<br><input name="show_in_welcome" type="text" value="<?=$editdata['show_in_welcome']?>"><br><br>
Status<br><input name="status" type="text" value="<?=$editdata['status']?>"> <br><br>
<input name="submit1" type="submit" value="Update">
</form>
<?php
}
?>
Looks like you are never updating the session data with the information received from the form. You will need something like this somewhere in your code.
$_SESSION['tag_title'] = $_POST['tag_title'];
$_SESSION['description'] = $_POST['description'];
$_SESSION['show_in_welcome'] = $_POST['show_in_welcome'];
$_SESSION['status']= $_POST['status'];
A few things to consider.
Make sure your db info does not overwrite the values after user submits form.
Always sanitize/validate user input before using it (unlike the above code, since it's just an example).
here session make no sense. each time loop runs previous value replaced therefore you are getting last value. Instead of you can use the concept of query strings. Instead of Printing Form print a anchor tag
<a href='file.php?id='.$row["tag_title"]>Edit</a>
Being your tag_title unique.
if(isset($_GET['id))
{
$id = $_['id']
// Your code based on id
}
Now using id run mysql query and select desired data and display in form
Certainly your code will return the last row as the session array overwrites the previous row's value after every iteration.
Make the session array as multi dimensional like $_SESSION[$id]["tag_title"] and then in your edit.php refer to the session array using the id value.
$_SESSION[$id] = array('tag_title' => $row['tag_title'], 'description' => $row['description'], 'show_in_welcome' => $row['show_in_welcome'], 'status' => $row['status'])
Then increment the $id after each iteration.
I’m trying to update multiple rows in a mysqli table from an HTML form. The data seems to be getting from the form to my "update database" page. But it’s not going into the database.
Here’s the relevant part of the form:
for($i=0;$i<$rowcount;$i++)
{
$row = mysqli_fetch_array($result);
echo "<tr>
<td> $row[SubFirstName] $row[SubLastName] </td>
<td> $row[PerfFirstName] $row[PerfLastName] </td>
<td style='display:none'><input type='text' class='input' name='PerformerID[]' value= '$row[PerformerID]' /> Years</td>
<td><input type='text' class='input' size= '5' name='GKYears[]' value= '$row[GKYears]' /> Years</td>
</tr>";
}
And here’s the code to insert the values into the database:
for($i=0;$i<$count;$i++)
{
mysqli_query($con, "UPDATE Performers SET
GKYears = '$_POST[GKYears][$i]'
WHERE PerformerID = '$_POST[PerformerID][$i]'");
}
When I do a var_dump of the POST data, it all seems to be there. Can someone explain how to fix this, and why it’s wrong? I’ve got other more complex variants of the same issue for the other pages.
Bad structure. Don't use CSS to simulate a hidden form field, and you don't even need the hidden field:
echo <<<EOL
<tr>
<td>... name stuff ...</td>
<td>... perf stuff ...</td>
<td><input type="text" name="GKYears[{$row['PerformerID']}]" value="{$row['GKYears']}" /></td>
</tr>
EOL;
Note how the ID value gets embedded in the field name, so you'll end up with
<input ... name="GKYears[42]" ... />
<input ... name="GKYears[103]" ... />
Then your PHP-side stuff becomes simply:
foreach($_POST['GKYears'] as $rowID => $value) {
... update db for record Id $rowID with value $value
}
Beyond that, your code is gaping wide and just begging for an SQL injection attack.
Got a bit of a confusing situation, I have a form on my webpage that allows a user to enter information into a database...
Form
<form id="insertbill">
Total <input type="text" id="total" name="total" /><br />
Bill name<input type="text" id="bill-name" name="bill-name" /><br />
bill descriptiion <input type="text" id="bill-description" name="bill-description" /><br />
bill colour<input type="text" id="bill-colour" name="bill-colour" />
<input type="button" value="submit" onClick="insertBill();" />
</form>
This form then send the information via AJAX to my php which then inputs it into my DB
AJAX
function insertBill()
{
$.post('insert_bill.php', $('#insertbill').serialize(),
function(data) {
$('#bills').append(data);
});
};
PHP
$uid = $_SESSION['oauth_id'];
$bill = mysql_real_escape_string($_POST['total']);
$billname = mysql_real_escape_string($_POST['bill-name']);
$billdescription = mysql_real_escape_string($_POST['bill-description']);
$billcolour = mysql_real_escape_string($_POST['bill-colour']);
#Insert Record
$query = mysql_query("INSERT INTO `outgoings` (user_id, bill, bill_name, bill_description, bill_colour ) VALUES ('$uid', '$bill', '$billname', '$billdescription', '$billcolour')") or die(mysql_error());
Once this is done, the data is then returned to the webpage, with another form that allows the user to update the record...
Returned data/form
Print "<tr>";
Print "<th>total:</th> <td>".$bill . "</td> ";
Print "<th>bill name:</th> <td>".$$billname . "</td> ";
Print "<th>bill deposit:</th> <td>".$billdescription . "</td> ";
Print "<th>colour:</th> <td>". $billcolour . " </td></tr>";
echo "<th>edit:</th> <td>
<form id='bill-upd'>
<input type='hidden' value='". $billname."' name='billid' id='billid''>
Total <input type='text' id='total' name='total' /><br />
Bill name<input type='text' id='bill-name' name='bill-name' /><br />
bill descriptiion <input type='text' id='bill-description' name='bill-description' /><br />
bill colour<input type='text' id='bill-colour' name='bill-colour' />
<input type='button' value='submit' onClick='updateBill();' />
</form>
</td>";
My problem is that the returned form doesn't update, this is because I need to somehow find the id of the record that was inserted, place it into my form to be returned I think... I hope this isn't too confusing, but has anybody a better way of doing this without reloading the page?
here's what what you could include on your, insert_bill.php, view page:
<script>
var json=<?
$arr=array($bill, $billname, $billdescription, $billcolour);
echo json_encode($arr); ?>;
$('tr td:nth-child(0)').html(json[0]);
$('tr td:nth-child(1)').html(json[1]);
$('tr td:nth-child(2)').html(json[2]);
$('tr td:nth-child(3)').html(json[3]);
</script>
I guess the one outstanding issue is the "id" you says gets returned from your database. Can you explain a little further? Are their multiple forms on your insert_bill.php page? Regardless, you could put that $id into the array and feed it into and html() function like I did with the other variables.
You need to return the data from the database and then change, i imagine if you just need the id you must do.
in PHP
echo mysql_last_insert_id(); // so you get the last inserted id and "return" to the JS
In JS, inside the function data
You get the echo as a return and change the id
and then $("#billid").val(data)
I hope it help you