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.
Related
The web screenshot shows that the time value is fetched from database column settime, I want to insert my own value into the set time column and update that single row but upon clicking update it doesn't update.
If I were to replace the update query with delete query as shown below it works and delete that particular row. My question is how can I use same method to update that particular column value based on id.
<?php
$id =(isset($_GET['id']) ? $_GET['id'] : null);
if($id!=null) {
$query="delete from activity where id=$id";
$result=mysql_query($query);
}
?>
Web
database
timer.php
<?php include 'header.php'; ?>
<div class="main" >
<table width="900px" cellpadding="5" cellspacing="5">
<tr>
<td style="width:10px"></td>
<td style="background:#F2F2F2; height:400px">
<?php
$settime =(isset($_POST['settime']) ? $_POST['settime'] : null);
$id =(isset($_GET['id']) ? $_GET['id'] : null);
if($id!=null) {
//$settime =(isset($_POST['settime']) ? $_POST['settime'] : null);
$query="update feedtime set settime='$settime' where id=$id";
$result=mysql_query($query);
}
?>
<table width="100%" class="tableStyle">
<tr style="font-weight:bold">
<td>NO.</td>
<td>User</td>
<td>Time</td>
<td>Time Left</td>
<td></td>
</tr>
<?php
$i=0;
$query="select * from feedtime";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)){
$i+=1;
echo '
<tr style="height:25px">
<td>'.$i.'</td>
<td>'.$row['user'].'</td>
<td><input id="timer" type="text" name="settime" style="width:200px;" value='. $row['settime'].' /></td>
<td>'. $row['timeleft'] .'</td>';echo '
<td align="center"><input type="button" value="Update" style="font-size:10px;width:80px;height:25px;padding:0;margin:0" onclick="location.href=\'timer.php?id='.$row['id'].'\' " /></td>
</tr>';
}
if($i==0)echo '
<tr style="height:25px">
<td colspan="6">Record not found</td>
</tr>'; ?>
</table><br>
<?php echo mysql_error(); ?>
</td>
</tr>
</table>
</div>
<?php include 'footer.php'; ?>
Ignoring the implementation details for a second (like using the MySQL extension), your main problem appears to be submitting (by POST) a single id / settime pair of values.
I would simply make a form per row. Something like this (very simplified)
<?php while($row = fetch_some_data()) : ?>
<tr>
<td>
<input form="form<?= $row['id'] ?>" type="text" name="settime" value="<?= $row['settime'] ?>" />
<td>
<td>
<form id="form<?= $row['id'] ?>" method="post" action="timer.php">
<button type="submit" name="id" value="<?= $row['id'] ?>">UPDATE</button>
</form>
</td>
</tr>
<?php endwhile ?>
Note the form attribute on the <input> and id attribute on the <form> are the same. This lets you tie the input to that form without it being a descendant element, allowing you to include all the required data without bastardising the HTML to get the form to wrap the table-row.
With this, pressing any "UPDATE" button will submit a POST request with data like
id=1&settime=12%3A08%3A00
which you can retrieve in your script via $_POST['id'] and $_POST['settime'].
For example (and using MySQLi)
if (isset($_POST['id'], $_POST['settime'])) {
$stmt = $conn->prepare('UPDATE `feedtime` SET `settime` = ? where `id` = ?');
$stmt->bind_param('si', $_POST['settime'], $_POST['id']);
$stmt->execute();
}
I have a dropdown HTML menu with some values which I want to insert into an input table. When the 'Insert Production' button is clicked I want it to insert these values into the relevant data table.
Previously I just entered the 'genre' details manually as text and they input into the table ok, but have now changed the input to a dropdown, but the value doesn't get entered into the table. Is there a simple way of ensuring that this value gets uploaded to the table? Sorry if this is painfully basic, but am still pretty new to all this.
<html>
<head>
<title>Inserting Production</title>
</head>
<body>
<form method="post" action="insert_product.php" enctype ="multipart/form-data">
<table width="500" height="650" align="center" border="2" bgcolor="#c6ff1a">
<tr align="center">
<td colspan="2"<h1>Insert new production:</h1></td>
</tr>
<tr>
<td align="right"><b>Production Name:</b></td>
<td><input type="text" name="prod_name" size="40"/></td>
</tr>
<tr>
<td align="right"><b>Production Genre:</b></td>
<td><select>
<option value="Drama">Drama</option>
<option value="Thriller">Thriller</option>
<option value="Comedy">Comedy</option>
<option value="Children">Children</option>
<option value="Sci-fi">Sci-fi</option>
<option value="Horror">Horror</option>
<option value="Documentary">Documentary</option>
<option value="Fantasy">Fantasy</option>
</select></form></td>
</tr>
<tr>
<td align="right"><b>Production Year</b></td>
<td><input type="text" name="prod_year"</td>
</tr>
<tr>
<td align="right"><b>Production Description</b></td>
<td><textarea name="prod_desc" cols="35" rows="10"/></textarea></td>
</tr>
<tr>
<td align="right"><b>Product Keywords</b></td>
<td><input type="text" name="prod_keywords"</td>
</tr>
<tr>
<td align="right"><b>Production Image</b></td>
<td><input type="file" name="prod_img"</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="insert_production" value ="Insert Production"/></td>
</tr>
</form>
</body>
</html>
<?php
//fetches product title, cat etc and submits them to database
if(isset($_POST['insert_production'])) {
//text data variables
$product_name = $_POST['prod_name'];
$product_genre = $_POST['prod_genre'];
$product_year = $_POST['prod_year'];
$product_desc = $_POST['prod_desc'];
$product_keywords = $_POST['prod_keywords'];
//Inserting image names in db and image files saving in my htdocs/Admin_area/product_images/$product_img1
//Images names
$product_img1= $_FILES['prod_img']['name'];
//Image temporary name(we have to set image temporary names)
$temp_name1= $_FILES['prod_img']['tmp_name'];
if ($product_name == '' OR $product_genre == '' OR $product_desc == '' OR $product_year == ''){
//Simple validation alert. If user leaves any of above fields empty
//will pop-up alert box saying 'Please insert your data etc ...'
echo "<script>alert('Please complete all fields!')</script>";
exit();
}//if
else {
move_uploaded_file($temp_name1,"product_images/$product_img1");
$insert_product = "insert into productions (prod_name, prod_genre, prod_year, prod_desc, prod_keywords, prod_img)
values ('$product_name', '$product_genre', '$product_year', '$product_desc', '$product_keywords', '$product_img1')";
$run_product = mysqli_query($db, $insert_product);
//If this query runs
if ($run_product) {
echo "<script>alert('Production inserted successfully!')</script>";
echo "<script>window.open('index.php?insert_product', '_self')</script>";
}//if
}//else
}//if
?>
just add this line and try again to submit the from.
<select name="prod_genre">
You forgot to give your select tag a name. You need to give it a name if you want to use this form element for e.g. database storage etc.
So change <select> to <select name="prod_genre">
I hope this helps you!
I try to show some data by dropdown option from mySQL
when the user choose option United states and click submit, the page will go to the next page and show the data only for United states
here is my code for test.html
<body>
<table border="0">
<tr>
<th>test
</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select>
<option value="US">United States</option>
<option value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<button type="submit">submit</button>
</td>
</table>
</body>
here is my second page showDB.php
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");
//connect to database
mysql_select_db("asdasd");
//query the database
//$query = mysql_query("SELECT * FROM auip_wipo_sample");
if($_POST['value'] == 'US') {
// query to get all US records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='US'");
}
elseif($_POST['value'] == 'AUD') {
// query to get all AUD records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='AUD'");
} else {
// query to get all records
$query = mysql_query("SELECT * FROM auip_wipo_sample");
}
//fetch the result
Print "<table border cellpadding=3>";
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$row['invention_title'] . "</td> ";
Print "<th>Pet:</th> <td>".$row['invention-title'] . " </td></tr>";
}
Print "</table>";
?>
I try the above code, but I got 2 error which are
Undefined index: value in C:\xampp\htdocs\folder\showDB.php on line 10
Undefined index: value in C:\xampp\htdocs\folder\showDB.php on line 14
line 10 is
if($_POST['value'] == 'US') {
and line 14 is
elseif($_POST['value'] == 'AUD') {
anyone can give solution
thanks
You need to add a name parameter to test.html - either set it to name="value" to make showDB.php to work without any alterations, or set lines 10 and 14 to match the name parameter you set. Example below:
edit:
#adeneo is right, you also need to add a form and a valid submit button
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="value">
<option name="country" value="US">United States</option>
<option name="country" value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<input type="submit" name="btn_submit" />
</td>
</table>
</form>
</body>
and the php:
if($_POST['country'] == 'US') {
// query to get all US records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='US'");
}
elseif($_POST['country'] == 'AUD') {
You have no element with the name value in your form, so you don't have a $_POST['value'] either! Actually, you don't even have a form to submit at all, you just placed a link to another page inside the submit button ?
<body>
<form action="showDB.php">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="value">
<option value="US">United States</option>
<option value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<button type="submit">submit</button>
</td>
</table>
</form>
</body>
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.
my current problem is :
I have a HTML table created "dynamically" according to how many rows brings back a mysql_query. The first column gets tha data from the query and the second column have a text field (see below):
<?php
$selApart = "SELECT idAPARTMENT FROM BUILDING, APARTMENT WHERE APARTMENT.BUILDING_ID = BUILDING.idBUILDING AND idBUILDING = '$building'";
$res = mysql_query($selApart) or die("Could not execute query.");
?>
<table width="244" border="0" cellspacing="0" id="hours_table">
<tr>
<td width="121">APARTMENT</td>
<td width="119">HOURS</td>
</tr>
<?php
$rcnt = 0;
while($r = mysql_fetch_array($res)){
$a = $r['idAPARTMENT'];
$rcnt++;
$rid = 'row'.$rcnt;
?>
<tr>
<td>'<?php echo $a?>'</td>
<td id='<?php echo $rid?>'><input type="text" name="hours" id="hours" value="0"/></td>
</tr>
<?php } ?>
<input type="submit" name="complete" id="complete" align="middle" value="INSERT"/>
After my table is "ready", I want to fill in my text fields and insert these values in an sql table. What I don't know is how I can get the value of each column through the id I set, sth like
if(isset($_POST['complete'])){
for($i=0; $i<$rcnt; $i++){
//INSERT INTO APARTMENT (idAPARTMENT, HOURS) VALUES ($a, **table.row.id**)
}
}
Can someone help? Is this possible to be done?
Thanks in advance!
He, thing you have to do is add $rid as name to text box, then just submit form and you will have them in $_POST["row"+$rid];
You can loop through every $_POST and if variable starts with row + num save it in db
foreach($_POST as $key => $value)
//if $key starts with row execute your db save
I hope this helps
Put that table into a form with method=POST and when submitting you'll find all input's in the $_POST array by name.
<?php
print_r($_POST);//this will just show you the contents of the array, you play with it later on
$selApart = "SELECT idAPARTMENT FROM BUILDING, APARTMENT WHERE APARTMENT.BUILDING_ID = BUILDING.idBUILDING AND idBUILDING = '$building'";
$res = mysql_query($selApart) or die("Could not execute query.");
?>
<form action="" method="POST"><!-- table must be included in a form with action pointing the script location, "" means itself -->
<table width="244" border="0" cellspacing="0" id="hours_table">
<tr>
<td width="121">APARTMENT</td>
<td width="119">HOURS</td>
</tr>
<?php
$rcnt = 0;
while($r = mysql_fetch_array($res)){
$a = $r['idAPARTMENT'];
$rcnt++;
$rid = 'row'.$rcnt;
?>
<tr>
<td>'<?php echo $a?>'</td>
<td id='<?php echo $rid?>'><input type="text" name="hours<?= $a ?>" id="hours" value="0"/></td><!-- name="hourse<?= $a ?>" each name must be unique, that's why you include the ID from your table. It's not a good idea to put a counter, use actual data from the table. -->
</tr>
<?php } ?>
</table>
<input type="submit" name="complete" id="complete" align="middle" value="INSERT"/>
</form>
The post "get data from dynamically created html table for certain columns" was very useful. It helped me a lot, but needs a little change:
<?php
//database connectivity
mysql_connect ("localhost","root","","sis")or die("cannot connect");
mysql_select_db("sis")or die("cannot select DB");
//query to fetch data
$sql1="select * from student";
$result1= mysql_query($sql1);
// forming table to view data
if($result1){
echo "<table cellspacing='1' cellpadding='5'>
<th width='30%'>Roll_No </th>
<th width='40%'>Name </th>
<th width='30%'>Sem & Sec </th>";
while($row=mysql_fetch_array($result1)){
$Roll_No=$row['Roll_No'];
$Name=$row['Name'];
$Sem_Sec=$row['Sem_Sec'];
echo"<tr>
<td>$Roll_No</td>
<td>$Name</td>
<td>$Sem_Sec</td>
</tr>";
}
?>