Reusable fieldsets PHP - php

I need to have about 60 different fieldsets within a form that all contain the same elements. The only difference is the ID being called by my query. The ID will be populating data for me from mysql.
Is there a way to do this more efficiently other than adding 60 fieldsets?
<fieldset>
<h2>Make Your Pick</h2>
<?php
require_once ('mysql_connect.php');
$query = "SELECT id, name FROM table WHERE id = 1";
$result = #mysql_query ($query) or die ('error submitting' .mysql_error());
echo "<select name='winner' id='winner'><option>Who Will Win?</option>";
while($drop=mysql_fetch_array($result)){
//data stored in $drop
echo "<option value=$drop[id]>$drop[name]</option>";
}
echo "</select>";
?>
<input id="insert" type="submit" value="Next" />
</fieldset>

I found the answer!
<?php
for ($i = 1; $i <= 60; $i++) {
echo '<fieldset>
<h2 class="fs-title">Make Your Pick</h2>';
require_once ('mysql_connect.php');
$query = "SELECT id, name FROM table WHERE id = $i";
$result = #mysql_query ($query) or die ('error submitting' .mysql_error());
echo "<select name='winner' id='winner'><option>Who Will Win?</option>";
while($drop=mysql_fetch_array($result)){
//data stored in $drop
echo "<option value=$drop[id]>$drop[name]</option>";
}
echo "</select>";
echo '<hr>
<input id="insert" type="button" name="next" class="next action-button" value="Next" />
</fieldset>';
}
?>

Related

How to show selected user input in my HTML form?

I have a PHP form which saves user data to a MySQL database.
I want to show the user which information is held about them and display it in a form in order for them to update or edit the values.
I have a problem in getting the user's saved data from the database in a PHP loop and show that to user in order for them to update or edit it.
Below is the piece of code:
<?php
$id = $_GET['id'];
$conn = mysqli_connect('localhost', 'phpmyadmin', 'Test#2000', 'user');
$sql1 = "SELECT * FROM usr WHERE id='$id'";
$result = mysqli_query($conn, $sql1);
$row = mysqli_fetch_assoc($result);
?>
<fieldset><label>Birthday</label>
<select name="birthday">
<?php
for ($i = 1300; $i <= 1397; $i++) {
echo "<option >$i</option>";
}
?>
</select>
<fieldset>
<button name="btn" type="submit" id="contact-submit">Submit</button>
</fieldset>
</form>
</div>
</body>
</html>
I want to show into the form's Select input the birthday value that user selected originally, in order to edit or update by user.
The <select> children elements <option> does support a selected tag to indicate that it was the selected value, so by adding the selected tag like so <option value='1' selected> you can have that as the selected value.
You'll also want to probably add the $i value into your option element to ensure that the values are being submitted properly.
Mozilla documentation:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
I have edited your code in this way.
<?php
$id = $_GET['id'];
$conn = mysqli_connect('localhost', 'phpmyadmin', 'Test#2000', 'user');
$sql1 = "SELECT * FROM usr WHERE id='$id'";
$result = mysqli_query($conn, $sql1);
$row = mysqli_fetch_assoc($result);
?>
<fieldset><label>Birthday</label>
<select name="birthday">
<?php for ($i = 1300; $i <= 1397; $i++) {
echo "<option" . (($i == $row['birthdayYear']) ? 'selected="true"' : '') . ">$i</option>";
}
?>
</select>
<fieldset>
<button name="btn" type="submit" id="contact-submit">Submit</button>
</fieldset>
</form>
</div>
</body>
</html>
Hope this helps, thanks.
A.) WITH PDO MODULE
It is best practice today to use prepared statements to avoid SQL injection. This is done through the PDO object.
Set for select the autocomplete="off" attribute, because Firefox apparently has a bug with the selected="selected" that needs this to be set.
See if it is the user's birthday, we can use intval to compare.
<?php
$id = $_GET['id'];
$host = 'localhost';
$username = 'phpmyadmin';
$password = 'Test#2000';
$db_name = 'user';
$conn = new PDO('mysql:host:=' . $host . '; dbname=' . $db_name, $username, $password);
$sql1 = "SELECT * FROM usr WHERE id = :id;";
$stmt = $conn->prepare($sql1);
$stmt->bindParam(':id', $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<fieldset><label>Birthday</label>
<select name="birthday" autocomplete="off">
<?php for ($i = 1300; $i <= 1397; $i++) {
if($i === intval($row['birthdayYear'])){
echo "<option selected='selected'>$i</option>";
} else {
echo "<option>$i</option>";
}
}
?>
</select>
<fieldset>
<button name="btn" type="submit" id="contact-submit">Submit</button>
</fieldset>
</form>
</div>
</body>
</html>
B.) Please at least try option A.), but if it doesn't work:
<?php
$id = $_GET['id'];
$conn = mysqli_connect('localhost', 'phpmyadmin', 'Test#2000', 'user');
$sql1 = "SELECT * FROM usr WHERE id='$id'";
$result = mysqli_query($conn, $sql1);
$row = mysqli_fetch_assoc($result);
?>
<fieldset><label>Birthday</label>
<select name="birthday" autocomplete="off">
<?php for ($i = 1300; $i <= 1397; $i++) {
if($i === intval($row['birthdayYear'])){
echo "<option selected='selected'>$i</option>";
} else {
echo "<option>$i</option>";
}
}
?>
</select>
<fieldset>
<button name="btn" type="submit" id="contact-submit">Submit</button>
</fieldset>
</form>
</div>
</body>
</html>

