Using php to UPDATE my sql for a review page - php

So this should be my last question for this entire project. I've made the employee timecard pages. Works great. I'm trying to make them a review/change page per timecard. Basically the initial review page pulls up the timecards they've submitted. They select Review. It carries the ID number to a review page, and I call upon the ID number to display all the info from that timecard into the new timecard html/php form. (Basically its the exact same form they used to submit, however I've echo'd the values as the option already)
When I do update though it's not only carrying over the echo'd value. It's only Updating any changes they make, and in fact deleting everything else. Below is a snippit of just one drop-down list (all over non drop-down's update fine. This is just for drop down lists where the data is contained in the DB.
<select name="starttime" id="input_6" style="width:150px">
<option value="" selected><?php echo $stime ?></option>
<?php
$result = mysql_query("SELECT time FROM selTime ORDER BY id");
while($row = mysql_fetch_array($result)) {
echo "<option value=\"" . $row['time'] . "\">" . $row['time'] . " </option>";
}
?>
</select>
So what happens with this code. Is when they open the review page They would see their start time as they submitted it. If they leave it alone, and not change anything and push submit after changing something else. The starttime actually UPDATES as blank. If they change the starttime to a value it DOES submit the change. Is there something in this code that I can change that will submit the echo'd value, instead of just displaying it?

I believe the issue is with this line of code:
<option value="" selected><?php echo $stime ?></option>
Although you set it as "selected" the selected element has the value of "" (empty). Try changing it to:
<option value="<?php echo $stime; ?>" selected="selected"><?php echo $stime; ?></option>

If they dont change this select list item, and submit the page directly, the selected item is the first one with the value="", so this will update in the database with blank ( null).
You need to give your fist item a value of date time now. This code here:
<option value="DATETINMENOW" selected><?php echo $stime ?></option>

Related

HTML text inputs not returning proper value

I used to be program in .Net so PHP with MySQL is very new for me.
I wanted to have same form for displaying and updating the DB, with work fine. I dont have issues with that, but after the save happens, the text inputs retains old value.
My HTML side
<select class="form-control" name="form1">
<option selected="selected">
<?php echo htmlspecialchars($GLO_variable1); ?>
</option>
<option value="option1">option 1</option>
<option value="option2">option 2</option>
</select>
<input class="btn btn-primary" type="submit" name="updaterecord" value="Update Summary">
I wanted the options to to reflect current record on the DB, which works fine when viewing/getting the record from DB.
<?php
function GetRecord(){
require 'conn.php';
if(!session_id()) session_start();
$globalinc = $_SESSION["globalinc"];
if(isset($globalinc)){
$sqlget="SELECT * FROM t_summary WHERE SNOW_INC= '". $globalinc ."'";
$result = mysqli_query($conn, $sqlget);
$getrecordarray = mysqli_fetch_assoc($result);
$GLOBALS['GLO_PRIORITY'] = $getrecordarray['PRIORITY'];
$GLOBALS['GLO_INC_STATEMENT'] = $getrecordarray['INC_STATEMENT'];
mysqli_free_result($result);
mysqli_close($conn);
}
}
?>
The above example sets data from MySQL to text input and text area in HTML. I called GetRecord() at the beginning of body with
<body>
<?php GetRecord(); ?>
</body>
Then I try to make changes to the form and update it with following.
<?php
function UpdateRecord(){
require 'conn.php';
$snowinc = mysqli_real_escape_string($conn, $_POST["SNOW_INC"]);
$incstatement = mysqli_real_escape_string($conn, $_POST["INC_STATEMENT"]);
$priority = mysqli_real_escape_string($conn, $_POST["PRIORITY"]);
$sql = "REPLACE INTO t_summary (SNOW_INC, INC_STATEMENT, PRIORITY)
VALUES ('$snowinc', '$incstatement', '$priority')";
if ($conn->query($sql) === TRUE) {
echo "<br>";
echo "Incident Summary updated successfully.";
echo "<br>";
} else {
echo "<br>";
echo "Error: " . $sql . "<br>" . $conn->error;
echo "<br>";
}
$conn->close();
}
if(array_key_exists('SNOW_INC',$_POST)){
UpdateRecord();
}
?>
Well, his also works well updating the data to DB, but problem happens after I saw a data.
For example, if value in DB shows option 1 then when I view the page it shows option 1, all good. When I change the value to option 2 and save, the value is updated in DB but remained the same in HTML. Obviously if I reload the page, it shows option 2 but I'm trying to avoid that.
Any idea to fix this will be much appreciated. Thank you in advance.
And I know I missed lots of other stuff here. Please ask questions if you want to know more info to fix this.
Thanks again!!
Edit : I have uploaded the site so you can look at the issue better
Instruction :
Go to https://www.mydevplace.xyz/alpha/ and create do a search of any string. It will say record not found and prompt you to create one.
Create a record. Should be self explanatory. Make sure to click Update Incident record. You should get a green confirmation saying Incident Summary updated successfully.
Now on the same page, edit the values to a different strings/records and click the same button. The values is returning to previous value. Thats my problem. If you keep clicking the button, you would see what I mean.
EDIT 2 : I implemented AJAX to update my records and only GetUpdate on first load of the page. All is working well now.
Thanks to all who helped me with this.
Change option value from option1 to option2
<option value="option1">option 2</option>
To
<option value="option2">option 2</option>
Although you have not shared your html, It will be difficult to estimate error.
However I found the below issue with you php codes.
function GetRecord(){
global $getrecordarray;
..
..
..
}
You forgot to define the $getrecordarray as global.

select dropdown value according to previous valuesubmit new one

