Why is only one table row updated? - php

I'm trying to update the rank column in the users table in MySQL using PHP, but when I try to change the values and press the update button, only the last one of the table rows is actually being updated. Here is an image of what the PHP table looks like on the webpage:
Here is the code:
<?php
include '../db/connect.php';
$con = $MySQLi_CON;
if (!$con){
die("Can not connect: " . mysql_error());
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE users SET rank='$_POST[rank]' WHERE user_id='$_POST[hidden]'";
$con->query($UpdateQuery);
}
$result = $MySQLi_CON->query("SELECT * FROM users")
or die(mysql_error());
echo "<table border=1>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Rank</th>
</tr>";
echo "<form action='test3.php' method='post'";
while($record = $result->fetch_array()){
echo '<tr>';
echo '<td>' . $record['user_id'] . '</td>';
echo '<td>' . $record['username'] . '</td>';
echo '<td>' . $record['email'] . '</td>';
echo '<td>' . '<input type="number" name="rank' . [$record['user_id']] . '" />';
echo '<td>' . '<input type="hidden" name="hidden" value="' . $record['user_id'] . '"</td>';
echo '<td>' . '<input type="submit" name="update" value="update"' . '</td></tr>';
}
echo "</table>";
for($_POST['rank'] as $user_id=>$rank){
$UpdateQuery = "UPDATE users SET rank='$rank' WHERE user_id='$user_id'";
$con->query($UpdateQuery);
}
$con->close();

The problem is all rank of each row is using the same name rank and also the hidden, the browser will override all rank and hidden value with the latest element.
change <input type="number" name="rank" /> to <input type="number" name="rank[$record['user_id']]" />
receive in php with $_POST['rank'] in array format and loop each rank.
for($_POST['rank'] as $user_id=>$rank){
$UpdateQuery = "UPDATE users SET rank='$rank' WHERE user_id='$user_id'";
$con->query($UpdateQuery);
}

Related

Get request doesnt work

I have a form that queries my server for data using MySQL. I am using a form that sends get requests. It doesn't show anything and I don't know why. I is so strange because my query is valid and I tested it on PHPmyadmin..I am not striving for answers only, I want to know why this happened and what is the reason behind it.
Here is my code:
<form name="get" action="Roster.php" method="get">
<select name="course" id="course">
<?php
$get = mysqli_query($con, "SELECT teaching.Course_ID FROM `teaching` WHERE teaching.F_ID=213000000 ");
while ($row = mysqli_fetch_assoc($get)) {
echo '<option value ="' . $row["Course_ID"] . '"> ' . $row["Course_ID"] . ' </option>';
}
?>
</select>
<select name="group">
<?php
$get = mysqli_query($con, "SELECT `Group_ID` FROM `teaching` WHERE `teaching`.F_ID= 213000000");
while ($row = mysqli_fetch_array($get)) {
echo '<option value ="' . $row["Group_ID"] . '"> ' . $row["Group_ID"] . ' </option>';
}
?>
</select>
<date-util format="yyyy-mm-dd">
<label for="Date" > Date </label><input id="meeting" name="date" type="date" />
</date-util>
<input type="submit" name="Send" value="Get"/>
</form>
<?php
if ($_GET['submit']) {
$sql = " SELECT enrollment.S_ID,student.ID,student.F_Name,student.L_name,attendance.Status,attendance.Date
From enrollment
INNER JOIN student On enrollment.S_ID
INNER JOIN attendance On enrollment.S_ID
where enrollment.Course_ID =" . $_GET["course"] . "and enrollment.Group_ID =" . $_GET["group"] . "and attendance.date =" . $_GET["date"] . " ";
$result = mysqli_query($con, $sql);
$message = "Please Choose Course_ID and Group_ID ";
if ($result > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "Hello";
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['F_Name'] . " " . $row['L.name'] . '</td>';
echo '<td>' . $row['Date'] . '</td>';
echo '<td>' . $row['Status'] . '</td>';
echo "</tr>";
}
} else {
echo "<tr>";
echo '<td>' . $message . '</td>';
}
}
?>
$_GET['submit'] does not exist. You need to add submit as your name attribute to your button like so
<input type="submit" name="submit" value="Get"/>
Also you should use prepared statements to prevent SQL injection attacks.

Why is this variable not being posted and giving an error?

