Dynamically updating db with SELECT menu - php

Thanks for all advice! I am pulling a list of months that are not set to "current month" into a drop down/select menu in order to set some other month to be available for action. I get that list but and it populates the dropdown but then doesn't pass that value to the update. I'm either super confused or missing something very small.
Here are the queries:
if (isset($_POST['addMonth'])) {
$addmonth = $_POST['addmonth'];
//open additional months
$query_open_additional = "UPDATE reimbmonths SET reimb_open = 1 WHERE monthname = '$addmonth' ";
$query_current = "SELECT * FROM reimbmonths WHERE reimb_open = 1";
$result_current = mysql_query($query_current) or die ('Query failed: ' . mysql_error() . "<br />\n$sql");
$num_current = mysql_num_rows($result_current);
echo "Added another month: " . $addmonth;
}
And this is the HTML/PHP:
<h3>Open additional months</h3>
<form id="add-months" action="" method="post" onsubmit="window.location.reload() onchange="addMonth();"">
<select name="addmonth" id="addmonth">
<option <? if (empty($_POST["addmonth"])) { echo 'selected';} ?> disabled> --- Choose a Month --- </option>
<?
$i = 0;
while ($i < $num_months) {
$month_to_add = mysql_result($result_select_closed,$i,"monthname");
?>
<option value="<? $month_to_add ?>" <? if($_POST["addmonth"] === '$month_to_add') { echo 'selected';} ?>><? echo ucwords($month_to_add) ?></option>
<?
$i++;
}
?>
</select>
<input class="submit_btn" type="submit" name="addMonth" value="Set Month"/>
</form>

Related

Displays the current value taken from ENUM in form

