PHP/HTML: activate users through admin page using radio button - php

I'm creating an activate/deactivate based on the admin input.
The idea is to allow the admin to view all the accounts from accounts table and choose one from Selections (activate or deactivate ) using radios input.
I really would appreciate it if you guys can help me out on this page, my project is almost done.
Thank you All for your suggestions, the I'm just trying to let the admin have the option to activate or deactivate from the user account, using this code.
The Code basically view all the informations of the accounts, and the admin just checkbox anyone she wants to activate and choose from the radios (activate/deactivate) and based on her input will set the value in the database to 0 which is deactivate and 1 to activate .
thank you
<?php
session_start();
if( isset($_SESSION['username']) ){
//Connect to DB
//include_once("Project/CIEconn.php");
$mysqlCON= mysqli_connect("localhost", "root", "","CIE") or die(mysqli_connect_error());
mysqli_select_db($mysqlCON,'CIE') or die ("no database");
if( isset($_POST['ActivateUsers']) ){
if( empty($_POST['Id']) || $_POST['Id'] == 0 ){
echo"<h4> please choose someone to activate </h4>";
}else{
$impid = implode("' , '" , $_POST['Id']);
$sqlDelete = "UPDATE Accounts SET Activated='$_POST[activate]' WHERE Id IN ('" . $impid . "')";
$DeleteQuery = mysqli_query($mysqlCON,$sqlDelete) or die ("Error : ".mysqli_error($mysqlCON));
}
}
$sqlCommand = "SELECT * FROM Accounts ";
$result = mysqli_query($mysqlCON,$sqlCommand) or die(mysql_error());
echo "<h1> Activate / Deactive User Account </h1>";
echo "<table border='1' width = 80% align=center >
<tr>
<th>Check </th>
<th>SSU ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>User Type </th>
<th>Activated</th>
<th>Activate/Deactive</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<form action='ActivateD.php' method='post'>";
echo "<td> <input type='checkbox' name='Id[]' value='". $row['SSU'] ."' /> </td> " ;
echo "<td align=center > " . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['Email'] . "</td>";
echo "<td align=center>" . $row['First_Name'] . "</td>";
echo "<td align=center>" . $row['Last_Name'] . "</td>";
echo "<td align=center>" . $row['userType'] . "</td>";
echo "<td align=center>" . $row['Activated'] . "</td>";
echo "<td align=center>" .
"
<input type='radio' name='activate' value='1' > Activate
<input type='radio' name='activate' value='0'> Deactive
</form>"
. "</td>";
echo "</tr> ";
}
echo "</table>";
echo " <br>
<form action='ActivateD.php' method='post'>
<div align='center'>
<input type='submit' name='ActivateUsers' value='Activate/Deactive'>
</div>
</form>";
mysqli_close($mysqlCON);
}
else{echo "must logout to see this page..!!";}
?>
<html>
<head><title> Activate / Deactive User Account </title>
<style type="text/css">
body{
background-color: #23438e;
}
table{
background: white;
}
h1{
color: #FF942B;
text-align: center;
padding-top : 50px;
text-decoration: none;
}
</style>
</head>
<body>
</body>
</html>

There appear to be several problems with your script:
You are wrapping a form around each of your radio inputs and checkboxes but a separate form around your submit button. In this setup, the form isn't going to send the right data when submitted.
Duplicating radio boxes with the same name on every row. Because you are only utilizing a single value for active/inactive, you should place one set of radio buttons at the bottom of your form.
Your SQL query escaping is incorrect:
Instead of
$sqlDelete = "UPDATE Accounts SET Activated='$_POST[activate]' WHERE Id IN ('" . $impid . "')";
You should use $sql = "update Accounts set Activated='" . $_POST['activate'] . "' where Id in ('" . $impid . "')";
Lack of error handling and input sanitization

Related

Take output from MySQL array and post specific info to different table