Why is bakeryid variable not being posted in my form? The error I am receiving is
"Notice:Undefined variable bakeryid"
I have two pages, one displays the form and the second is the action for the form. The second form kept saying that it was undefined also. The bakeryid is the ID for each cakes order.
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes");
$bakeryid = $_POST['bakeryid'];
?>
<table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['order'] . "</td>";
echo '<td><input type="hidden" name="bakeryid" value="' . $bakeryid . '"/></td>';
echo '<td><input type="hidden" name="memid" value="' . $memid . '"/><input type="submit" name="takeorder" value="Take Order" ></td>';
echo "</tr>";
}
echo "</form>";
echo "</table>";
Answer:
echo '<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>';
You set $bakeryid with $_POST['bakeryid'], but then define bakeryid with $bakeryid.
Please try:
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes c INNER JOIN members m ON c.memid = m.memid");
$bakeryid = $_POST['bakeryid']; // this line is unnecessary
?> <table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['order'] . "</td>";
echo '<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>'; // this line changed
echo '<td><input type="hidden" name="memid" value="' . $memid . '"/><input type="submit" name="takeorder" value="Take Order" ></td>';
echo "</tr>";
}
echo "</form>";
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes c INNER JOIN members m ON c.memid = m.memid");
$bakeryid = $_POST['bakeryid']; ?>
<table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo '<tr>
<td>' . $row['firstname'] . '</td>
<td>' . $row['order'] . '</td>
<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>
<td><input type="hidden" name="memid" value="' . $memid . '"/>
<input type="submit" name="takeorder" value="Take Order" >
</td>
</tr>';
}
echo '</form></table>';
The reason bakeryid isn't appearing in the form, is when setting the fields value you are using $bakeryid which is set to a post that hasn't happened yet. You want to set the value to $row['bakeryid'] as above.

Adding css to a form

I am trying to add CSS to my form but not sure how to do this. The form is created in php and MySQL, in browser it looks like: http://gyazo.com/5d099ead9bd6ea83859a5114b2438748
I need to allign the text and drop downs so they are in the equal throughout and add some spacing. Anyone help with CSS for this?
html currently:
<div class="wrap">
<img src="/images/logo.png" alt="Highdown logo" />
<h1>Entry form</h1>
</div>
css currently:
.wrap {
position: relative;
}
The form is produced with this:
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\" action=\"save.php\"> ";
while ($row = $event_result->fetch_assoc()) {
echo $row['Name']. ' ';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo "<select name ='". $row['Name']."'>";
while ($row1 = $student_result->fetch_assoc()) {
echo '<option value="" style="display:none;"></option>';
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select> <br />";
}
}
}
echo '<input type="submit" value ="Submit">';
echo '<input type="reset" value ="Reset">';
echo '<input type="button" value = "Add student" onclick="location.href=\'http://localhost/sportsday/addstudent.php\'">';
echo '<input type="button" value = "Delete student">';
echo "</form>";
}
Use
<form>
<table>
<tr> //1st Table row
<td></td> //Table column data
<td></td> //table column data
</tr> //1st row ends
<tr> // 2nd Table row
<td></td> //Table column data
<td></td> //table column data
</tr> //2nd row ends
</table>
</form>
This will give you a better layout of the form.
This should work i did not try as i dont have the database
//Query to display all events
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\" action=\"save.php\"> ";
echo '<table>';
echo '<tr>';
echo '<td>';
while ($row = $event_result->fetch_assoc()) {
echo $row['Name']. ' ';
echo '</td>';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo '<td>';
echo "<select name ='". $row['Name']."'>";
while ($row1 = $student_result->fetch_assoc()) {
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select> <br />";
echo '</td>';
echo '</tr>';
}
}
}
echo '</table>';
echo '<input type="submit" value ="Submit">';
echo '<input type="reset" value ="Reset">';
echo '<input type="button" value = "Add student" onclick="location.href=\'http://localhost/sportsday/addstudent.php\'">';
echo '<input type="button" value = "Delete student">';
echo "</form>";
}
?>
you can directly write in css
form {
⋮ declare css
}
or give name to form
form[name="value"]{
⋮ declare css
}
or add any class or id on form
#formid{
⋮ declare css
}
.formclass{
⋮ declare css
}
First , check your database...
May be there is Another Issue not related to Tabular Output.
So , First remove Table Tag..and check whether its working ?
Then try in HTML TABLE TAG
Otherwise give me sample database .sql File and complete PHP code in google drive or on shared drive.
So that I can check and identify where is problem ?

PHP and MySQL Database Search Multiple Tables At One Time

