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);
}
Related
I want to create a dropdown menu with aaaa,bbbb,aaaa,cccc.
This is what I have but it returns an empty menu.
<form method="POST">
<br><br><label for="selections">Make a selection: </label>
<select id="selections" name="selections" required>
<?php
global $wpdb;
$results = $wpdb->get_results($wpdb->prepare( "SELECT columndata FROM mytable"));
foreach ($results as $pointer) {
echo "<option value='$pointer'>$pointer</option>";
}
?>
</select>
</form>
Please try below modify code:
<form method="POST">
<br><br><label for="selections">Make a selection: </label>
<select id="selections" name="selections" required>
<?php
global $wpdb;
//Add prefix before in your table name
$results = $wpdb->get_results("SELECT columndata FROM wp_mytable");
foreach ($results as $pointer) {
//Result from query in object so need to change your variable $pointer
echo "<option value='$pointer->columndata'>$pointer->columndata</option>";
}
?>
</select>
</form>
Below is the code where i created a drop down list, i am able to retrieve data from MySQL but i have to repeat the option step for every field, how can I setup a code for the drop down so i can just shorten to call it once in selection list, like in a for loop..also how do i create a error message to display if there is no selection, and if i select a data from the list it stores in the database
<form action="sign.php" method="post">
<label for="employee_name">Employee name</label>
<select name = "employee_name">
<option value=""> -----------Select----------- </option>
<?php
$stmt = $pdo->prepare('Select name from people');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['name'].'</option>';
}
?>
</select>
<label for="manager">manager</label>
<select name = "manager">
<option value=""> -----------Select----------- </option>
<?php
$stmt = $pdo->prepare('Select name from people');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['name'].'</option>';
}
?>
</select>
<label for="senior">Senior</label>
<select name = "Senior">
<option value=""> -----------Select----------- </option>
<?php
$stmt = $pdo->prepare('Select name from people');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['name'].'</option>';
}
?>
</select>
<input type="submit" name="register" value=" Click to Add"></button>
</form>
Use array and run query once to set options in array.
<?php
$optionArray = array();
$stmt = $pdo->prepare('Select name from people');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$optionArray[] = '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
?>
<form action="sign.php" method="post">
<label for="employee_name">Employee name</label>
<select name = "employee_name">
<option value=""> -----------Select----------- </option>
<?php
foreach($optionArray as $row)
{
echo $row;
}
?>
</select>
how can I setup a code for the drop down so i can just shorten to call it once in selection list, like in a for loop.
Just create a loop. though it should be foreach, not for.
First collect your data,
$people = $pdo->query('Select name from people order by name')->fetchAll(PDO::FETCH_COLUMN);
$sections = [
'employee_name' => 'Employee name',
'manager' => 'Manager',
'senior' => 'Senior',
];
and then loop it over
<form action="sign.php" method="post">
<?php foreach ($sections as $label => $caption): ?>
<label for="<?=$label?>"><?=$caption?></label>
<select name = "<?=$label?>">
<option value=""> -----------Select----------- </option>
<?php foreach ($people as $name): ?>
<option><?=$name?></option>
<?php endforeach ?>
</select>
<?php endforeach ?>
<input type="submit" name="register" value=" Click to Add"></button>
</form>
Instead of repeating this every time you can create a method which performs the same task.
<label for="employee_name">Employee name</label>
<select name = "employee_name">
<option value=""> -----------Select----------- </option>
<?=loadOptions('name','people');?> <!--shortcut way to echo in php-->
</select>
<?php
function loadOptions($field,$table){
$stmt = $pdo->prepare('Select '.$field.' from '.$table);
$stmt->execute();
$options='';
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$options .= '<option>'.$row['name'].'</option>';
}
return $options;
}
?>
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>
I don't know what I have done to this but I had the form working and now it has stopped. My problems are with the bottom if statement where I am trying to delete the user. As I said it was working properly and now it has stopped. I just can't figure out what the issue is? And yes I do backup but clearly should do it more!
function HMdisplayrooms() {
global $wpdb;
echo '<html><body><h1>Display Rooms</h1>';
echo '<p>Order room view by: <p>
<form name = "view_HM_rooms" method="post" action="">
<select name="roomsView" size = "1">
<option value = "room_id">Room ID</option>
<option value = "room_type">Room Type</option>
</select></br>
<input type="submit" name="action">
</form></body></html>';
echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
if(isset($_POST['action'])) {
$roomType = $_POST['roomsView'];
$query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
$rooms = $wpdb->get_results($query);
foreach ($rooms as $room) {
$roomID = ($room->room_id);
echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
<input type="submit" name="action2" value="Delete Room"></form></td></tr>';
if(isset($_POST['action2'])){
$results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
$msg = "Room deleted";
return $msg;
}
}
}
}
Use the code like this,
function HMdisplayrooms() {
global $wpdb;
echo '<html><body><h1>Display Rooms</h1>';
echo '<p>Order room view by: <p>
<form name = "view_HM_rooms" method="post" action="">
<select name="roomsView" size = "1">
<option value = "room_id">Room ID</option>
<option value = "room_type">Room Type</option>
</select></br>
<input type="submit" name="action">
</form></body></html>';
echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
if(isset($_POST['action'])) {
$roomType = $_POST['roomsView'];
$query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
$rooms = $wpdb->get_results($query);
foreach ($rooms as $room) {
$roomID = ($room->room_id);
echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
<input type="hidden" name="DelRooMId" value="'.$roomID.'">
<input type="submit" name="action2" value="Delete Room"></form></td></tr>';
}
}
if(isset($_POST['action2'])){
$roomID = $_POST['DelRooMId']; // sanitize the input
$results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
$msg = "Room deleted";
return $msg;
}
}
I have pulled the DELETE block outside of your code. It may help you. The problem with your code is $_POST['action'] will be null at the time of DELETE operation , so the code never executes, that may be the error
Note - Make sure to get the $roomID when you are using the below structure
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();