Populate Radio buttons and submit value to query database

I am trying to learn programming and have to populate a set of radio buttons and submit what is selected to show records from a database. I have already done this with a selection list, but can't quite understand what I need to change to convert it to a radio buttons.
Selection list:
<?php
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";
$rs = mysqli_query($dbConn, $sql) or die ('Problem with query' . mysqli_error($dbConn));
?>
<form id="task9" action="task7.php" method="get">
<select name="staffID" id="staffID">
<?php
while($row = mysqli_fetch_array($rs)) {
$name=$row["staffName"];
$staffIden=$row["staffID"];
echo "<option value=".$staffIden.">".$name."</option>";
}
?>
<br><br>
<input type="submit" name="submit" method="get">
<input type="reset" name="reset">
</form>
Radio buttons (all I get is all the names and only One radio button):
<?php
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";
$rs = mysqli_query($dbConn, $sql) or die ('Problem with query' . mysqli_error($dbConn));
?>
<form id="task9" action="task7.php" method="get">
<input type = "radio" name="staffID" id="staffID">
<?php
while($row = mysqli_fetch_array($rs)) {
$name=$row["staffName"];
$staffIden=$row["staffID"];
echo "<option value=".$staffIden.">".$name."</option>";
}
?>
Hopefully this question is clear enough.
Try using below code.
<?php
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";
$rs = mysqli_query($dbConn, $sql)
or die ('Problem with query' . mysqli_error($dbConn));
?>
<form id="task9" action="task7.php" method="get">
<?php
while($row = mysqli_fetch_array($rs)) {
$name=$row["staffName"];
$staffIden=$row["staffID"];
echo "<label>";
echo "<input type='radio' name='staffID' value='".$staffIden."'/> ";
echo $name;
echo "</label><br/>";
}
?>
<br><br>
<input type="submit" name="submit" method="get">
<input type="reset" name="reset">
</form>
try this
<?php
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";
$rs = mysqli_query($dbConn, $sql)
or die ('Problem with query' . mysqli_error($dbConn));
?>
<form id="task9" action="task7.php" method="get">
<?php
while($row = mysqli_fetch_array($rs)) {
$name=$row["staffName"];
$staffId=$row["staffID"];
?>
<input type='radio' name='staffID' value='<?php echo $staffId ?>'/>
<?php echo $name; ?>
<br/>
<?php
}
?>
<br/><br/>
<input type="submit" name="submit" method="get">
<input type="reset" name="reset">
</form>
to add to what is there you will likely need to add either some code to your task7.php file to handle the database actions or if the file is task7.php you will need to add a block to the top of your file to handle the self-submitted form

Display saved checkbox value

