I have a table with some data from a mysql database. I need to update that information from within the table itself so when I press the button submit (table is inside a form) I need that all that data gets updated on the database. I can get the information from the database with no problem but I'm unable to update it! Here is the code:
<table border=1 cellpadding=4 cellspacing=0 width=960px style='font-size:10px'>
<form id='form1' name='form1' method='post' action='itself.php'>
<thead>
<tr>
<th colspan=3> People on the list </th>
<th><input type='submit' name='filtrar' id='filtrar' value='Filtrar'/></th>
<th><input type='submit' name='modificar' id='modificar' value='Modificar'/></th>
</tr>
<tr>
<th><label for='id'>ID</label></th>
<th><label for='id'>Friends</label></th>
<th><label for='id'>On the list?</label></th>
</tr>
<?
while($row = mysqli_fetch_array($result_listas, MYSQLI_BOTH))
$id= $row['id'];
{
?>
<tr>
<td><? echo "$row[id]" ?>
</td>
<td><select name='friends[<? echo "$id" ?>]' size='1' id='friends[<? echo "$id" ?>]'>
<option selected='selected'><? echo "$row[friends]" ?></option>
<option>less than 10</option>
<option>more than 10</option>
</select>
</td>
<td><select name='onlist[<? echo "$id" ?>]' size='1' id='onlist[<? echo "$id" ?>]'>
<option selected='selected'><? echo "$row[onlist]" ?></option>
<option>SI</option>
<option>NO</option>
</select>
</td>
<?
$ssql_min="select min(id) as id from listas_old";
$result_min= mysqli_query($link, $ssql_min);
$resultado_min = mysqli_fetch_array($result_min, MYSQLI_BOTH);
$ssql_max="select max(id) as id from listas_old";
$result_max= mysqli_query($link, $ssql_max);
$resultado_max = mysqli_fetch_array($result_max, MYSQLI_BOTH);
if(isset($_POST[modificar]))
{
for($i=$resultado_min['id']; $i<$resultado_max['id']; $i++)
{
$sql1="UPDATE listas_old SET friends='$friends[$i]', onlist='$onlist[$i]' WHERE id='$i'";
$result1=mysqli_query($link, $sql1);
}
}
?>
The OP wrote in a comment:
Got the solution for myself! This is what I did...
Change the for sentence like this:
for($i=$resultado_min['id'];
$i<=$resultado_max['id'];
$i++) {
$friendsfor= $_POST['friends('.$i.')'];
$onlistfor= $_POST['onlist('.$i.')'];
$sql1="UPDATE listas_old SET friends='$friendsfor', anotado= '$onlistfor' WHERE id='$i'";
}
It seems like you have your quotes all wrong in the update. You are trying to concatenate a string (your update sql) with php variables. But you need to make sure that php recognizes them as a variable instead of just as a string.
Should be like this
$sql1= "UPDATE listas_old SET friends='" . $friends[$i] . "', onlist='" . $onlist[$i] . "' WHERE id='" . $i . "'";
You only need to add in the single quotes if you are adding a varchar or date or something. If it is an int, you don't need the single quotes. Also I'm not sure what onlist would do, or if that is valid.
Related
For this project, I have an upload page for the user to upload a CSV file where it is inserted into a DB in one single staging table. From there, some things are split and also inserted into separate tables. Each CSV has 2 to 8 rows, and each row has 228 fields, so there are 228 columns in my staging table.
Once uploaded, the user can go to a page (userSelect.php) and use dropdowns to help select a work order to view where it will be displayed in multiple tables.
The upload/insert works great, and the dropdowns are populating with the elements from the database properly but I can't seem to connect the selections to the table page.
For instance, if the user selects the "Work Order Packet" dropdown and chooses 'February Zone B1', I want this to show links with any record from the database that has this work order packet name. Then, once they select the record they want, I want the display.php page to fill all the tables with the 228 elements from that specific record, using the id I assume.
I have the code for these two pages below. The userSelect page does not currently have any code for showing the database records as links either so i'm hoping to figure out how I can show those and then use the one the user selects to fill the tables on the display page.
userSelect.php
<form method="post" action="display.php" enctype="multipart/form-data">
<input type="submit" name="submit" value="Confirm" >
</form>
<?php
$server = "localhost";
$user = "root";
$pw = "root";
$db = "uwsTest";
$connect = mysqli_connect($server, $user, $pw, $db);
if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo'success!';
}
?>
<label>Select Work Order Packet:</label>
<select name="workOrderPacket">
<?php
$sql = mysqli_query($connect, "SELECT workOrderPacket FROM staging;");
while($row = $sql->fetch_assoc()){
echo "<option >" . $row['workOrderPacket'] . "</option>";
}
?>
</select>
<label>Select Work Order ID:</label>
<select name="WorkOrderNumber">
<?php
$sql2 = mysqli_query($connect, "SELECT workOrderNum FROM staging;");
while($row2 = $sql2->fetch_assoc()){
echo "<option>" . $row2['workOrderNum'] . "</option>";
}
?>
</select>
display.php
<?php
if(isset($_POST['submit']))
{
while($row = mysqli_fetch_array($result1)){
?>
<!--Qa Table-->
<table>
<tr>
<th colspan="2">Qa/Qc CheckList</th>
</tr>
<tr>
<td>Service Address Correct</td>
<td><? echo $row['serviceAddress'];?> </td>
</tr>
<tr>
<td>Service Loc Correct</td>
<td><? echo $row['serviceLoc'];?> </td>
</tr>
<tr>
<td>Meter Number Correct</td>
<td><? echo $row['meterNumber'];?> </td>
</tr>
<tr>
<td>Meter Manufacturer Changed</td>
<td><? echo $row['meterManufacturer'];?> </td>
</tr>
<tr>
<td>Meter Type Changed</td>
<td><? echo $row['meterType'];?> </td>
</tr>
<tr>
<td>Meter Model Changed</td>
<td><? echo $row['meterModel'];?> </td>
</tr>
<tr>
<td>Low Register Correct</td>
<td><? echo $row['registerCorrect'];?> </td>
</tr>
<tr>
<td>High Register Correct</td>
<td><? echo $row['registerCorrect'];?> </td>
</tr>
</table>
UPDATE - unfinished SQL query for display.php:
$query1 = "SELECT * FROM staging WHERE StageID = ;";
$result1 = mysqli_query($connect,$query1);
You will need to update your SQL statement to filter the associated results.
SELECT workOrderNum FROM staging WHERE workOrderPacket = '.$_REQUEST["workOrderPacket"] ';
I am fairly new to PHP and SQL.
I want to be able to assign a task to a certain user and the update that row in the database with the user's name assigned to that task.
Here is my code:
<table border="0" width="1100px" style= "font-size: 12px" >
<thead>
<tr valign="top" align="left">
<th height="20"></th>
<th height="20">Customer</th>
<th>Vehicle</th>
<th>Appt Time</th>
<th>Notes</th>
<th>Assign</th>
<th>Action</th>
<tr><td valign="top" colspan="6"><hr><br></td></tr>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr valign='center'>
<td width='50'><b>{$row['id']}</td>
<td width='220' height='70'><b>{$row['firstname']}
</td>
<td width='240'><b>{$row['car']}</b> <br>{$row['reg']}</td>
<td width='170'>Monday<br>24 September<br>17:00</td>
<td width='240'>{$row['notes']}<br><b>Status:</td>
<td width='240'>
<form action='bookings.php' method='post'>
<select style='width:90px' class='reg' name='assign'required>
<option value=''></option>
<option value='User1'>User1</option>
<option value='User2'>User2</option>
<option value='User3'>User3</option>
<option value='User4'>User4</option>
<option value='User5'>User5</option>
</select><input type='submit' value='>' class='assignButton'/></form>
</td><td>
<button class='myButton'>Edit</button>
</td>
<tr><td colspan='6'><hr class='hrTitle'></td></tr>
</tr>\n";
}
?>
</tbody>
</table>
As you can see, I have a number of users that can be selected, I want to be able to assign that task to a user from the select list.
Any help is much appreciated.
Try the following steps.
Give a name to your submit button.
<input type='submit' value='>' name='submit' class='assignButton'>
Add a hidden input field to your form. Give it the value of the id of the current row. Give it a name. I chose id.
This is very important so you can know which customer to edit. Please double check the value that you will set. From your code, I see it is $row['id'].
<input type="hidden" name="id" value='$row["id"]''>
In your PHP file bookings.php (assuming you have a connection to your database), process the submitted form.
if(isset($_POST["submit"])){ //checks if the form was submitted
$id = $_POST["id"]; //id of the customer
$assign = $_POST["assign"]; //your selected value
$query = "UPDATE table SET columnToModify = '$assign' WHERE id = '$id'";
$result = $connection->query($query); //run the query
}
Hope it helps.
Look like everything is working fine with this code but in fact fails to update the database, Data are displayed correctly while fetching data but when i press update Button the data disappear but no update has been executed. It look fine to me but seems i am wrong.
This is a project for my professor so i don't care for the SQL injection and others.
<html>
<head>
<link rel="stylesheet" type="text/css" href="btnstyle.css">
<title>Managament System</title>
</head>
<body>
<h1>TU Chemnitz Student managament system</h1>
<br>
ADD Person
Edit Person
Manage Boards
Manage Departments
Search N&S
Triple Search
Membership
<br>
<br>
<?php
// set database server access variables:
$host = "localhost";
$user = "";
$pass = "";
$db = "";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$querys = "SELECT * FROM tblperson";
// execute query
$result = mysql_query($querys) or die ("Error in query: $query. ".mysql_error());
echo "<table border=1 align=center>
<tr>
<th>Personal ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Deparment</th>
<th>Board</th>
<th>Marticulation Number</th>
<th>Reg Date</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result)) {
?>
<?php
echo '<tr>';
echo '<td>'. $row['personid'].'</td>';
echo '<td>'. $row['personname'].'</td>';
echo '<td>'. $row['personsurname'].'</td>';
echo '<td>'. $row['persondepartment'].'</td>';
echo '<td>'. $row['personboard'].'</td>';
echo '<td>'. $row['martinumber'].'</td>';
echo '<td>'. $row['personregdate'].'</td>';
echo '<td>'.' EDIT '.'</td>';
}
?>
</body>
</html>
and this is the edit file which seems to problematic.
<?php
include_once('coneksioni.php');
if(isset($_GET['edit']))
{
$personid = $_GET['edit'];
$res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row = mysql_fetch_array($res);
}
if(isset($_POST['newpersonname']))
{
$newpersonname = $_POST['newpersonname'];
$personid = $_POST['personid'];
$sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
$res = mysql_query($sql) or die ("Cant be updated");
echo "< meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
<form action="edit20.php" method="POST">
<table border="0">
<tr>
<td>First Name</td>
<td><input type="text" name="newpersonname" value="<?php echo $row[1];?>" maxlength="30" size="13"></td>
</tr>
<tr>
<td>Last Name</td>
<td> <input type="text" name="personsurname" value="<?php echo $row[2];?>" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Department</td>
<td>
<select name='persondepartment'>
<option>Production</option>
<option>Sales</option>
</select>
</td>
</tr>
<tr>
<td>Board</td>
<td>
<select name='personboard'>
<option>Evaluation</option>
<option>Executive</option>
<option>Research</option>
</select>
</td>
</tr>
<tr>
<td>Marticulation Number</td>
<td> <input type="text" name="martinumber" maxlength="60" size="30"></td>
</tr>
<tr>
<td>Date of Registration</td>
<td><input type="date" name="personregdate" maxlength="7" size="7"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value=" Update"></td>
</tr>
</table>
</form>
You are looking for personid when the Update button is pressed on the form in edit20.php but that value has never been set so it will be empty and the update will fail.
After
<form action="edit20.php" method="POST">
add:
<input type="hidden" name="personid" value="<?php echo $personid; ?>">
On edit page seem your confusing the same variable with different values. If you state $personid variable to contain the edit value from get, then just re-use the variable don't assign new value. On this line you assign new value :
$personid = $_POST['personid'];
Don't assign new value since it has the initial value already to use just set the variable global for usage
$personid = $_GET['edit'];
Or else create a hidden element and pass edit value into it.
Please add name attribute for your update button
<td colspan="2"><input type="submit" name="update" value=" Update"></td>
and chk whether the update button set or reset as in the place of
if(isset($_POST['newpersonname'])) // change text 'newpersonname' as 'update'
You use a variable that doesn't excist:
<?php
include_once('coneksioni.php');
if(isset($_GET['edit']))
{
$personid = $_GET['edit'];
$res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row = mysql_fetch_array($res);
}
if(isset($_POST['newpersonname']))
{
$newpersonname = $_POST['newpersonname'];
$personid = $_POST['personid']; // this doesn't excist
$sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
$res = mysql_query($sql) or die ("Cant be updated");
echo "< meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
$personid = $_POST['personid']; doesn't excist in your code. Its simply a piece of code you put in there to probably proces, but forgot to define the variable in the code. Place the following in your form.
<input type="hidden" name="personid" value="<?php echo $_GET['edit']; ?>">
You only use this just once because you send the form back after proces to your home, hence it wont be used anymore. You can also use the avariable you defined as $personid; on that position.
If that fails, something maybe wrong in your query. Try to echo out the query (remove qucikly the meta command) by simply just do echo $sql after you do the sql query. 9 out of 10 times, it's a typo.
Ive been having problem with my codes.. Im a still noob with php so i really appreciate if anyone could help me :'( .. Im trying to display data from database based on the selected dropdown list value. There are three data currently in my database table .. However, only one show up on my output .. i dont know whats wrong with my codes. can anyone help me ..
this is my code departmentlist.php
Choose Department :
<form action="" method="POST">
<select name="department">
<option value="" selected="selected">-- Choose department --</option>
<option value="Information System Department">Information System Department</option>
<option value="Finance">Finance</option>
<option value="HR and Administration">HR and Administration</option>
<option value="Security">Security</option>
</select>
<input type="submit" value="Submit" />
</form>
</br>
<table border="1" id="table">
<tr><th bgcolor="#00CCCC">Department</th>
<th bgcolor="#00CCCC">Locaion</th>
<th bgcolor="#00CCCC">Serial Number</th>
<th bgcolor="#00CCCC">Description of asset</th>
</tr>
<?php
mysql_connect('localhost','root',"") or die('Error1 '.mysql_error());
mysql_select_db("ams") or die('error2'.mysql_error());
if($_SERVER['REQUEST_METHOD'] =='POST')
{ $dep=$_POST['department'];
$query="SELECT * FROM asset WHERE department= '" . $dep . "'";
$run=mysql_query($query);
$numrow = mysql_num_rows($run);
$row=mysql_fetch_array($run, MYSQLI_ASSOC);
echo "<tr><td bgcolor='#00FFCC'>".$row['department']."</td><td bgcolor='#00FFCC'>".$row['location']."</td><td bgcolor='#00FFCC'>".$row['serialno']."</td><td bgcolor='#00FFCC'>".$row['desc']."</td></tr>";
}
}
?>
</table>
Manual: http://www.php.net/mysql_fetch_array
Put mysql_fetch_array in a while loop so:
while ($row = mysql_fetch_array($run, MYSQL_ASSOC)) {
echo "<tr><td bgcolor='#00FFCC'>".$row['department']."</td><td bgcolor='#00FFCC'>".$row['location']."</td><td bgcolor='#00FFCC'>".$row['serialno']."</td><td bgcolor='#00FFCC'>".$row['desc']."</td></tr>";
}
I also suspect that you should MYSQL_ASSOC not MYSQLI_ASSOC since you're using mysql not mysqli.
Take note that mysql will be deprecated so use mysqli.
you have to loop through the results.
while($row = mysql_fetch_array($run, MYSQLI_ASSOC)){
echo "<tr><td bgcolor='#00FFCC'>".$row['department']
."</td><td bgcolor='#00FFCC'>".$row['location']
."</td><td bgcolor='#00FFCC'>".$row['serialno']
."</td><td bgcolor='#00FFCC'>".$row['desc']
."</td></tr>";
}
Try using a while loop.
while($row=mysql_fetch_array($run)){
echo "<tr>
<td bgcolor='#00FFCC'>".$row['department']."</td>
<td bgcolor='#00FFCC'>".$row['location']."</td>
<td bgcolor='#00FFCC'>".$row['serialno']."</td>
<td bgcolor='#00FFCC'>".$row['desc']."</td>
</tr>";
}
I figured out how to update a table with multiple columns using checkboxes, except when more than one checkbox is selected the update will only happen for one of the columns not both. Could someone please help? Thank you!
Here is the working code for the table:
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="frmactive" method="GET" action="assigncases1.php">
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr>
<select name="assigned_to">
<option value=""></option>
<option value="Kristin Dodd"> Kristin Dodd </option>
<option value="Matt Ursetto"> Matt Ursetto </option>
<option value="Derek Bird"> Derek Bird </option>
<option value="John Castle"> John Castle </option>
<option value="Martin Delgado"> Martin Delgado </option>
</select>
<br>
<input type='submit' value = 'Assign'>
</tr>
<tr>
<td> </td>
<td colspan="4" align="center"><strong>Update multiple rows in mysql with checkbox<br>
</strong></td>
</tr><tr>
<td align="center" bgcolor="#FFFFFF"></td>
<td align="left"><strong>Name</strong></td>
<td align="left"><strong>SSO</strong></td>
<td align="left"><strong>case_status</strong></td>
<td align="left"><strong>Assigned To:</strong></td>
</tr>
<?php
$result=mysql_query($sql);
$count=mysql_num_rows($result);
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><input name="checkbox" type="checkbox" id="checkbox[]" value="<?
echo $rows['id']; ?>"></td>
<td><? echo $rows['full_name']; ?></td>
<td><? echo $rows['sso']; ?></td>
<td><? echo $rows['case_status']; ?></td>
<td><? echo $rows['assigned_to']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center"> </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
And this is what I have for the php page that take the info.
// Retrieve data from database
$checkbox = $_GET['checkbox'];
$assigned_to = $_GET['assigned_to'];
// update data in mysql database
$sql="UPDATE rmstable2 SET assigned_to='$assigned_to' WHERE id='$checkbox'";
$result = mysql_query($sql);
$count = mysql_numrows($result);
{
mysql_query("UPDATE `rmstable2` SET `assigned_to` = '$assigned_to'
WHERE `id` = '$checkbox'")
or die(mysql_error());
}
//If they did not enter a search term we give them an error
if ($assigned_to == "")
{
echo "<h3>You forgot to enter a required field. Please try again.</h3><br>";
}
// if successfully updated.
else if($assigned_to !== "")
{
echo "<b>Update Successful</b><br>";
echo "<br>This case was assigned to:<b> $assigned_to</b><br>";
echo "<br>To see the change go back and click on <b>Refresh Page</b> if you assigned it
to someone other than you, the case will disappear and show up in their list of
assigned cases.";
}
else {
echo "ERROR";
}
?>
In your HTML, you're defining your checkboxes with the name checkbox - so only one value will be sent to PHP. To get an array of values, update the checkboxes to be created with name="checkbox[]" and when the form is submitted, you can use the selected values like an array.
In that case, you'll be able to loop through each item with:
$checkbox = $_GET['checkbox'];
$assigned_to = $_GET['assigned_to'];
foreach ($checkbox as $id) {
// do a very basic validation to see if the ID is numeric
if (!is_numeric($id)) continue;
$sql = 'UPDATE rmstable2 SET assigned_to="' . mysql_real_escape_string($assigned_to) . '" WHERE id=' . (int)$id;
mysql_query($sql);
}
Also, as always worth noting, the mysql_* functions are being deprecated and you should consider to start using either mysqli_* or PDO methods.