Right now my array displays information that I need, but I am trying to allow users to claim the jobs by clicking the "claim button" and posting their username, user id, order id, and setting the claimed info to show it is claimed.
<h3>Current Jobs</h3>
<p class="text-muted m-b-20">These are the jobs either in progress or are available to be claimed.</p>
<div class="table-responsive">
<? echo "<table class=\"table table-striped\">
<thead>
<tr>
<tr>
<th>Order #</th>
<th>Order Type</th>
<th>Order ID Number</th>
<th>Order Status</th>
<th>Order Dates</th>
<th>Order Claimed</th>
<th>Claim Job</th>
</tr>
</thead>
<tbody>";
$order_id = $row['order_item_id'];
$user_id = $uid;
$user_name = $username;
while($row = mysql_fetch_array($wclaim))
{
echo "<tr id=" . $row['order_item_id'] . ">";
echo "<td >" . $row['order_item_id'] . "</td>";
echo "<td>" . $row['order_item_name'] . "</td>";
echo "<td>" . $row['order_id'] . "</td>";
echo "<td>" . $row['post_status'] . "</td>";
echo "<td>" . $row['post_date'] . "</td>";
echo('<td>'.(($row['claim_aktiv']==1) ? 'Yes' : 'No').'</td>');
---> if(isset($_POST["submit"])) {
"INSERT INTO claimsystem (claim_id, user_id, user_name, claim_aktiv)
VALUES ('$order_id','$user_id','$user_name','1')";
}
---> echo "<td><input type=\"submit\" name=\"submit\"></input></td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
The code I need help with has a arrow in front of it
--->
When I click on the submit button it does not do anything.
Since the only information you need from the user is an identifier for the given row, you can build a simple small form into each row. Structurally it would look something like this:
while($row = mysql_fetch_array($wclaim))
{
// output your table cells
echo "<td><form method=\"post\"><input type=\"hidden\" name=\"order_item_id\" value=\"" . $row['order_item_id'] . "\" /><input type=\"submit\" name=\"submit\"></input></form></td>";
}
Note the things in that table cell:
form - Each row would have its own small form, so you're only posting just that one row's value when clicking the button.
input type="hidden" - The order_item_id value to be submitted.
input type="submit" - the submit button
You can add an action to the form to post to another page, or post back to this same page by default. When handling the post, you'd just use the value being sent from the input type="hidden":
$_POST["order_item_id"]
That would be the identifier you need to insert the record.

How to update data in a table using dropdownlist box in PHP?

Im trying to make an orders table in PHP where i can choose in a dropdownlist box the driver and the truck to each order from the existing sql db.
i managed to create the dropdownlist inside the table but i dont know how to update the db.
im trying using isset function but im probably not doing it right.
here is the code i made + a screenshot:
screen
<!DOCTYPE html>
<html>
<head>
<title>Orders</title>
<link rel="stylesheet" type="text/css" href="table-general.css">
<style type="text/css">
body
{
font-family: Arial, Verdana, sans-serif;
font-size: 90%;
color: #666;
background-color: #f8f8f8;
}
</style>
</head>
<body>
<h1>Orders</h1>
Done</br></br>
<table class="general">
<tr class="head">
<th>Order_ID</th>
<th>Customer_ID</th>
<th>Driver_ID</th>
<th>Truck_ID</th>
<th>Date</th>
<th>Project_Name</th>
<th>Project_Place</th>
<th>Amount</th>
</tr>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("login");
$result = mysql_query("select * from orders_table") or die("Failed".mysql_error());
$result2 = mysql_query("select * from trucks_table") or die("Failed".mysql_error());
if(mysql_num_rows($result2))
{
$select= '<select name="select">';
while($record2=mysql_fetch_array($result2))
{
$select.='<option value="'.$record2['TruckID'].'">'.$record2['TruckID'].'</option>';
}
}
$select.='</select>';
while($record = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $record['Order_ID'] . "</td>";
echo "<td>" . $record['Customer_ID'] . "</td>";
echo "<td>" . $record['Driver_ID'] . "</td>";
echo "<td>" . $select . "</td>";
if(isset($_POST['select']))
{
$t=$_POST['select'];
$sql = mysql_query("update orders_table set TruckID='$t' where Order_ID='".$record['Order_ID']."' ");
}
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Project_Name'] . "</td>";
echo "<td>" . $record['Project_Place'] . "</td>";
echo "<td>" . $record['Amount'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
You probably need to learn something more about HTTP FORMS, how to send data from client to the server with POST or GET and how to use this data on the server with PHP.
see tutorials like these:
http://www.w3schools.com/php/php_forms.asp
In short - to send data from HTML page to the server, you need to wrap the inputs, selects, textareas in
<form action="url"> </form>
where url is pointing to your server php script, where your handling the form data
then you need to include
<input type="Submit">
which will generate a button on which when the user clicks the data are sent back to the server

Updating Page won't work

thanks to you all for this great web-site, it's my second time asking a question, I hope it's not too much ^^
I'm working on my project, and I've got kind of a weird issue with my update page,
it's basically displaying all data from a table in mysql, and when I checkbox the two rows to update them with new informations, it copies all new informations from the "(last row only)" and copy it all over the other rows, and the result is , all row become identical !!!! am I doing something wrong ? please help me guys this is my code..
I ALSO when I try to update any other row it wont update, but only update the last row...
<?php
session_start();
if( isset($_SESSION['username']) ){
include('../CIEcon.php');
echo "<form action= 'adminCleaning.php' method = 'post'>" ;
if(isset($_POST['update'])){
if( isset($_POST['id']) ){
if( empty($_POST['id']) || $_POST['id'] == 0 ){
echo"<h4> please choose something to delete </h4>";
}else{
echo $implid = implode("' , '", $_POST['id']);
$sqlUpdate = "UPDATE Cleaning SET JobName= '$_POST[jobname]',Description= '$_POST[description]',NoStudent='$_POST[nostudent]',DueDate='$_POST[duedate]' WHERE Id IN('" . $implid . "')";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate )or die(mysqli_error($dbCIE));
if (mysqli_affected_rows($dbCIE) > 0) {
echo "You have successfully updated your data.<br><br>";
}
else {
echo "The data you submitted matched the current data so nothing was changed.<br><br>";
}
} // end of else..
} // end of if isset($_POST['id']) ...
} // end of if isset($_POST['update']) ...
$sql = "SELECT * FROM Cleaning ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
/// NOW DISPLAY ALL INFO FROM CHOSEN DATABASE...
echo "
<table cellpadding ='4' border='1' width='80%' align='center'>
<tr>
<th class='tt' >Check </th>
<th class='tt'> Job's Name</th>
<th class='tt' >Description</th>
<th class='tt' > No Students needed</th>
<th class='tt' >Due Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<br>";
echo "<tr>";
echo "<td> <input type='checkbox' name='id[]' value='". $row['Id'] ."' /> </td>"; // array[] cause to edit more than one record...
echo "<td><input type='text' name='jobname' value='" . $row['JobName'] . "'> </td>";
echo "<td><input type='text' name='description' value='" . $row['Description'] . "'> </td>";
echo "<td><input type='text' name='nostudent' value='" . $row['NoStudent'] . "'> </td>";
echo "<td><input type='text' name='duedate' value='" . $row['DueDate'] . "'> </td>";
echo "</tr>";
}
echo "</table>";
/// END THE SEARCH HERE...........
echo " <br>
<div align='center'>
<input type='reset' value='clear' />
<input type='submit' name='update' value='update' />
</div> ";
mysqli_close($dbCIE);
echo "</form>";
}
else{echo "must logout to see this page..!!";}
?>
<html>
<head><title> ..Cleanding.... </title></head>
<style type="text/css">
body{
margin-top: 70px; /*space above the table....*/
background-color: #23438e;
}
table{
background-color: white;
}
.tt{
background: #f26822;
color: white ;
}
</style>
<body>
<!-- <a href= "../AdminIndex.php" > <button> Main Page </button></a> -->
</body>
</html>
The problem is that all your inputs have the same name, and you are using an IN to update, rather than an =
Change your input tags to follow this pattern:
<input type='text' name='jobname[".$row['Id']."]' value='" . $row['JobName'] . "'>
and the SQL to use this pattern:
JobName= '$_POST[jobname][$id]' ... WHERE Id = $id
If more than one id is sent at a time, you'll need a loop to loop through all the inputs and run the query.
Each row of the HTML table should be sent to the server with a unique id, and each update should be sent to the database with the appropriate data and id.

How to echo the results in PHP

I need to be able to display the results of the submitted form with radio buttons. I want the ID to display with either of the results '1' or '2' from the radio button values.
i.e. ID: 13 - Value: 2
if the row id is set within name the radio buttons work fine but I'm not sure how to display the results from it
if the name of the radio button is set to 'ans' it links all radio buttons together, when I want them working per id
Please see my below code to explain a little better - I'm just not sure how to echo this
<?php
$sql = "SELECT * FROM 'tz_todo' ORDER BY 'position' ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border='1' width='100%' bordercolor='#000000' style='border-collapse: collapse'><tr class='t1' bgcolor='#00204F'><th>
<font color='#FFFFFF'>ID</font></th><th><font color='#FFFFFF'>CHECK</font></th>
<th width='50'>
<font color='#FFFFFF'>OK</font></th><th width='50'><font color='#FFFFFF'>FAIL</font></th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>
<p align='center'>" . $row["id"] . "</td><td>
<p align='center'>" . $row["text"] . "</td><td align='center' width='50'><input type='radio' name='" . $row["id"] . "' value='1'><br></td>
<td align='center' width='50'><input type='radio' name='" . $row["id"] . "' value='2'><br></td></tr>";
}
echo "</table><input type='submit' value='Submit' name='submit' class='buttons'> ";
} else {
echo "0 results";
}
$conn->close();
?>
</form>
<?php
if (isset($_POST['submit'])) {
echo("????????????????????????????????????");
}
?>
I've now amended it slightly and have it generating results but now the IDs aren't linked to the two radio buttons (1 and 2)
<?php
$sql = "SELECT * FROM `tz_todo` ORDER BY `position` ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border='1' width='100%' bordercolor='#000000' style='border-collapse: collapse'><tr class='t1' bgcolor='#00204F'><th>
<font color='#FFFFFF'>ID</font></th><th><font color='#FFFFFF'>CHECK</font></th>
<th width='50'>
<font color='#FFFFFF'>OK</font></th><th width='50'><font color='#FFFFFF'>FAIL</font></th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
$rowid = $row["id"];
echo "<tr><td>
<p align='center'>" . $row["id"] . "</td><td>
<p align='center'>" . $row["text"] . "</td><td align='center' width='50'><input type='radio' name='ans' value='1'><br></td>
<td align='center' width='50'><input type='radio' name='ans' value='2'><br></td></tr>";
}
echo "</table><input type='submit' value='Submit' name='submit' class='buttons'> ";
} else {
echo "0 results";
}
$conn->close();
?>
</form>
<?php
if (isset($_POST['ans'])) {
$answer = $_POST['ans'];
if ($answer == "1") {
echo 'OK';
} elseif ($answer == "2") {
echo 'FAIL';
}
}
?>
Please let me know if you would like me to explain a little better or differently
Thanks,
Tom
I think I understand your problem. You should name the radio button this way for example
name='ans" . $row["id"] . "'
This way, radio buttons will work together for each row.
Instead of using the name field within the radio button, I've merged it all into the value field:
<input type='radio' name='radio' value='".$rowid." - 1'>
but then I have the problem that you can only select 1 radio button for all rows