I use the following code to select all value from a DB table and put it in dropdown. Here is the code :
<select name="dis_name">
<?php
$qu="Select DISTINCT name from root";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
Based on previous code a value is selected and submitted. I want to show previously selected value in dropdown on another page so i use following code:
<select name="dis_name">
<option value="RAM"<?php echo $r20 == "RAM" ? " selected" : ""; ?>>RAM</option>
<option value="ROHAN"<?php echo $r20 == "ROHAN" ? " selected" : ""; ?>>ROHAN</option>
</select>
This code work fine till no new value added in DB table. But whenever i add new value i have to update edit code manually. Is there any way, so that dropdown previous selection updated automatically in edit page.
Add the condition in your loop, eg:
<?php
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'";
if ($r[0] == $r20) echo " selected";
echo "> $r[0] </option>";
}
?>
If I get what you want (assuming that $r20 is the previously selected value:
<?php
$qu="Select DISTINCT name from root";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'";
if($r[0]==$r20){echo 'selected="selected"';}
echo "> $r[0] </option>";
}
?>
this way the select will display as selected the option that match the $r20 value dinamically.
BTW, you should use id in your code. This way you could also have more than one user with the same name (but different ID). With DISTINCT, as you are doing, if you have more than one user with the same name you will get only one.

Keep selected option in php

I have below code which runs query correctly but does not keep selected option. I could get similar answers but not worked with this code. Here select options are in combination of user given and rest are from mysql column.
I am new to php, so any help will be appreciated.
<select name="tester">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($conn, "Select DISTINCT tester from workflow1");
while($r=mysqli_fetch_row($dd_res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
You can use the $_POST superglobal array to check the value of submitted form fields. In this case $_POST['tester'] contains the value of the select field after submitting the form.
echo "<option value='$r[0]'".($_POST['tester']==$r[0]?' selected':'')."> $r[0] </option>";
I am thinking you are asking to select the value passed into the form? If I am correct in that, you would do something like:
$selVal = $_REQUEST['tester'];
$val = $r[0];
echo '<option value="'.$val.'"';
if ($val == $selVal) {
echo ' selected="selected"';
}
echo '>'.$val.'</option>';
The $_REQUEST gets any POST, GET or COOKIE value and should be validated to make sure no hack attempts or anything. Once you have the value, you just add the selected attribute if it matches the value in the list.

PHP/MySQL Auto select option based on previous page

I have a music database with a PHP front end where you can add/edit/delete artists, albums, tracks using the web client. The last real problem I have is getting a select box to automatically select an option I pass to the page.
Example:
On a page called 'createCD.php' I have this code:
echo "<td><a href=\"editCD.php?cdTitle=".rawurlencode($row['cdTitle'])."&cdID=$row[cdID]&artID=$row[artID]&cdGenre=".rawurlencode($row['cdGenre'])."&cdPrice=$row[cdPrice]\" </a>Edit</td>";`
This is used as a link to the next page, and collects all the information about an album in the database and sends in to a page called 'editCD.php'.
Now on this page, all the information is used to fill out the webpage as shown here (there is more but for the purposes of this post, only the first select box matters):
Artist Name:
<!-- dropdown with artist name -->
<?php
echo '<select name= "artID" id="artID">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['artID'].'">'.$row['artName'].'</option>';
}
echo '</select>';
?>
<p>
Album Title:
<input id="cdTitle" type="text" name="cdTitle" value ="<?php echo htmlspecialchars($cdTitle); ?>" />
</p>
What I would like is for the "selected" option for 'artID' to be the value that is passed to the page. Using the associative array, I was able to display the 'artName' associated with the 'artID'. Currently, all the information about the album appears correctly apart from the 'artName' and it defaults to the first value. This is a problem as if a user simply clicks "Update" it will update the name to the default name, therefore changing the database entry by accident.
I know I need to be using
<option selected ...>
but I'm not sure on the syntax to use.
<?php
$artID = $_GET['artID']; // get the artID from the URL, you should do data validation
echo '<select name= "artID" id="artID">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['artID'].'"';
if ($artID == $row['artID']) echo ' selected'; // pre-select if $artID is the current artID
echo '>'.$row['artName'].'</option>';
}
echo '</select>';
?>
$artId = $_GET['artID'];
while ($row = mysqli_fetch_assoc($result)) {
$selected = $artId == $row['artID'] ? 'selected' : '';
echo '<option value="'.$row['artID'].'" '.$selected.'>'.$row['artName'].'</option>';
}
First you get the id via $_GET['artID']. (In a real scenario use intval or something to prevent sql injection)
Then check in the loop if the id from database is the same as the id from GET and when it is print "selected, else nothing.

Multiple initially selected values in a dynamic select list with PHP

Thanks in advance for your help. This list is to update an existing record, so it's populated from a database with $rs_fullcat then checked with another recordset $rs_cat for the initially selected values. I can only get it to initially select one value, the first one in $rs_cat, but I need it to select all of the existing options from the database. I feel like I'm close but I can't find the answer anywhere. Here's the code:
<select name="category" multiple="multiple" id="category">
<?php
do {
?>
<option value="<?php echo $row_rs_fullcat['categoryID']?>"<?php if (!(strcmp($row_rs_fullcat['categoryID'], $row_rs_cat['categoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_fullcat['category_name']?></option>
<?php
} while ($row_rs_fullcat = mysql_fetch_assoc($rs_fullcat));
$rows = mysql_num_rows($rs_fullcat);
if($rows > 0) {
mysql_data_seek($rs_fullcat, 0);
$row_rs_fullcat = mysql_fetch_assoc($rs_fullcat);
}
?>
</select>
What you want to do, is first select (and fetch) all the selected ones from the database, and put them in a variable ($rs_cat in your case). Then, in your while loop it's a simple matter of doing an in_array() check to see if the value is selected.

Categories