I have a field in the mysql database of type "ENUM",
the field is called "career_status" and these values are stored,
Active,
Retired,
Dead
I created a form for update, the one I would like and display the current value in the form.
Example:
if now it is stored Active displaying Active in the form, if nothing is empty etc ..
How to get these values from the field?
this is the code
<?php
require_once("connetti.php");
$actor_id = $_GET["id"];
$query = mysql_query("SELECT * FROM actor WHERE actor_id=" . (int)$actor_id) or die ("Error in query: " . mysql_error());
$row = mysql_fetch_array ($query);
$nome=$row['nome'];
$performer_aka=$row['performer_aka'];
$website=$row['website'];
$career_status=$row['career_status'];
$birthday=$row['birthday'];
$died=$row['died'];
$status=$row['status'];
?>
<div id="BioData" style="overflow: hidden;">
<center><h1><?php echo $nome;?> <span class="infoblock-pagetype">modifica dati</span></h1></center>
<form action="<?php echo $_SERVER['PHP_SELF'] . '?id=' . $actor_id; ?>" method="post">
<tr>
<td class="paramname">
<b>Career Status: </b> (<b><font color="red">selezionare il valore</font></b>)
</td>
<td class="paramvalue">
<select name="career_status" id="career_status"><?php echo $career_status;?>
<option value="1">Active</option>
<option value="2">Retired</option>
<option value="3">Dead</option>
<option value="0"> </option>
</select>
</td>
</tr>
<?php
if (isset($_POST['modifica']))
{
//include "connetti.php";
//$query = mysql_select_db("xxx", $db);
if ($query)
{
$nome=$_POST['nome'];
$performer_aka=$_POST['performer_aka'];
$website=$_POST['website'];
$career_status=$_POST['career_status'];
$birthday=$_POST['birthday'];
$died=$_POST['died'];
$status=$_POST['status'];
$query=mysql_query("UPDATE actor SET
nome='".$_POST['nome']."',
performer_aka='".$_POST['performer_aka']."',
website='".$_POST['website']."',
career_status='".$_POST['career_status']."',
birthday='".$_POST['birthday']."',
died='".$_POST['died']."',
status='".$_POST['status']."'
WHERE actor_id=". $actor_id);
if($query) echo "<h2>Congratulazioni! Dati inseriti.</h2>";
else echo "<h2>Attenzione! Dati non inseriti!</h2>";
} else echo "<h2>Errore! Database non selezionato.</h2>";
}
?>
If I understood correctly that you wish to indicate which career_status option is currently selected for the record then perhaps the following might help
<select name='career_status'>
<?php
/* possible options for career status */
$status=array(
0=>'',
1=>'Active',
2=>'Retired',
3=>'Dead'
);
for( $i=0; $i < count( $status ); $i++ ){
/* is this item selected? */
$selected = $i==$career_status ? ' selected' : '';
printf('
<option value="%d"%s>%s',
$i,
$selected,
$status[ $i ]
);
}
?>
</select>

PHP dropdown search result selecting wrong input

I have made a dropdown search form that is auto populated by my database content. The voices in the table would be for example types of woods with varing dimensions.
So there are repeatable wood names with diverse data.
To avoid repetition the dropdown is populated with wood types combined to be selected then displayed with all their variants.
The problem is, upon selecting an input, the results are of the item listed above and not the one selected.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
echo "<option value=\"\">Selezione Materiale</option>";
while($row = $info->fetch_assoc()){
if($row['finit'] != $finit) {
echo "<option value=\"$finit\">" . $row['finit'] . "</option>";
$finit = $row['finit'];
}
}
?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
Since there are many variants(dimensions) associated with a single wood type, you have to first take the wood type as input from user(via dropdown list), and probably then you may want to display all possible variants(dimensions) of that particular wood type.
So, change the SQL query in the following way,
$query = "SELECT DISTINCT finit FROM prime";
and the while loop in the following way,
while($row = $info->fetch_assoc()){
$output = "<option value='" . $row['finit'] . "'";
if($row['finit'] == $_POST['finit']){
$output .= " selected='selected'";
}
$output .= ">" . $row['finit'] . "</option>";
echo $output;
}
Try this but change if condition according your default value and sql value should be match.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
?>
<option value="">Selezione Materiale</option>;
<?php
while($row = $info->fetch_assoc()){
if($row['finit'] == $finit) {
$selected = 'selected';
}else{
$selected = '';
$finit = $row['finit'];
}
?>
<option value="<?php echo $finit ?>" <?php echo $selected ?>><?php echo $row['finit']?></option>
<?php } ?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

How to make data display in the table on my webpage once it's been input into my database?

I'm not sure how to describe it, so here's a video where I explain my problem.
I tried rearranging some of the code, as I do believe nothing is faulty, attempting to make sure that the table refreshes with the new data inside it, however every time I tried to place my code in a different order (executing the queries in different orders), it either functions differently than how I want it to function or it doesn't function at all.
Both queries do function separately, I'm just unsure why they're not working together.
Searchbar has the value seen inputted in the homepage on both my Search page and this page in question. However it was left blank for this page, which gave me the result of having the full table display which is what I wanted to happen. I'm just not sure how I can edit my code so, when submitted, it will display the newly added data.
My PHP:
<?php
$find = $_POST['searchbar'];
$host = "localhost";
$username = "FFF";
$pword = "L3FhqJNey8Op2qJY";
$database = "Project";
include 'includes/db.inc.php';
$Name2 = $_POST['Name'];
$YearOfRelease2 = $_POST['YearOfRelease'];
$Studio2 = $_POST['Studio'];
$Age2 = $_POST['Age'];
$Score2 = $_POST['Score'];
?>
My HTML:
<html>
<head>
<title>Add a Film - Films! Films! FILMS!</title>
</head>
<body>
<h1>Films! Films! FILMS!</h1>
<h2>Add a Film</h2>
<p>If you wish to add a film to our database, feel free to add data relating to the film in the respective boxes below. You should then refresh the page.</p>
<p>Add Film:</p>
<form method="POST" action="AddFilm.php">
<p>Name of Film: <input type="text" name="Name"></p>
<p>Year of Release: <input type="text" name="YearOfRelease"></p>
<p>Name of Studio: <input type="text" name="Studio"></p>
<p>Age Rating: <select name="Age" size="1">
<optgroup label="Select Age Rating">
<option value="U">U</option>
<option value="PG">PG</option>
<option value="12">12</option>
<option value="15">15</option>
<option value="18">18</option>
</optgroup>
</select></p>
<p>Review Score: <input type="text" name="Score"></p>
<p><input type="submit" name="submit" value="Submit and Refresh"></p>
</form>
<?php
echo "<h2>$output</h2>";
$query_string = "SELECT * FROM movies WHERE Name LIKE '%$find%' OR YearOfRelease LIKE '%$find%' OR Studio LIKE '%$find%' OR Age LIKE '%$find%' OR Score LIKE '%$find%'";
$query_string2 = "INSERT INTO movies (Name, YearOfRelease, Studio, Age, Score) VALUES ('$Name2', '$YearOfRelease2', '$Studio2', '$Age2', '$Score2');";
if ($result = $mysqli->query($query_string2)) {
$output2 = $Name2 ." has been added to the database.";
echo "<p>$output2</p>";
} else {
echo ("Error performing query: " . $mysqli->error() );
}
$result->close();
if ($result = $mysqli->query($query_string)) {
echo "<table border='1'>";
echo "<tr><th>FilmID</th><th>Name</th><th>YearOfRelease</th><th>Studio</th><th>Age</th><th>Score</th></tr>";
while ($row = $result->fetch_object())
{
$FilmID = $row->FilmID;
$Name = $row->Name;
$YearOfRelease = $row->YearOfRelease;
$Studio = $row->Studio;
$Age = $row->Age;
$Score = $row->Score;
$output ="<tr><td> $FilmID";
$output = $output . "<td> $Name";
$output = $output . "<td> $YearOfRelease";
$output = $output . "<td> $Studio";
$output = $output . "<td> $Age";
$output = $output . "<td> $Score </tr>";
echo "<p>$output</p>";
}
echo "</table>";
echo "<hr>";
echo '<p>Back to Home Page</p>';
$result->close();
} else {
echo ("Error performing query: " . $mysqli->error() );
}
$mysqli->close();
?>
</body>
</html>

How to connect the html drop down to fetch the records from mysql database

I'm struggling for this, please some one help. I'm not a html or php expert.
I've created a simple webpage that pulls the records from mysql database from last 7 days, 10 days etc by changing the select statement every time. Works fine till here.
But it is a pain to change the select statement every time, instead I want to create a simple drop down list.
I've added simple drop down with the list of the dates like 1(indicates one day of records), 7- 7 days of records etc. Once the user select 7, then it has to jump to that query that pulls 7 day records and then continue.
But what am I doing wrong here, I can't able to execute it.
Here is my code
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
<p>
<select name="value">
<option value="7">7</option>
<option value="1">1</option>
</select>
<?php
$dbh = mysqli_connect("","", "", "");
if (!$dbh) {
echo "Error: Unable to connect to mysqli." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
print ("<h1>Metrics</h1>");
print ("<table class=\"hovertable\">\n");
if($_POST['value'] == '7')
{
// query to get 7 records
$select="select * from table where FROM_UNIXTIME(date)>= (NOW() - INTERVAL 7 DAY)";
}
else
{
// query to get 1 records
$select="select * from table where FROM_UNIXTIME(date)>= (NOW() - INTERVAL 1 DAY)";
}
$result = $dbh->query($select);
print "Total records :". ' '. $result->num_rows ;
if ($result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{
// Capture each record
$data = array('row'=>$row);
print ("<tr>");
print ("<td style=\"white-space: nowrap\"> $ID</td>");
print ("<td style=\"white-space: nowrap\"> $department</td>");
print ("<td style=\"white-space: nowrap\"> $customername</td>");
print ("<td style=\"white-space: nowrap\"> $date</td>");
print ("</tr>\n");
}
}
else
{
echo "0 results";
}
exit;
?>
$dbh->close();
</p>
</body>
</html>
I see that you are checking for the user posted value here :
$_POST['value']
But I did not see where and how you are posting this value to the page. Maybe you need a form and a submit button to post the selected value:
<form action="youpage.php" method="post">
//your dropdown code here
<input type="submit" value="Submit">
</form>
You most post the value of the select by either a form or JavaScript.
Using a form would look like:
<form method="post">
<select name="value">
<option value="7">7</option>
<option value="1">1</option>
</select>
<input type="submit" value="go" />
</form>
Using JavaScript
<form method="post" >
<select name="value" onchange="this.form.submit()">
<option value="7">7</option>
<option value="1">1</option>
</select>
</form>
If you don't want the whole page to reload, you'd want to separate or copy the query and table into another PHP page, and submit the form using an XHR.
Updated to include a function to create the select options.
<?php
Function ddform_Interval($array) {
// generates the dropdown selects based on values in array
$result = "";
$post = 1; // default post value
IF (isset($_POST['value'])) {
// validate the post value. It must match a value in array
IF (in_array($_POST['value'], $array)) { $post = $_POST['value']; }
}
Foreach($array as $value) {
IF ($post == $value) { $result .= "<option selected value=\"".$value."\">".$value."</option>";
}ELSE{ $result .= "<option value=\"".$value."\">".$value."</option>";
}
}
return $result;
}
$allowed_postvals = array('1', '7'); // add more values to add select options
$result = "";
$dbh = mysqli_connect("","", "", "");
if (!$dbh) {
echo "Error: Unable to connect to mysqli." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
// set & validate post value
IF (isset($_POST['value'])) {
IF (in_array($_POST['value'], $allowed_postvals)) { $postvalue = $_POST['value']; }ELSE{ $postvalue = '1'; }
}ELSE{
$postvalue = 1;
}
$query = "SELECT * FROM table WHERE FROM_UNIXTIME(date)>= (NOW() - INTERVAL ".$postvalue." DAY)";
$sql = $dbh->query($query);
if ($sql->num_rows > 0) {
$result .= "Total records :". ' '. $sql->num_rows ;
$result .= "<table class=\"hovertable\">\n";
while ($row = $sql->fetch_assoc()) {
// Capture each record
$data = array('row'=>$row);
$result .= "<tr>";
$result .= "<td style=\"white-space: nowrap\"> ".$row['ID']."</td>";
$result .= "<td style=\"white-space: nowrap\"> ".$row['department']."</td>";
$result .= "<td style=\"white-space: nowrap\"> ".$row['customername']."</td>";
$result .= "<td style=\"white-space: nowrap\"> ".$row['date']."</td>";
$result .= "</tr>\n";
}
$result .= "</table>\n";
}else{
$result .= "0 results";
}
$dbh->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
<p>
<form action="" name="form" method="post">
<select name="value"><?php echo(ddform_Interval($allowed_postvals)); ?></select>
<input type="submit" name="submit" value="Submit">
</form>
</p>
<h1>Metrics</h1>
<?php echo($result); ?>
</body>
</html>

drop down sorting options not working

i am trying to sort my products using a drop down box. when i select an option the code does not run and the products do not change position. iv managed to sort products by using different buttons but i think a drop down list would look better on a website.
<?php
echo $sort = #$_GET['order'];
if (!empty($sort)) {
echo $query="SELECT * FROM products ORDER BY '".$sort."'";
} else {
echo $query="SELECT * FROM products order by " ;
}
?>
<form name="sort" action="" method="post">
<select name="order">
<option value="choose">Make A Selection</option>
<option value="price_asc">Price </option>
<option value="price_desc">Z-A</option>
<option value="name_asc">A-Z</option>
</select>
<input type="submit" value=" - Sort - " />
</form>
<?php
//Run the query.
$record_set = $connection->query($query);
while( $row = $record_set->fetch_assoc() ) {
echo '<div class="product">';
echo '<div class="product-content"><h3>'. $row['name'].'</h3>' .'</div>' . '<br />'.'<div class="product-thumb"><img src="/ISD assignment2/images/'. $row['imageName'].'"></div>'. '<div class="product-desc">'.$row['description']. '</div>'. '<br />' .'£'. number_format($row['price'], 2) . '<p>Add</p>';
echo '</div>';
}
?>
Two potential issues:
1) Your Form has no action. I believe you are trying to get it to refresh onto itself.
which would be:
action=" <?php echo $_SERVER['PHP_SELF'] ?>"
2) You are submitting via POST, yet using GET.
$sort = $_POST['order'];

Categories