Dynamically displaying option values using PHP - php

I'm new to PHP and have created a very basic HTML form. As you can see in my form, the option values are all done by hand (there are more, I just simplified this example). What I want is for these to be generated dynamically using just PHP, so that I would physically have to add every single year etc.
I've done some searching but I can't seem to find exactly what I'm after so thought I'd ask here. From what I gather I need to create a query and echo out the option value somehow, although I'm not sure how to do this.
SELECT gameYear from games
I'd guess the above would be the correct query as all the form would need is the bookYear from the table?
<form id = "gameYear" method="get" action="result.php">
<label>
Game Year
<select name="gameYear">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
</select>
</label>
<input type = "submit" name="search" value = "Search">
</form>
Thanks, any help/guidance is appreciated.

<form id = "gameYear" method="get" action="result.php">
<label>
Game Year
<select name="gameYear">
<option value=''>--Select Year--</option>
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$SqlResult = mysqli_query($link, "SELECT gameYear from games");
while($Row = mysqli_fetch_array($SqlResult))
{
?>
<option value="<?php echo $Row['gameYear'] ?>"><?php echo $Row['gameYear'] ?></option>
}
?>
</select>
</label>
<input type = "submit" name="search" value = "Search">
</form>

<?php $sql = "SELECT gameYear from games order by gameYear ASC";
$result = mysql_query($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); ?>
<form id="gameYear" method="get" action="result.php">
<label>Game Year
<select name="gameYear">
<?php while($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['gameYear'] ?>"><?php echo $row['gameYear']?></option>
<?php } ?>
</select>
</label>
<input type = "submit" name="search" value = "Search">
</form>

Related

User form-data to update query PHP

I have basic question about storing $variable data and later use in html script, anyone who can help me? Right now, the variable $gset is not stored.
<?php
// Update Strictness value
if(isset($_POST['strictness'])){
$gset = $_POST['strictness'];
$strictnessUpdate = "UPDATE users SET strictness = '$gset' WHERE user_id = 1";
mysqli_query($conn, $strictnessUpdate);
echo "strictness value updated";
}
?>
<form method="POST" class="form-align" action="">
<h5 class="my-6">Current Strictness :</h5> <?php echo $gset; ?><br>
<select name="strictness" required>
<option value=""></option>
<option value="15">15</option>
<option value="31">31</option>
</select>
<input type="submit" name="substrict" value="CHANGE">
</form>
<?php
$_grabStrictness ="SELECT strictness FROM users WHERE id = 1";
$gs_query = mysqli_query($conn, $_grabStrictness);
$gs_result = mysqli_fetch_array($gs_query);
if ($gs_result > 0) {
while ($result = mysqli_fetch_array($gs_query)) {
$gset = $result['strictness'];
}
}
if(isset($_POST['strictness'])){
$gset = $_POST['strictness'];
$strictnessUpdate = "UPDATE users SET strictness = '$gset' WHERE user_id = 1";
mysqli_query($conn, $strictnessUpdate);
echo "strictness value updated";
}
?>
<form method="POST" class="form-align" action="setStrictness(<?php $conn ?>)">
<h5 class="my-6">Current Strictness :</h5> <?php echo $gset; ?><br>
<select name="strictness" required>
<option value=""></option>
<option value="15">15</option>
<option value="31">31</option>
</select>
<input type="submit" name="substrict" value="CHANGE">
</form>

Dropdown PHP with SQL with two different queries in one line inside dropdown > (Name, Number)

