I have a table populated from a mysql database. One of the fields is "status". I would like this cell to be a drop down box inside the table, so I can then update the particular field.
This code, correctly displays the table and currently it displays the "status" filed inside a text box that I can edit successfully. I would like this to be a drop down though.
<?php
require_once('db_connect.php');
$result = mysql_query("SELECT *
FROM queries
WHERE SR = '$_GET[SR]'
")
or die(mysql_error());
echo '<form name="Form" action="update.php" method="post">';
echo
"<table id='box-table-b'>
<thead>
<tr>
<th>SR</th>
<th>Product</th>
<th>Status</th>
</tr>
</thead>";
while($row = mysql_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['SR'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . '<input type="text" name="status" value="'.$row['status'].'" />' . "</td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
echo '<input type="submit" name="Save" value="Save" />';
echo '</form>';
?>
Can someone please show me how to do this ?
To answer the question, you should use the <select></select> tags. Example:
<select>
<option>Item 1</option>
<option>Item 2</option>
<option> ... </option>
<option selected="selected">Item N</option>
</select>
In this specific example, the dropdown would appear with "Item N" selected by default.
As a side note, isn't using mysql_* functions bad practice in general?
by drop down menu I guess you mean a select tag, anything more complicated that this requires a custom implementation.
This requires 2 steps: first you need to create the select tag and populate it with option tags, then you need to set the desired value as selected.
to create the select tag:
$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
$myselect.="<option value='$e'>$e</option>";
}
$myselect.="</select>";
$status_value is a array, you can have in your code or get it from a query.
To select the correct one you can add the following if to the code above:
$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
if($e == $row['status']){
$myselect.="<option value='$e' SELECTED>$e</option>";
}else
$myselect.="<option value='$e'>$e</option>";
}
}
$myselect.="</select>";
Related
I'm trying to limit the number of rows that the page shows according to the users selection from a drop down(think like a store page "show number of items per page")
At the moment I'm using php to call MySql and then echo out my results. I know that php cant do anything after the page loads. The next thing that comes to mind is java script.
However I have no experience with java script, I'm fair normal with java.
What are other options that you would suggest?
note: I want to limit my while loop not the mysql result.
Here is my code as it stands now:
<form name="input" action="EditPartyP.php" method="post">
<tr>
<td>Party ID</td><td>Party Name</td>
<td>Start Date</td><td>End Date</td><td>Sales</td><td>VIEW:
The drop down selection options that are being offered.
<select>
<option value="10">10</option>
<option value="20">20</option>
<option value="40">40</option>
<option value="80">80</option>
</select>
</td>
The results from the query being displayed, I under stand that I will have to change this because the php is done when the user sees the output html.
<?php
$ID = $_SESSION['ID'];
$result = PartyData::PartyLookupByID($_SESSION['ID'], "DESC");
This loop now just runs until the result runs out of data.
I need to make it stop when the users number is reached as well.
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<form name="input" action="EditPartyP.php" method="post">';
echo "<td>" . $row['PartyID'] . "</td>";
echo "<td>" . $row['PartyName'] . "</td>";
echo "<td>" . $row['sDate'] . "</td>";
echo "<td>" . $row['eDate'] . "</td>";
echo "<td>" . $row['PartyOrderTotal'] . " </td>";
echo '<input type="hidden" value=" ' . $row['PartyID'] . '" name="PartyID">';
echo "<td>" . '<button type="submit" value="Edit" name="Action">Edit</button>' . "</td>";
echo '</form>';
echo "</tr>";
}
?>
</table>
Thank you for reading and a comment helps more than a down vote.
You might want to look into https://datatables.net if you want features such as per page row diaplay or so called pagination.
Based on the selected page (via a $_GET['var'] and the users choice of number of records per page, you have to adjust your query with the limit statement.
SELECT * FROM table LIMIT 10, 50
Would start at the 10th record and will select 50 records max.
You can use SQL Limit command to retrieve specific number of data.
SELECT column_name(s) FROM table_name LIMIT number;
Hi im doing my final project this year. I need to input data into a database together with month and day. However, month and day is recalled from another database. Some of codes does not work. Btw im using php and mysql.
there are 3 pages.
This is the first page: pilih.php
<html>
<form action="pilih_process.php" method="POST">
<table>
<tr>
<td>Month:</td>
<td><select name="month"/>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
</select>
</td>
</tr>
<tr>
<td>Day:</td>
<td><select name="day"/>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</td>
</tr>
<tr>
<td>Group:</td>
<td><select name="group"/>
<option>1</option>
<option>2</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"/></td>
</tr>
</table>
</form>
</html>
This is the second page: pilih_process.php
<html>
<?php
include ('connect_database.php')
$query1 = mysql_query("SELECT Student_ID, Student_Name FROM student_details WHERE Group_ID=$group");
$month = $_POST['month'];
$day = $_POST['day'];
$group = $_POST['group'];
echo "<table>";
echo "<tr>";
echo "<td>Month: $month</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Day: $day</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Group: $group</td>";
echo "</tr>";
echo "</table>";
echo "<form action='pilih_process2.php' method='POST'>";
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
while ($row = mysql_fetch_array($query1) )
{
echo "<tr>";
echo '<td>' . $row['Student_ID'] . '</td>';
echo '<td>' . $row['Student_Name']. '</td>';
echo "<td><input type='checkbox' name='status[]' value='Expert'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Intermediate'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Amature'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Noob'/></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td><input type='submit' name='submit' value='Submit'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
</html>
This is the third page: pilih_process2.php
<html>
<?php
include ('connect_database.php');
$month = $_SESSION['month']; //From First page: pilih.php
$day = $_SESSION["day"]; //From First page: pilih.php
$student_id = $_SESSION["Student_ID"]; //From Second page: pilih_process.php
$student_name = $_SESSION["Student_Name"]; //From Second page: pilih_process.php
$group = $_POST['group']; //From First page: pilih.php
$status = $_POST['status']; //From Second page: pilih_process.php
mysql_query("INSERT into mencuba(Month_ID, Day_ID, Student_ID, Student_Name, Group_ID, Status) VALUES('$month', '$day', '$student_id', '$student_name', '$group', '$status')");
/*
$status_test = $_GET['status']; //I DONT KNOW WHETHER THIS ARRAY METHOD IS CORRECT
for($i=0; $i < count($checked); $i++){
echo "Selected " . $checked[$i] . "<br/>";
}
*/
?>
Here how it goes:
From the First page, the user have to choose which month, day and group and submit.
On the Second page, it displays the Student ID, Student Name, Group and Status which is based on what group.
For the status, the user has to pick one and submit.
On the third page. The data Month_ID, Day_ID, Student_ID, Student_Name, Group_ID and Status will be inserted into the database.
PROBLEMS:
When I insert the data, only Month_ID, Day_ID, Group_ID and Status is inserted into the database. I was unable to call the variable Student_ID and variable Student_Name.
it only insert one data. if there are 100 data to enter obviously ihave to use array which i do not really sure how to use.
We are taught simple PHP and MySql
As noted in other answers, there are a few things going on here.
You are trying to use the variable $group before you've set it
You are using raw POST data in a query - this is very very bad practice - even if it's a project you'll score better for doing it better. At the very least wrap that in mysql_real_escape_string so it looks like you tried.
$group = mysql_real_escape_string($_POST['group']);
As for the main issue:
You need to pass the first forms parameter to the second form. Just putting them in the table will not save them. Pass them as hidden form values
echo "<input type="hidden" name="group" value="'.$group.'" />";
Beyond that, if you expect to have multiple results for your query (ie: multiple people from one group) then you need to change your logic again because each one will just be overriding the form values from the one before.
You place $group in your query before you initiate it. you must initiate it first and then place it to query.
like this :
$month = $_POST['month'];
$day = $_POST['day'];
$group = $_POST['group'];
$query1 = mysql_query("SELECT
Student_ID, Student_Name
FROM student_details
WHERE Group_ID=$group");
About status thing
you have to use select instead of checkbox , if you want to give multiple select use another name property for each field in your form.
So basically I don't really know how to explain what i need or if the title is even correct but i need some help.I'll explain what I'm trying to do the best I can.
This is a picture of the page I'm working on and referring to
http://i40.tinypic.com/14m5euh.png .
SO basically what I need to do is,when the user uploads a file and clicks on upload it will upload to a database along with the job number,so far I've managed to get it to update into the database.
But I can't get the job number in the database because my information is all in an array from the database I tried to store the information into a session variable,its loops and gets replaced by the next one,so basically what ever the last job number is will be stored in the database which of cause is wrong?
The last problem I've is,how do I get the users answer from the drop down box into a variable ?
My code:
$con = mysqli_connect("localhost", "root", "","fixandrun") or die(mysqli_error());
$data = mysqli_query($con,"SELECT * FROM bookjob") or die(mysql_error());
Print "<table border cellpadding=10>";
Print "<table border='2'>";
echo "<table border='2' cellpadding=10>
<tr>
<th> Job number</th>
<th> Job details</th>
<th> Pc number </th>
<th> Job status</th>
<th> Upload report</th>
<th> Update Job</th>
</tr>";
while($row= mysqli_fetch_array($data))
{
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td> <form action='reportupload.php' method='post' enctype='multipart/form-data' name='uploadform'>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
<input name='upload' type='submit' id='upload' value='Upload'>
</form>
</td>";
echo "<td> <a href='updateadmin.php'>Update information</a></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
There are many problems with your code. It would be extremely time consuming to go through them all, so I'll run down some things you need to look at to get this going.
FIrstly, you can make as many forms as you want, and with the help of PHP you can do this dynamically as youre doing. The problem is for every job, youre creating a new form with the same exact name. You need to find a way to make them the name of each form dynamic if youre going to do that.
Secondly, you probably shouldnt make a new form, so take all your opening and closing tags out of the loop, put the opening form tag before the while, and the ending form tag after the while loop.
I would suggest you go read about HTML forms a little more and PHP while loops and fetching information from a database with embedded forms before messing with this more. No offence, but I think you need a little more knowledge on this before fiddling with it.
I would suggest making a test database and table with maybe 3 fields, then making a small form with a couple field and seeing if you can fetch the data and update it with your form first.
i think i have done what you ment, is this right now?
$cnt = 0;
while($row= mysqli_fetch_array($data))
{
echo " <form action='test.php' method='post' enctype='multipart/form-data' name='uploadform' . $cnt>";
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'. $cnt] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
</td>";
echo "<td><input name='upload' type='submit' id='upload' value='Upload'></td>";
echo "</tr>";
echo "</form>";
++$cnt;
}
echo "</table>";
echo "<br>";
?>
I'm having an issue trying to update multiple entries in my database via a php populated drop down menu. Here is the code on my page that populates the table showing me all entries currently in my database:
$result = mysqli_query($con,"SELECT * FROM Submissions");
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Title</th>
<th>Text</th>
<th>Public Post OK?</th>
<th>Date/Time Submitted</th>
<th>Approved?</th>
<th>Test Approved</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . nl2br($row['text']) . "</td>";
echo "<td>" . $row['publicpost'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td><select name=\"approved\"><option value=\"" . $row['approved'] . "\">" . $row['approved'] . "</option><option value=\"yes\">Yes</option><option value=\"no\">No Again</option></select></td>";
echo "<td>" . $row['approved'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<br><br>
<form action="update.php" method="post">
<input type="submit" name="SubmitButton" value="Update" class="submit" style="cursor:pointer;">
</form>
<?php
mysqli_close($con);
?>
This is the php code for "update.php":
$approved = $_POST['approved'];
mysqli_query($con,"UPDATE Submissions SET approved = $approved");
$update_query= "UPDATE Submissions SET approved = '$approved'";
if(mysqli_query($con,$update_query)){
echo "updated";}
else {
echo "fail";}
?>
<form action="approvesubmissions.php">
<input type="submit" value="Approve Submissions page">
</form>
The goal is to have the ability to update the field "approved" with a drop down menu from "NO" to "YES" or vice versa. Instead, what is happening with this query, is that it is erasing the data in the "approved" field instead of updating it. I'm somewhat new to php and i have researched a TON on this and have come up with no solutions. Any help is GREATLY appreciated!
First, let's assume 'approved' is a TINYINT(1) or something.
Your select html should be more like this. It will autofill based on the DB value.
$selected = 'selected="selected"'; // pre-selection attribute
$isApproved = !!$row['approved']; // is this active? (approved is 1 or 0)
echo '<select name="approved">
<option value="1" ' . ($isApproved ? $selected : '') . '>Yes</option>
<option value="0" ' . (!$isApproved ? $selected : ''). '>No</option>
</select>';
Secondly, your form is at the bottom of the table, but your input that you want is in the table. When you submit your form, there is no $_POST['approved'], because that's technically not in a form. To fix, you'll need to put your opening form tag at the top before the table. Then, you'll want to put your submit button and closing form tag at the end, after you've echoed the table out.
Thirdly, your post.php page should NOT ever take user input directly into a query. But, simply do this:
// Convert input to boolean answer, then int (for the query).
$approved = isset($_POST['approved']) ? (int)!!$_POST['approved'] : 0;
mysqli_query($con,"UPDATE Submissions SET approved = '$approved'");
While we're on the topic, this would be a great time to jump into prepared statements for your project. It might sound scary, but it can save you from SQL injection.
// Make the prepared statement
$query = mysqli_prepare("UPDATE Submissions SET approved = ?");
// Safely bind your params
mysqli_stmt_bind_param($query, "i", $approved);
// Run it
mysqli_stmt_execute($query);
// "close" the statement (hint: it's reusable for things like bulk updates, etc)
mysqli_stmt_close($query);
I need your advice. I fetch data from database to table: ID, Name.
In table are Actions: Delete, Enable, Block. When action Delete
is selected, I would like, that respectively record will be deleted.
However, my script does not work and always delete last record, even I select
another record. I think problem is, that select name and hidden input
name is similar for all records. But I can not find way, how to create
them with different names.
Any advice is welcome.
HTML:
<form method='post'>
<table border='1'>
<tr>
<th> ID </th>
<th> Name </th>
<th> Action </th>
</tr>
Code:
$db = new PDO('mysql:host=localhost;dbname=****;charset=utf8', '**', '**');
$query = $db->query("SELECT ID,statusas,login,vardas,email FROM users");
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<tr><input type='hidden' name='id' value='".$row[0]."'>";
echo "<td>".$row[0]."</td>";
//echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
//echo "<td>".$row[3]."</td>";
// echo "<td>".$row[4]."</td>";
echo "<td><select name='action'>
<option value='choose'>Choose..</option>
<option value='delete'> Delete </option>
<option value='enable'> Enable </option>
<option value='block'> Block</option>
</select></td>";
echo "</tr>";
}
echo "<br><input type='submit' name='submit'></table></form>";
if($_POST['submit']) {
if ($_POST['action']== 'delete') {
echo $_POST['id']; // delete query, but now I am just checking if I get a proper ID.
}
}
else {
echo "bad";
}
You are using the same name attribute on every row in the form, so they are being overridden and it's using the last one.
What you could do is either wrap every row in its own form, or you could do something like this, and have only 1 submit button to execute the action on every row:
// remove hidden id element
...
echo "<td><select name='action[" . $row[0] . "]'>"
...
// remove submit button in the loop, but add it after the while loop
...
if (isset($_POST['action']))
{
foreach ($_POST['action'] as $id => $action)
{
if ($action !== 'choose')
{
// do action on the id;
echo $id . " -> " . $action . "<br>";
}
}
}
You put every ID in the form, which results in the CGI to send something like this: id=1, id=2, id=3, etc. PHP then only reads the last ID and deletes that.
To fix it, give each row its own form.
while($row = $query->fetch(PDO::FETCH_BOTH)) {
echo "<form method='post'>";
echo "<tr><input type='hidden' name='id' value='".$row[0]."'>";
echo "<td>".$row[0]."</td>";
//echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
//echo "<td>".$row[3]."</td>";
// echo "<td>".$row[4]."</td>";
echo "<td><select name='action'>
<option value='choose'>Choose..</option>
<option value='delete'> Delete </option>
<option value='enable'> Enable </option>
<option value='block'> Block</option>
</select></td>";
echo "</tr>";
echo "</form>";
}
echo "<br><input type='submit' name='submit'></table>";