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);
Related
I have a table that I am echoing out and now I added a check box to the end of it.
I would like when that box is checked. It can be checked on multiple items and then the save button is clicked. It would go to the post page and set complete in the database to "Yes" for all the IDs that were checked.
Problem is I do not understand how the checkbox will know which ID is which once it goes to the post page.
<?php
$conduct = $_SESSION['username'];
$query = mysqli_query($con, "SELECT * FROM newworders WHERE train = 'Yes' AND conductor = '$conduct' AND complete = ' '");
echo "<table id='tb' border='1'>
<tr class='head'>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . '' . $row['first'] . '<br />' . "</td>";
echo "<td>" . '' . $row['last'] . '<br />' . "</td>";
echo "<td>"."<input type='checkbox' value='Yes' name='complete'" . "</br>". "</td>";
}
<div class='new'>
<form action="savepending.php" method='POST'>
<input type='submit' name ='save'/>
</form>
</div>
Then the post page.
<?php
if(isset($_POST['save']))
{
$query = mysqli_query($con, "UPDATE newworders SET complete = 'Yes' WHERE ")
}
?>
I have no idea what to put in the WHERE part. I just dont understand how it will tell which ID is which.
Make your checkboxes an arrray, indexed by newworders primary key. Then you can just iterate through complete. PHP arrays are associative, so you will only have as many elements as you have checkboxes.
I have displayed a table of my data from the data base with check boxes to the left. I want to find a way to link the check boxes to the question number (ID). when I hit submit I want the selected id's to be echoed. pretty much I want someone to be able to select the questions they want and then display them.
<?php
$con=mysqli_connect("####","####","#####","###");
$result = mysqli_query($con,"SELECT * FROM q_and_a ");
echo "<table border='1'>
<tr>
<th>Add</th>
<th>#</th>
<th>Question</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>Answer</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" name="questions[]" value="$id"></td>';
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['question'] . "</td>";
echo "<td>" . $row['choiceA'] . "</td>";
echo "<td>" . $row['choiceB'] . "</td>";
echo "<td>" . $row['choiceC'] . "</td>";
echo "<td>" . $row['choiceD'] . "</td>";
echo "<td>" . $row['answer'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
submit button
<form method="POST" action="makeTest.php">
<input type="submit" name="make" value="Make Test">
</form>
Make some edits to your code then it will work.
1. Change your query by adding fields not * (to ensure performance and display order)
$result = mysqli_query($con,"SELECT id,question,choiceA,choiceB,choiceC,choiceD,answer FROM q_and_a ");
then before while block open form tag(HTML)
<?php
//above codes will be there as you show before
echo '<form method="POST" action="makeTest.php">';
while($row = mysqli_fetch_array($result)){
{ $id=$row['id']; // initialize your id here, so as to pass it in checkbox too
// then continue with your code
}
?>
<input type="submit" name="make" value="Make Test">
</form>
in maketest.php yo can hande ckeckbox using foreach, see below
foreach($_POST['questions'] as $questions){
//do your job
}
I'm creating a table and when the user clicks on the button I want it to open a php file called player_profile, where that page will call up more information on that particular player.
When it comes to passing variable through pages I'm aware that something along these lines will work.
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
Yet when I add it to the button the php file wont load. Below is my code without passing a variable that works.
<?php
$result = mysql_query("SELECT *, CONCAT(FirstName,' ', LastName) AS Name FROM Player WHERE TeamID = '$TeamID' ORDER BY LastName ASC");
echo "<table id='customers' border='1'>
<tr>
<th>PlayerID</th>
<th>Name</th>
<th>Position</th>
<th>Tries</th>
<th>Tackles</th>
<th>Turnovers</th>
<th> Info </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["PlayerID"] . "</td>";
echo "<td>" . $row["Name"] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td>" . $row['Tries'] . "</td>";
echo "<td>" . $row['Tackles'] . "</td>";
echo "<td>" . $row['Turnovers'] . "</td>";
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."';>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
echo "</tr>";
}
echo "</table>";
?>
The code above works perfectly at displaying a table and taking me to the player_profile page. But if change the button to the code below the page so that I can pass a variable it doesn't load
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."';>
$_SESSION['varname'] = ".$row['PlayerID'].";
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
Don't use the session to pass parameters. It breaks the way users expect a page works.
Use $_GET parameters:
John
Then in player_profile.php do:
$id = isset($_GET["id"]) ? $_GET["id"] : false;
if ($id === false) {
exit("missing input");
}
// etc
The problem with using a session here is that if someone opens two different player profiles what can he expect to see? What if he refreshes one of them?
A more advanced and involved way of parameter passing uses URL Rewriting to get nice looking URLs like:
John
You have code trying to set a session inside your echo. You want it separate:
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."'>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
If you want to access the PlayerID variable on the player_profile.php you just use:
$var_value = $_GET['id'];
You could also put it into a session var at that point, but I don't know why you would need that.
Ive got to make a web-based checkout for an assignment and have come across a problem, ive imported a database and set up the data in a table with added check boxes alongside it. I need to take the reference number (stored first in the array) onto another page using sessions. From using var_dump i cant seem to get anything from the selected from the table.
Code:
Button code
<p><tr>
<input type="submit" name="Select" id="Select" value="Add Selected To Cart"/>
</tr></p>
Access database code(values changed for saftey)
<?php
Accesses database
$con=pg_connect("host=hostname port=portnumbers
dbname=name user=user password=password");
if (!$con)
{
die('Could not connect to database');
}
?>
Database display
//Creates table
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Title</th>
<th>Platform</th>
<th>Description</th>
<th>Price</th>
<th>Buy</th>\n";
while($row = pg_fetch_array($res)){
echo"<tr>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo '<td><input type="checkbox" name="selected[]" value="' . $row['0'] . '" /></td>';
echo"</tr>";
}
echo"</table>";
?>
Well, I think you can as follow because you are trying to improve a shopping car:
//Use the `$_SESSION` var to hold the values
foreach ($_POST['selected'] as $item)
$_SESSION['cart'][$item] = $item;
As the item's id will be indexed, each time a submit is performed:
If the item exists, will be replaced
If the item does not exist, will be added
In order to remove items, you should use a "view cart" page and then show the items:
foreach ($_SESSION['cart'] as $item)
echo '' . $item . '';
I have this code for listing mysql records and putting it into a table according to the address inputted. My problem is, how to do just the same but this time, making use of textboxes to project the contents of the record corresponding to the same data inputted.
I'm just a beginner with no talent. Maybe you could give me some idea on how to do it.
mysql_select_db("hospital", $con);
$result = mysql_query("SELECT * FROM t2 WHERE ADDRESS='{$_POST["address"]}'");
echo "<table border='1'>
<tr>
<th>HospNum</th>
<th>RoomNum</th>
<th>LastName</th>
<th>FirstName</th>
<th>MidName</th>
<th>Address</th>
<th>TelNum</th>
<th>Nurse</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['HOSPNUM'] . "</td>";
echo "<td>" . $row['ROOMNUM'] . "</td>";
echo "<td>" . $row['LASTNAME'] . "</td>";
echo "<td>" . $row['FIRSTNAME'] . "</td>";
echo "<td>" . $row['MIDNAME'] . "</td>";
echo "<td>" . $row['ADDRESS'] . "</td>";
echo "<td>" . $row['TELNUM'] . "</td>";
echo "<td>" . $row['NURSE'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
As a suggestion, I do not think displaying the values inside of a textbox is the best idea. With that being said, you achieve your results by performing the following
while($row = mysql_fetch_array($result)) {
echo "<input type=\"text\" value='" . $row['HOSPNUM'] . "'><br />";
echo "<input type=\"text\" value='" . $row['ROOMNUM'] . "'><br />";
....
}
You would need to escape the " inside of the text boxes by using PHP's escape special character \
You need to search from your present tables and use AJAX to notify the user.
I would suggest you look into a framework in PHP which would help you a LOT since you are just starting your projects. List of frameworks can be found at http://www.phpframeworks.com/
As far as i could understand your question, you want to retrieve data from mysql based on the input of the text boxes. This is how you can go about it:
<form action="yourpage.php">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="text" name="text3">
</form>
Now you can retrieve data from mysql using where clause.
select * from table where text1 = 'text1' and text2 = 'text2' and text3 = 'text3'
Note: You need to change the names in form and query as per your requirement.