I am trying to make the form search by ID, firstname, and lastname. I want the user to type in either one in the search field and get the results from the database.
Here is the actual form I am using:
<form action="form.php" method="post">
<input type="text" name="term" />
<input type="submit" value="Submit" />
</form>
And here is the form.php
<?php
$db_hostname = 'localhost';
$db_username = 'test';
$db_password = 'test';
$db_database = 'test';
// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_database, $con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>SpeedZone Data Search</title>
<style type="text/css">
table { border-collapse:collapse; }
table td, table th { border:1px solid black;padding:5px; }
tr:nth-child(even) {background: #ffffff}
tr:nth-child(odd) {background: #ff0000}
</style>
</head>
<body>
<form action="form.php" method="post">
<input type="text" name="term" />
<input type="submit" value="Search" />
</form>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname LIKE '%".$term."%' or id = ".$term;
$r_query = mysql_query($sql);
echo "<table border='1' cellpadding='5'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Address</th> <th>City</th> <th>State</th> <th>Zip</th> <th>Phone</th> <th>DL</th> <th>Email</th> <th>Car and Controller</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while ($row = mysql_fetch_array($r_query)){
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
echo '<td>' . $row['address'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['st'] . '</td>';
echo '<td>' . $row['zip'] . '</td>';
echo '<td>' . $row['phone'] . '</td>';
echo '<td>' . $row['dl'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['carcont'] . '</td>';
echo '<td>Edit</td>';
// echo '<td>Delete</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
}
?>
</body>
</html>
Where I currently have this: It is only searching the ID. I want to be able to type in the ID, or the firstname, or the lastname, or first and last if possible.
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname LIKE '%".$term."%' or id = ".$term;
I think there are a few things I will need to change but I am confused and have lost myself in it and cannot solve it. Please help.
You need to put quotes around $term when you compare it with id. Otherwise, you'll get a syntax error if it's not a number.
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname LIKE '%".$term."%' or id = '".$term."'";
Also, this assumes you don't use 0 as an id. When a number is compared to a string, the string is converted to a number, and all non-numeric strings get converted to 0 and they'll match that id. If that's a problem, you should check whether $term is a number first. If it's not a number, use a query that doesn't include the id check:
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%$term%' or lastname LIKE '%$term%'";
if (is_numeric($term)) {
$sql .= " or id = $term";
}
Your query looks coorect to me.
Try using braces to separate out OR conditions
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or (lastname LIKE '%".$term."%') or (id = ".$term);

From where do i pass id to delete a record?

How do i pass id to delete record in this code?
<form action="index.php">
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
</form>
The above code is in index.php and it is submitting it to itself.
Without needing javascript, seperate GET urls etc, just plain old HTML & the original POST: just add the ID to the name of the button:
<input type="submit" value="Delete" name="btnDel[<?php echo $id;?>]">
And in receiving code:
if(isset($_POST['btnDel']) && is_array($_POST['btnDel'])){
foreach($_POST['btnDel'] as $id_to_delete => $useless_value){
//delete item with $id_to_delete
}
}
Here's how I would do it:
Change
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
To
echo '<td><input type = "button" value = "Delete" onclick = "btnDel(' . $row['id'] . ')" /></td>';
Add the following field to the form (outside of the while loop of course):
<input type="hidden" name="id" id="userid" />
Define the following javascript function:
function btnDel(id) {
if (confirm("Really delete id=" + id + "?")) {
document.getElementById('userid').value = id;
document.forms[0].submit();
}
}​
Then you can retrieve that value using $_GET['id'] or $_POST['id'] depending on your form's method.
EDIT: Here's a working demo, and its source
Use this to submit the id as a part of form.
<input type="hidden" id="id" name="id" value="<? echo $row['id']; ?>" />
or you can send values in URL to do the same thing
An example:
Delete
A full working sample
<?
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
switch($_GET['action']) {
case "delete":
$query = "DELETE FROM tbluser WHERE id='".$_GET['id']."'"
$result = mysql_query($query);
break;
//well other actions
}
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
You could also send it in the url at action="index.php" given that you move it below while(($row = mysql_fetch_array($query)) != NULL) { like this
echo "<form action='index.php?var=" . $row['id'] . "'>";
You would then get the variable using $_GET['var']
If the id needs to stay hidden (on the page and in the link) a hidden input element and submitting the form using method="post" like previously suggested would be the better way to go.
From the table:
echo '<input type="hidden" name="user_id" value="'.$row['id'].'" />';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
This will be sent to the server as a parameter named user_id. Ideally, you should be using the POST method in the form.
This assumes that the user id is named, well id in the table.
BTW, the != NULL is unnecessary.
It's sufficient to write:
while($row = mysql_fetch_array($query)) {
NULL evaluates to FALSE in a boolean context.
Update:
As mentioned in comments to another answer, there is an issue with your approach. With multiple rows, you won't be able to distinguish between user ids.
One solution would be use multiple forms.
Another option would be to have the name of the submit button include the id. You'd then parse this out of the name of the $_POST array keys.

Categories