I have some checkbox options that I save in the DB. I was able to view and also select multiple options and save them in the DB. The issue is that I want to display the saved information but I don't know how to do that.
<form action="save_comp.php" method="post">
<?php
//Display
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br />";
}
?>
<input name="submit" type="submit" value="submit" />
</form>
Save into DB
<?php
session_start();
$id = $_SESSION['user_id'];
//$id = 3;
include ('mysql_connect.php');
$insStr = '';
foreach($_POST['comp'] as $val){ $insStr .=$val.","; }
mysql_query("INSERT INTO competency_result (user_id,result) VALUES ( '$id', '$insStr' )") or die(mysql_error());
echo'<script>alert("Inserted Successfully")</script>';
?>
All I want to do now is to display the saved information in a table format. I tried doing this but it only showed me the saved ID
<?php
$res= mysql_query("SELECT * FROM competency_result WHERE user_id = '$user'")or die(mysql_error());
while($row = mysql_fetch_array($res))
{
echo"<tr>";
echo"<td> $row[result]</td>";
?>
<?php
echo"</tr>";
}
?>
<form action="save_comp.php" method="post">
<?php
//Display
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
echo"<input type='checkbox' name='comp[". $row['id']. "]' value='". $row['competency'] ."' /> ".$row['competency']." <br />";
}
?>
<input name="submit" type="submit" value="submit" />
</form>
If you want to checkboxes check then you can try with below code:
<?php
$sql = mysql_query("SELECT name FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
$focus=explode(",",$row['name']);
?>
<input type="checkbox" name="focus[]" value="Art" <?php if(in_array("Comp",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Mathematics" <?php if(in_array("Mathematics",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Dance" <?php if(in_array("Dance",$focus)) { ?> checked="checked" <?php } ?> >
<?php
}
?>

insert selected option into table

I have this code and I'm trying to put the selected state in a subcat table.
So far it returns an empty value. I'm not sure if this is clear or not, but all I want is: select a state from the select option and submit it. I want to get the selected state name into my table subcat.
enter <?php
include("connect.php");
$state = $row['states']; //Select name
if (isset($_POST[submit])){
$query = "INSERT INTO subcat (sub_name) VALUES ('$state')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo "<select name='states'>
<option value=''>Select a state</option>";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='$row[id]'>$row[name]</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="Continue" />
</form> here
Thanks
Change $state = $row['states'] to $state = $_POST['states']
<?php
include("connect.php");
if (isset($_POST[submit]))
{
$state = $_POST['states']; //Select name
$query = "INSERT INTO subcat (sub_name) VALUES ('$state')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo "<select name='states'>
<option value=''>Select a state</option>";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='$row[id]'>$row[name]</option>"; // if you want to
//get the name into table, then use like this
//echo "<option value='$row[name]'>$row[name]</option>"; or
//echo "<option>$row[name]</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="Continue" />
</form>
Try this:
enter <?php
include("connect.php");
if (isset($_POST[submit])){
$state = $_POST['states'];
$query = "INSERT INTO subcat (sub_name) VALUES ('".mysql_real_escape_string($state)."')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM state");
echo '<select name="states" id="states">
<option value="">Select a state</option>';
while ($row = mysql_fetch_assoc($sql)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
echo '</select>';
?>
<input type="submit" name="submit" value="Continue" />
</form> here
Dont forget to use mysql_real_escape_string to prevent SQL injections. I have replaced $state = $row['states']; with $state = $_POST['states'];
I dont know where u got $row from...
The above will insert the states name into the database.

How to get values from SQL query made by php?

So I made a query like this
global $connection;
$query = "SELECT *
FROM streams ";
$streams_set = mysql_query($query, $connection);
confirm_query($streams_set);
in my DB there are filds
ID, UID, SID, TIME (all INT type exept time)
So I am triing to print query relult into form
<form>
<select class="multiselect" multiple="multiple" name="SIDs">
<?php
global $connection;
$query = "SELECT *
FROM streams ";
$streams_set = mysql_query($query, $connection);
confirm_query($streams_set);
$streams_count = mysql_num_rows($streams_set);
for ($count=1; $count <= $streams_count; $count++) {
echo "<option value=\"{$count}\"";
echo ">{$count}</option>";
}
?>
</select>
<br/>
<input type="submit" value="Submit Form"/>
</form>
How to print out as "option" "values" SID's from my sql query?
while ($row = mysql_fetch_array($streams_set)) {
echo '<option value="'.$row['SID'].'">'.$row['SID'].'</option>';
}
<form>
<select class="multiselect" multiple="multiple" name="SIDs">
<?php
global $connection;
$query = "SELECT *
FROM streams ";
$queryResult = mysql_query($query, $connection);
while($row = mysql_fetch_assoc($queryResult)) {
echo '<option value="'. $row["id"] .'">'. $row["title"] .'</option>';
}
?>
</select>
<br/>
<input type="submit" value="Submit Form"/>
</form>
You just have to replace the id and title index with your appropriate fields.
or mysql_fetch_object()
http://php.net/manual/en/function.mysql-fetch-object.php
Ole Jak,
take a look at PHP's mysql_fetch_array http://php.net/manual/en/function.mysql-fetch-array.php, that's what you'll want to do in a while loop :-)

Categories