MySQL UPDATE statement is not updating database data through web form

Scenario: I have a web form where the user can manually enter data (see below). The user will submit the form and the data will be automatically added to the database.
date, order_ref, first_name, last_name, postcode, country, quantity, scott_packing, packing_price, courier_price, dispatch_type, job_status
On another page, the user will be able to only view all the jobs that are currently being (this data is taken from the database) processed and add the tracking number and edit the packing_price,courier_price and job_status and submit the new data.
http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-04-23at104045_zps2a628d50.png
Issue: When the user clicks the 'submit all' button, the user is supposed to be redirected to the thank you page which simply notifies the user that their entry has been successful however at the moment, the user is only directed to a blank page which contains the navigation menu. I have checked the database to see if the data has been updated but nothing has changed. How do i get my update statement to work so that the user can update the existing jobs?
This is the code for the page that displays all the jobs:
<?
session_start();
if(!session_is_registered(myusername))
{
header("location:../index.php");
}
include("../template/header.php");
include("../controllers/cn.php");
$sql = "SELECT * FROM Jobs";
$qry = mysql_query($sql);
echo "<div class='content'>";
echo "<form class='form_edit' method='post' action='updatejob.php'>";
echo "<table id='job_list' cellpadding='0' cellspacing='0'>
<tr>
<th>Job No</th>
<th>Date</th>
<th>Qty</th>
<th>Postcode</th>
<th>Country</th>
<th>Packed by Scott</th>
<th>Packing Price</th>
<th>Courier Price</th>
<th>Tracking No</th>
<th>Dispatch Type</th>
<th>Job Status</th>
</tr>";
while($row = mysql_fetch_array($qry))
{
echo "<tr>";
echo "<td width='80' style='text-align: center;'>" . $row['order_ref'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['date'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['quantity'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['postcode'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['country'] . "</td>";
echo "<td width='100' style='text-align: center;'><input type='CHECKBOX' id='scott_packing' name='scott_packing' value='". $row['scott_packing'] . "'></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='packing_price' name='packing_price' value='". $row['packing_price'] . "'/></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='courier_price' name='courier_price' value='". $row['courier_price']."'/></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='tracking_number' name='tracking_number' value='". $row['tracking_number'] . "'/></td>";
echo "<td width='100' style='text-align: center;'>" . $row['dispatch_type'] . "</td>";
echo "<td width='100' style='text-align: center;'><select name='job_status' id='job_status'>
<option value='". $row['job_status'] ."'>". $row['job_status']. " <option value='dispatched'>Dispatched</td>";
//echo "<td width='100' style='text-align: center;'><a href='editjob.php'>edit</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='submit all'/>";
echo "</form>";
mysql_close();
?>
And here is the code that is supposed to update the data:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:../index.php");
}
include("../template/header.php");
include("../controllers/cn.php");
if (isset($_POST['submit']))
{
$order_ref = $_POST['order_ref'];
$packing_price = $_POST['packing_price'];
$courier_price = $_POST['courier_price'];
$tracking_number = $_POST['tracking_number'];
$job_status = $_POST['job_status'];
$sql_qry = "UPDATE Jobs SET '$packing_price, $courier_price, $tracking_number, $job_status' WHERE order_ref = '$order_ref'";
$query = mysql_query($sql_query);
if(!$query)
{
die('Could not update data' .mysql_error());
} else
{
header("location: updatesuccess.php");
exit;
}
mysql_close();
}
?>
Your update query is wrong. The query of update should be like
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
In this case it will be something like
UPDATE Jobs SET packing_price='$packing_price',courier_price='$courier_price',tracking_number='$tracking_number',job_status='job_status' WHERE order_ref = '$order_ref'";
and also you missed out the name property for input fields. If you don't specify it you can't access it like $_POST['packing_price'] where packing_price is be the name of the input field.
Also add method="post" to the form like
echo "<form class='form_edit' action='updatejob.php' method='post'>";
Try with this query
$sql_qry = "UPDATE Jobs SET column1 = '$packing_price', column2 ='$courier_price', .... WHERE order_ref = '$order_ref'";
And like Roland Jansen stated you are missing the name attributes on your input tags

Categories