Update retrieved data - php

Hi there im in the process of making a page to approve and deny names in a database, I currently have it so it finds an pulls the pending requests im looking for now i wanna know how to update the row once i click approve or deny.
the values it needs to update is the NameState column to either ("approved") or ("REJECTED")
there can be any where from 1 to 100 names at a time.
Thanks Ryan
$DB_HOST = "IP";
$DB_USER = "user";
$DB_PASS = "Pass";
$DB_DTBS = "DB";
mysql_connect($DB_HOST, $DB_USER, $DB_PASS) or die(mysql_error()); // Connect to database server(localhost) with username and password.
mysql_select_db($DB_DTBS) or die(mysql_error()); // Select registration database.
$search = mysql_query('SELECT * FROM TABLE WHERE `NameState` = \'(\"PENDING")\'') or die(mysql_error());
$match = mysql_num_rows($search);
if($match > 0){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Current Name</th>
<th>Requested Name</th>
<th>Approve or Deny</th>
</tr>";
while($row = mysql_fetch_array($search)) {
echo "<tr>";
echo "<td>" . $row['object_id'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['WishName'] . "</td>";
echo '<td> <form method="post" action=<?php echo Approve:<input type="radio" value="Approve" name="name"> Deny:<input type="radio" value="Deny" name="name"><br /> </td>';
echo "</tr>";
}
echo "</table>";
echo '<input type="Submit" value="Submit" name="Submit"> </form>';
} else {
echo("No Names to Approve");
}
?>

Related

How to delete a row in php?

I made from in php. When I select a class, for example I select the class9, it shows me the table of class9. I also have inserted a row in the table containing the delete button to delete the corresponding row. Now I want to delete a row buy clicking the button which is in the row. how can I do that?
First I choose a class from this option as in the image below ;The image from which we select the class
And then the corresponding table is going to be shown.
This is the image. When I click on the button, the corresponding row should be deleted.
<?php
include "connection.php";
?>
<!doctype html>
<html>
<head>
<title>KDR</title>
</head>
<body>
<table border="2px" width="50%" height="auto">
<tr>
<th>No</th>
<th>Name</th>
<th>F/Name</th>
<th>Age</th>
<th>Delete</th>
</tr>
<?php
$table = $_POST['formCountry'];
$sql = "SELECT * FROM $table";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td><form method='POST' ><input type='submit' name='deletestudent' value='Delete'/></form></td>";
echo "</tr>";
}
?>
</table>
</body>
Something like this. Maybe will need to adjust it a bit.
Your table button:
echo "<td><form method='POST' action="delete_row.php" ><input type='submit' name='deletestudent' value="'.$row['id'].'"/></form></td>";
In PHP (delete_row.php) you should do the following
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['id']) and is_numeric($_POST['id']))
{
$delete = $_POST['id']
$stmt = $conn->prepare("DELETE FROM YOURTABLENAME WHERE id = ?");
$stmt->bind_param('i', $delete);
$stmt->execute();
$stmt->close();
}
Note: Not tested and I'm using mysqli_* here.
Update: As #icecub suggested you can use also hidden field to get the ID
echo "<td>
<form method='POST' action='delete_row.php' >
<input type='hidden' name='deletestudent' value='".$row['id']."'/>
<input type='submit' value='Delete'/>
</form>
</td>";
This is how not to do , see the comments below , important !
Should be send the current id of the element , so when you click the button
get the element ID by $_GET['id'];
<td><a href='?id=".$row['id']."'>delete</a></td>
if (isset($_GET['id'])) {
//Throw query DELETE ... WHERE id = $_GET['id'];
}

How to Update a Data in MySQL using Radio Button?