I want the dropdown to show the "client_code", "name" in one line. It almost works but not 100%. I am a beginner with php and SQL, can someone help me please?
Code that doesn't work
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$queryKlantCode = "SELECT klant_code FROM klant";
$resultKlant=mysqli_query($mysqli,$queryKlant);
$resultKlantCode=mysqli_query($mysqli,$queryKlantCode);
while($row=mysqli_fetch_array($resultKlant) &&
$row2=mysqli_fetch_array($resultKlantCode) )
{
?>
<option><?php echo $row[0]. ", ". $row2[0];?></option>
<?php
}
?>
</select>
</form>
Code that only works with retrieving name in dropdown from database
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$res=mysqli_query($mysqli,$queryKlant);
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row[0]; ?></option>
<?php
}
?>
</select>
</form>
You can select more than one column from a table in the same select, and as both these columns live in the same table it makes producing this result much simpler.
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$sql = "SELECT naam, klant_code FROM klant";
$result = mysqli_query($mysqli,$sql);
while($row=mysqli_fetch_array($result)){
?>
<option><?php echo $row[0]. ", ". $row[1];?></option>
<?php
}
?>
</select>
</form>
You probably want to do this with your <option> tag as well rather than put the name and code in the visible portion
<option value="<?php echo $row[1];?>"><?php echo $row[0];?> </option>
And if you use mysqli_fetch_assoc() you can use the columns names so you know what you are putting where
while($row=mysqli_fetch_assoc($result){
<option value="<?php echo $row['klant_code'];?>"><?php echo $row['naam'];?> </option>

For each form option selected, add to mysql

I have a multiple select form:
<form method="post" action="register_results.php" name="registerform" class="form-horizontal" role="form">
<div class="label">Select Name:</div>
<select name="names" multiple="yes" size="15">
<option value = "">---Select---</option>
<?php
while ( $row=mysqli_fetch_assoc($result)) {
echo "<option value='".$row['registrant_name']."'>".$row['registrant_name']."</option>";
}
mysqli_close($con);
?>
</select>
</form>
The register_results.php file looks like this:
$registrant_name = $_POST['names'];
$event_result = $_POST['result'];
$query = "UPDATE events_regged SET result = $event_result WHERE event_name='event10' AND registrant_name=('$registrant_name') ";
$result = mysqli_query($con, $query);
I want to be able to add multiple mysql rows (one row for each username) if multiple names are selected in the form. How can I do that?
Use
<select name="names[]" multiple="yes" size="15">
instead of
<select name="names" multiple="yes" size="15">
and use foreach
foreach($_POST['names'] as $value)
{
//your query goes here
}
You have some changes required within your code as
<select name="names" multiple="yes" size="15">
^^^^^
it should be
<select name="names[]" multiple size="15">
This will result into an array so the result of
$registrant_name = $_POST['names']
will be an array
Change the form select element name from "names" to "names[]" and then you can access an array of posted values - which you can then iterate through.
First change your form as below (it's more elegant and fast to use Id instead of name (string) in Sql):
<select name="ids[]" multiple size="15">
<option value = "">---Select---</option>
<?php
while ( $row=mysqli_fetch_assoc($result)) {
echo "<option value='".$row['registrant_id']."'>".$row['registrant_name']."</option>";
}
mysqli_close($con);
?>
</select>
And in your PHP use foreach:
$registrant_ids = $_POST['ids'];
$event_result = $_POST['result'];
foreach($registrant_ids as $id)
{
$query = "UPDATE events_regged SET result = $event_result WHERE event_name='event10' AND registrant_id=".$id;
$result = mysqli_query($con, $query);
}

php mysql search using boxlist

I am looking for a way to search for data from the database using input type box list, I tried make the code but it doesn't display anything:
html code:
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="1">user1</option>
<option value="2">user2</option>
<option value="3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
php code:
if (isset($_POST['users'])) {
$key = trim ($_POST['users']);
$s = "SELECT * FROM users where user_name LIKE '%$key %'";
$res = mysql_query($s) or die('query did not work');
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
when I try the code I didn't get any result and when I remove the while loop and put this instead of it :
<?php echo $key; ?>
it gives me the numbers of the selected value, for example if I select user2 the result will be 2. and I want the result to be user id and user name.
you need to fetch all the user name in your drop down select box
<select name="users">
<option selected="selected" value="">-- select --</option>
<?php $s2 = "SELECT * FROM users";
$q2=mysql_query($s2) or die($s2);
while($rw=mysql_fetch_array($q2))
{
echo '<option value="'.$rw['userid'].'">'.$rw['username'].'</option>';
}</select>
?>
<?php if (isset($_POST['search'])) { // submit button name here
$key = $_POST['users'];
$s = "SELECT * FROM users where user_id='".$key."'";
$res = mysql_query($s) or die($s);
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
edit your html to this,you will get the in $_POST which will be in value='something'
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="user1">user1</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
Or if value is the id of user then change query to this
$s = "SELECT * FROM users where user_id='".$key."'";

How to update values in my DB with new values from <select> and <input> tags?

With this way, I get results from database and "print" them. But I don't know how I will update those results when I press the submit button!!! I just need an idea or something for the next step. Thank you in advance!!!
Here is an example of my code...
<?php // DATABASE QUERY
$query="SELECT countdown_module, hometeam_position
FROM jos_gm_nextmatch
WHERE id = 1";
$result=mysql_query($query);
// DATABASE VARIABLES
$countdown_module = mysql_result($result,$i,"countdown_module");
$hometeam_position = mysql_result($result,$i,"hometeam_position"); ?>
<form action="***.php" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<select name="hometeam_position">
<option value="<?php echo $hometeam_position ?>"><?php echo $hometeam_position ?></option>
<option disabled="disabled" value="...">...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">3</option>
<option value="5">5</option>
<input name="submit" type="submit" value="UPDATE" />
</form>
You would use the form action to redirect to a script where you do the update. On this script you can access the the forms input elements by using the $_POST array. As for how to do update queries, an example could be:
$query="UPDATE mytable
SET title = '".$title."', name = '".$name."', date = '".$date."'
WHERE id = ".$id;
$result=mysql_query($query);
UPDATE:
An example of the script could be:
$hometeam_position = $_POST['hometeam_position']; //access the selected option when submitting
$countdown_module = $_POST['countdown_module']; //access the text input
$query = "UPDATE jos_gm_nextmatch SET countdown_module = '".$countdown_module."', hometeam_position = '".$hometeam_position."' WHERE id = 1";
$result=mysql_query($query);
You could before or after selecting the fields from the database simply increment them
...
if (isset($_POST['submit'])) {
$stmt = "UPDATE jos_gm_nextmatch
SET countdown_module = " . $_POST['countdown_module'] .
" , hometeam_position =" . $_POST['hometeam_position'] .
" WHERE id=1";
mysql_query($stmt);
}
mysql_close();

Categories