I am trying to update the rank column in the users table in my database by presenting data in a PHP form and using a button to submit. However once i edit the data in my PHP form and press submit, the data in the database remains unchanged. I'm adding a (link to the) picture of the webpage, and the code is posted below.
Webpage image
<!DOCTYPE HTML>
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
Displays all data from 'users' table
*/
// connect to the database
include('../db/connect.php');
// get results from database
$result = $MySQLi_CON->query("SELECT * FROM users")
or die(mysql_error());
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Username</th> <th>Email</th> <th>Rank</th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = $result->fetch_array()) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_id'] . '</td>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td><input type="hidden" name="user_id[]" id="newrank" width="20px" min="0" max="100" value="' . $row['user_id'] . '"></td>';
echo '<td><form method="POST" action=""><input type="number" name="newrank[]" id="newrank" width="20px" min="0" max="100" value="' . $row['rank'] . '"></form></td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
if(isset($_POST['btn-update'])) {
for($i = 0; count($_POST["user_id"]); $i++) {
$_POST['newrank'][$i] = $MySQLi_CON->real_escape_string($_POST['newrank'][$i]); # If this function exists either, if not comment or remove this line!
$_POST['user_id'][$i] = $MySQLi_CON->real_escape_string($_POST['user_id'][$i]); # If this function exists either, if not comment or remove this line!
$MySQLi_CON->query('UPDATE users SET rank=' . $_POST['newrank'][$i] . ' WHERE user_id=' . $row['user_id'][$i] . '');
}
echo "Updated the rows.";
}
?>
<br>
<button type="submit" class="btn btn-default" name="btn-update" id="btn-update">Update</button></a>
<p>Add a new record</p>
</body>
</html>
Seems there is an error in your query statement
Modify this : if ($$MySQLi_CON->query($sql) === TRUE) {
with if ($MySQLi_CON->query($sql) === TRUE) {
You need to parse the id you wish to modify to the $_POST. Also, you need to use <form action="" method="POST"> in your code.
Didn't tested it, but the following should work:
<!DOCTYPE HTML>
<html>
<head>
<title>View Records</title>
</head>
<body>
<table border='1' cellpadding='10'>
<thead> <th>ID</th> <th>Username</th> <th>Email</th> <th>Rank</th> <th colspan="3"></th></thead>
<tbody>
<?php
//Displays all data from 'users' table
// connect to the database
include('../db/connect.php');
// get results from database
$result = $MySQLi_CON->query("SELECT * FROM users")
or die(mysql_error());
// loop through results of database query, displaying them in the table
while($row = $result->fetch_assoc()) {
// echo out the contents of each row into a table
?>
<form action="" method="POST">
<tr>
<td><?php echo $row['user_id']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email'];?></td>
<td><input type="number" name="newrank" id="newrank" value="<?php echo $row['rank']; ?>"></td>
<td><input type="hidden" name="id" value="<?php echo $row['user_id']; ?>"><button type="submit" class="btn btn-default" name="btn-update" id="btn-update">Update</button></td>
<td>Delete</td>
</tr>
</form>
<?php
}
?>
</tbody>
</table>
<?php
if(isset($_POST['btn-update']))
{
$sql = 'UPDATE users SET rank=' . $_POST['newrank'] . ' WHERE user_id=' . $_GET['id'] . '';
if ($MySQLi_CON->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $MySQLi_CON->error;
}
}
?>
<p>Add a new record</p>
</body>
</html>
Related
I am making an application to secure doors and windows in your house. The app is done for 80% but i can't figure out how I can delete a database record of a door of house. The problem is I can't find a way to read out the right data to delete the record. Could you please look in my scripts i made for the delete page?
I have one page to insert al the data in a table to overview all the devices that are "connected" to the app.
<section>
<form action="verwijderen.php" method="post">
<?php
$sql = "SELECT device_id, naam, plaats.plaats, type.type, status FROM devices, type, plaats, status
WHERE devices.type_id = type.type_id
AND plaats.plaats_id = devices.plaats_id
AND status.status_id = devices.status_id";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
echo '<table><tr><td>ID</td><td>Naam</td><td>Plaats</td><td>Type</td><td>Status</td><td>Verwijderen</td></tr>';
while($row = $result->fetch_assoc())
{
echo '<tr>';
echo '<td>' .$row['device_id']. '</td>';
echo '<td>' .$row['naam']. '</td>';
echo '<td>' .$row['plaats']. '</td>';
echo '<td>' .$row['type']. '</td>';
echo '<td>' .$row['status']. '</td>';
echo '<td>';
?>
<input type="submit" value="Verwijderen" name="submit">
</form>
<?php '</td>';
}
echo '</table>';
}
?>
</section>
The other page I have is the page to delete the records that are inserted into the table. The problem with the deleting is I can't find a way to delete the records. Adding the records to the database in html with a $_POST/$_GET works fine..
<?php
error_reporting(E_ERROR);
include '../dbconnect.php';
//$connection = mysql_connect("", "", ""); // Establishing Connection with Server
//$db = mysql_select_db("cerar", $connection); // Selecting Database from Server
if(isset($_POST['submit']))
{ // Fetching variables of the form which travels in URL
}
mysqli_close($con); // Closing Connection with Server
//header( "refresh:3;url=index.php" );
?>
Create a form for each row of your table, and include a hidden input field containing the key value to pass to the server. So move the opening form tag just before the input type="submit" you have, and include another hidden input, like this:
<form action="verwijderen.php" method="post">
<input type="hidden" value="<?=$row['device_id']?>" name="device_id">
<input type="submit" value="Verwijderen" name="submit">
</form>
Then in PHP you can find the device id that must be deleted in $_POST['device_id']. It should be easy to write the delete statement.
<section>
<form action="verwijderen.php" method="post">
<?php
$sql = "SELECT device_id, naam, plaats.plaats, type.type, status FROM devices, type, plaats, status WHERE devices.type_id = type.type_id AND plaats.plaats_id = devices.plaats_id AND status.status_id = devices.status_id";
$result = $con->query($sql);
if ($result->num_rows > 0) {
echo '<table>
<thead>
<tr>
<th>ID</th>
<th>Naam</th>
<th>Plaats</th>
<th>Type</th>
<th>Status</th>
<th>Verwijderen</th>
</tr>
</thead>
<tbody>';
while($row = $result->fetch_assoc()){
echo '<tr>';
echo '<td>' .$row['device_id']. '</td>';
echo '<td>' .$row['naam']. '</td>';
echo '<td>' .$row['plaats']. '</td>';
echo '<td>' .$row['type']. '</td>';
echo '<td>' .$row['status']. '</td>';
echo '<td>';?>
<input type="hidden" value="<?php echo $row['device_id']; ?>" name="device_id">
<input type="submit" value="Verwijderen" name="submit">
<?php '</td>';
}
echo '</tbody>
</table>';
}
?>
</form>
</section>
PHP Code
<?php
error_reporting(E_ERROR);
include '../dbconnect.php';
extract($_POST);
//$connection = mysql_connect("", "", ""); // Establishing Connection with Server
//$db = mysql_select_db("cerar", $connection); // Selecting Database from Server
if(isset($_POST['submit']) && $_POST['submit'] == 'Verwijderen')
{
//delete query
//DELETE FROM devices WHERE device_id = $device_id
}
mysqli_close($con); // Closing Connection with Server
//header( "refresh:3;url=index.php" );
?>
This is a table of students' ID numbers, with a button after each ID.
When I click on a button, I want it to open a new page called "score.php", and display the selected ID.
But the code doesn't work. It only show the text "ID", but not the number.
Here is "index.php"
<html>
<head>test</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3_2556")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . '<input type="hidden" name="student_id" value=" ' . $_POST['student_id'] . ' " /> <button type="submit" name="btn_student_id" >Select</button> </td> ';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>
And here is "score.php"
<head>test</head>
<body>
<?php
$student_id = $_POST["student_id"];
echo '<p> ID: '.$student_id.'</p>';
?>
</body>
Since you are using a <button>, there is no need to use a <input type="hidden">. Just add the student_id as the button value -
<button type="submit" name="btn_student_id" value=" ' . $row['student_id'] . ' " >Select</button>
Then in you php just get the value from the clicked on button -
<?php
$student_id = $_POST["btn_student_id"];
echo '<p> ID: '.$student_id.'</p>';
?>
your index.php file will be:
<html>
<head>test</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3_2556")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . '<input type="hidden" name="student_id" value=" ' . $row['student_id'] . ' " /> <button type="submit" name="btn_student_id" >Select</button> </td> ';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>
Sir,
I have a form which contains 3 text fields and a submit button.(basically a search form) I made a query set through which i dynamically display a table on the same page. So inputting a specific data in any of the text field and hitting the submit button the table shows the relevant result for that query.
Now when the page loads it shows the empty table also. I want when user enters some txt inputs and hits submit button then only the table below should shows up. Please help me. I am totally new to coding. I uses dreamweaver.
Try this:
<?php
if(isset(POST['submit']))
{
?>
Set table structure here ....
<?php
}
?>
-
Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<title>Test</title>
<head>
</head>
<body>
<form action="" name="myForm" enctype="multipart/form-data" method="post">
<table width="100%" align="center">
<tr><td>First Name</td><TD><input type="text" name="fname"></TD></tr>
<tr><td></td><TD><input type="text" name="mname"></TD></tr>
<tr><td>Last Name</td><TD><input type="text" name="lname"> </TD></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit" onmouseover="this.style.cursor=\'pointer\';" OnMouseOut="this.style.cursor=\'default\';">
</td></tr></table></form>
<?PHP
if(isset(POST['submit']))
{
$con = mysql_connect("$host","$username","$pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name", $con); //database name
mysql_query("insert into db_table set first_name='".$POST['fname']."',middle_name='".$POST['mname']."',last_name='".$POST['lname']."'");
$result = mysql_query("SELECT * FROM $db_table"); //table
echo "<table cellpadding='0' cellspacing='0'>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['middle_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>
</body>
</html>
Since you have not provide any code, I guess you can do something like this :-
If your search result return null then don't show the table :-
if(mysqli_num_rows($result) <= 0)
{
//don't show your table
}
else
{
// result table
}
THIS IS MY CODE HERE I AM GETTING PROBLEM THIS IS A INPUT TYPE TAG AND NOT SELECTED DATA FROM DATABASE USING PHP.
here is first part of this code is html formatted ad second part is php
i want select data from database to data slect to same page as we see on the sopping cart sites
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpSelect</title>
</head>
<body>
Insert Age for search
<form action="#" method="post" >
<input type="text" id="val" name="resValue" />
<input type="submit" value="submit" /></form>
<?php
if(isset($_POST['submit']))
{
$res=$_POST['resValue'];
echo $res;
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons where Age=25");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Try changing the SQL to this
SELECT * FROM Persons where Age='".mysql_escape_string($formValue['val'])."'
First issue I found on your code is here:
<input type="submit" value="submit" />
it should be:
<input type="submit" value="submit" name="submit" />
To be able to get the results. Below are the codes:
<?php
$query = "";
if(isset($_POST['submit']))
{
$res=$_POST['resValue'];
$query = " where Age='$res'"
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons $query");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Change your select query to this one.
SELECT * FROM Persons where Age='".mysql_escape_string($_POST['val'])."'
In your problem the all value of form get by the name of all fields of form.\
so here should be
<input type="submit" name="submit" value="submit"/>
Because in $_POST['submit'] submit is same as button's name.
$result = mysqli_query($con,"SELECT * FROM Persons where Age="25");
I'm trying to create insert, update and delete relate to shopping, all of them are working fine except update page.
Please see sample code of list of product from index.php page ...
<?php
$sql = "SELECT product_id, product_name, product_category, product_retail_price, product_price, product_detail FROM product";
$result_db = $db->query($sql) or die('Error perform query!');
?>
<table border="1">
<tr>
<th>product name</th>
<th>product category</th>
<th>product retail price</th>
<th>price</th>
<th>detail</th>
<th>Update</th>
<th>Delete</th>
<th>Insert</th>
</tr>
<?php
while ($r = $result_db->fetch_object()) {
$update = "update_form.php?product_id={$r->product_id}&
product_name={$r->product_name}&
product_category=$r->product_category&
product_retail_price={$r->product_retail_price}&
product_price={$r->product_price}&
product_detail={$r->product_detail}";
$delete = "delete.php?product_id={$r->product_id}";
$insert = "insert.php";
echo '<tr>';
echo '<td>' . $r->product_id . '</td>';
echo '<td>' . $r->product_name . '</td>';
echo '<td>' . $r->product_category . '</td>';
echo '<td>' . $r->product_retail_price . '</td>';
echo '<td>' . $r->product_price . '</td>';
echo '<td>' . $r->product_detail . '</td>';
echo "<td><a href='{$update}'>Update</a></td>";
echo "<td><a href='{$delete}'>Delete</a></td>";
echo "<td><a href='{$insert}'>Insert</a></td>";
echo '</tr>';
}
$db->close();
?>
</table>
</body>
As you can see above code where it said
while ($r = $result_db->fetch_object()) {
$update = ......
This sending the data of relate to product using "product_id" sending sending to the updateform.php page ... that updateform.php page is the code showing
<body>
<form action="update.php" method="post">
<input type="hidden" value="<?= $_GET['product_id'] ?>" name="product_id"/>
product name: <input type="text" name="product_name" value="<?= $_GET['product_name'] ?>">
product category: <input type="text" name="product_category" value="<?= $_GET['product_category'] ?>">
product retail price: <input type="text" name="product_retail_price" value="<?= $_GET['product_retail_price'] ?>">
product price: <input type="text" name="product_price" value="<?= $_GET['product_price'] ?>">
product detail: <input type="text" name="product_detail" value="<?= $_GET['product_detail'] ?>">
<input type="submit" name="submit">
</form>
</body>
</html>
When I run the code, the updateform.php showing the text field with <?= $_GET['xxxxxx'] ?>"
Why am I getting this result?
Is it this code?
$update = "update_form.php?product_id={$r->product_id}&
product_name={$r->product_name}&
product_category=$r->product_category&
product_retail_price={$r->product_retail_price}&
product_price={$r->product_price}&
product_detail={$r->product_detail}";
Is it the right code to get right information from database using "product_id" number? Is there better code to write than this?
Try using :
<?php echo $_GET['xxxxxx']; ?>
and if it works, it means that in your php configuration the use of short opening tags is restricted. meaning no