Hello Everyone Good Afternoon
Can I ask a question? but before that here is my code.
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con=mysqli_connect("localhost","root","","election2016");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>Candidate Name</th>
<th>Position</th>
<th>Vote</th>
<th>Number of Votes</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td><input type='radio' name='candidateid'/>";
echo "<td>" . $row['NumberofVotes'] . "</td>";
}
echo "</table>";
mysqli_close($con);
?>
<br>
<br>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<input name = "update" type = "submit" id = "update" value = "Update">
</form>
</center>
</font>
</html>
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$candidateid = $row['candidateid'];
$sql = "UPDATE candidate_info SET numberofvotes = '1' WHERE candidateid = $candidateid" ;
mysql_select_db('election2016');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>
The Output of my Code Here is it will show list of Candidates,Position,Radio Button Number of Votes with a button save.
My error here is that when i select a radio button and click the button update i want to put 1 in numberofvotes field but its not updating. Whats wrong with my code?
Any help would be appreciated.
TY so much
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con=mysqli_connect("localhost","root","","election2016");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>Candidate Name</th>
<th>Position</th>
<th>Vote</th>
<th>Number of Votes</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td><input type='radio' name='candidateid' value='".$row['candidateid']."' >";
echo "<td>" . $row['NumberofVotes'] . "</td>";
$candidateid=$row['candidateid'];
}
echo "</table>";
mysqli_close($con);
?>
<br>
<br>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<input type="hidden" name="candidateid" value="<?php echo $candidateid;?>">
<input name = "update" type = "submit" id = "update" value = "update">
</form>
</center>
</font>
</html>
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$candidateid = $_POST['candidateid'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$candidateid = $_POST['candidateid'];
$sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'" ;
mysql_select_db('election2016');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>

php mysql UPDATE command wont update

All the data is loaded into a table, each row as a button. Once the button has been pressed, the row has to be updated. But when I click on the button, nothing happens. The text box on the rows return to its original value... It is really starting to cheese me off
<!DOCTYPE html>
<head>
<title>Edit Students</title>
</head>
<?php
$user = 'root'; //Database username ("Root for xampp")
$pass = ''; //Database password ("empty for exampp")
$db = 'dragondrivingschooldb'; //Name of database
$con = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); //Create new data connection ('name of host/server', user, password, database name)
if (isset($_POST['btnUpdate'])) { //Once Update button pressed perform this code
$updatequery = mysqli_query($con, "UPDATE booking SET FirstName='$_POST[txtfirstname]' WHERE BookingID='$_POST[txtid]' "); //excute UpDate Query
};
$sql = mysqli_query($con, "SELECT * FROM booking"); //Select All from Booking
//Create Headers for table
echo "<table border='1'>
<tr>
<th></th>
<th>Booking ID</th>
<th>First Name</th>
</tr>";
//Show Edit Form///////////////////////////////////////////////////////////////////////////////////////////////////
while($row = mysqli_fetch_array($sql)) { //Run sql code till there are no more rows to import
echo "<form action=EditStudent.php method=post>"; //Run update code at top of this page
//Populate table with query (sql)
echo "<tr>";
echo "<td> <input name=update type=submit value=update /> </td>"; //once press update row this button is apart of
echo "<td> <input type=text value=" . $row['BookingID'] . " name=txtid /> </td>";
echo "<td> <input type=text value=" . $row['FirstName'] . " name=txtfirstname /> </td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
mysqli_close($con); //Close connection
?>
</html>
I think that BookingID it is an integer, so your update line need to be:
$updatequery = mysqli_query($con, "UPDATE booking SET FirstName='" . $_POST['txtfirstname'] . "' WHERE BookingID=" . $_POST['txtid'] . ""); //excute UpDate Query
EDIT:
I tested your script and the problem was that you closed the form outside the while loop. Now its working
<!DOCTYPE html>
<head>
<title>Edit Students</title>
</head>
<?php
$user = 'root'; //Database username ("Root for xampp")
$pass = ''; //Database password ("empty for exampp")
$db = 'all_tests'; //Name of database
$con = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); //Create new data connection ('name of host/server', user, password, database name)
if (isset($_POST['btnUpdate'])) { //Once Update button pressed perform this code
$updatequery = mysqli_query($con, "UPDATE test_1 SET FirstName='" . $_POST['txtfirstname'] . "' WHERE BookingID='" . $_POST['txtid'] . "'"); //excute UpDate Query
};
$sql = mysqli_query($con, "SELECT *FROM test_1"); //Select All from Booking
//Create Headers for table
echo "<table border='1'>
<tr>
<th></th>
<th>Booking ID</th>
<th>First Name</th>
</tr>";
//Show Edit Form///////////////////////////////////////////////////////////////////////////////////////////////////
while($row = mysqli_fetch_array($sql)) { //Run sql code till there are no more rows to import
echo "<form method=post>"; //Run update code at top of this page
//Populate table with query (sql)
echo "<tr>";
echo "<td> <input name='btnUpdate' type='submit' value='update' /> </td>"; //once press update row this button is apart of
echo "<td> <input type='text' value=" . $row['BookingID'] . " name='txtid' /> </td>";
echo "<td> <input type='text' value=" . $row['FirstName'] . " name='txtfirstname' /> </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysqli_close($con); //Close connection
?>
at first check that $_POST[txtid] has value or not. and be sure that field name is correct or incorrect.
There are no input field in your html that has name btnUpdate. What I can see its name is update. Therefore your row:
if (isset($_POST['btnUpdate'])) {
would never be true

how to search for each line of texarea data in mysql and display the result in html table?

hi i am taking textarea data which are imageurl links one below each other and trying to search for those image urls in mysql db and if the imageurl found in db then display the full row information in html table.
I tried the following code but i get empty html table! i dont get any mysql error either! i am not sure if i am separating each line of textarea data correctly or not!could any one tell me what i am doing wrong?
form:
<form method="post" action="./doit.php">
<textarea id="links" name="links" rows="20" cols="100">
example text
</textarea>
<br>
<input type="submit" name="mysubmit" value="find" />
</form>
</html>
phpcode:
$textarea_value = $_POST['links'];
$lines = explode("\n", $textarea_value);
$server = "localhost"; // MySQL hostname
$username = "xxxxx"; // MySQL username
$password = "xxxxx"; // MySQL password
$dbname = "xxxxx"; // MySQL db name
$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
echo "<table border='1'>
<tr>
<th>user name</th>
<th>page URL</th>
</tr>";
foreach ($lines as $line) {
$result = mysql_query("SELECT ID,username,imageUrl,imagePageURL FROM mydb WHERE imageUrl = '$line'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['imagePageURL'] . "</td>";
echo "</tr>";
}
};
echo "</table>";
?>

create a Compare page using checkbox and data from mysql

Im a newbie in PHP and I want to create a simple webpage app for my website, I was able to produce this page base on a tutorial here.
<?php
$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*****", $con);
$result = mysql_query("SELECT * FROM products");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>classification</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['classification'] . "</td>";
echo "<td><input type='checkbox' name='{number[]}' value='{$row['prodID']}' /></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<?php
?>
<html>
<head>
</head>
<form name="form1" method="post" action="result_page.php">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
<body>
</body>
</html>
but my problem is how to create a result_page.php to show the selected entries or data base on the selected checkbox so i can create a comparison page. I have this as being my result_page.php but nothing is showing up. I know Im doing something wrong but I cant find out.
<?php
error_reporting(E_ALL);
$host = 'localhost';
$user = '******';
$pass = '******';
$dbname = '******';
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
$sql = "SELECT * FROM products WHERE prodID IN (";
foreach ($_POST['number'] as $product) $sql .= "'" . $product . "',";
$sql = substr($sql,0,-1) . ")";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result))
{
echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</td></tr>\n";
do {
printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["1"], $myrow["2"], $myrow["3"]);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
?>
A quick glance, the section that generates the output is not correct. You have looped two times for no apperant reason.
while ($myrow = mysql_fetch_array($result)) //<========remove this line
{ //<========remove this line
echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</td></tr>\n";
do {
printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["1"], $myrow["2"], $myrow["3"]);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
} //<========remove this line
This is done by human parse, but should serves as a starting point.
And to recap tadman, no this is not a good tutorial. And normally you won't need to do printf for the output.

Categories