Am retrieving some data from the DB and am allowing users to make multiple selection via check box and also selecting a level for each selected check box. when saving, i only get to see the selected check boxes in the DB but not the level selected.
Code for making selection
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency WHERE department = '$department'");
while($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td>";
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo"<td> <select name='level[]'value= ".$row['id']." >
<option></option>
<option>level 1</option>
<option>level 2</option>
<option>level 3</option>
<option>level 4</option>
<option>level 5</option> </select> </td> ";
}
echo "</tr>";
?>
<input name="submit" type="submit" value="submit" />
</form>
<?php
echo" </table>";
?>
..
Code for saving into the DB
session_start();
$id = $_SESSION['user_id'];
$hobb = $_POST['comp'];
$level = $_POST['level'];
include ('mysql_connect.php');
$N = count($hobb);
for($i=0; $i < $N; $i++)
{
$var1=$hobb[$i];
$var2 = $level[$i];
//include ('connect.php');
include ('mysql_connect.php');
$table = "INSERT INTO competency_result (user_id,competency_id,level) ".
"VALUES ('$id', '$var1', '$var2')";
mysql_query($table) or die(mysql_error());
$inserted_fid = mysql_insert_id();
mysql_close();
}
Set the key the same for comp[] and level[], ie. $row['id'] -
while($row = mysql_fetch_array($sql))
{
...[your code]...
echo "<input type='checkbox' name='comp[".$row['id']."]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo "<td> <select name='level[".$row['id']."]' >
...[your code]...
}
(note: the <select> does not have a value attribute. That is defined by the <option>)
Then you can easily get the corresponding values using
foreach($hobb as $key=>$val)
{
$var1=$hobb[$key];
$var2=$level[$key];
...[your code]...
}
(also, it looks like you have <tr> inside your loop, but </tr> after. I would assume you would want to move the closing tag inside the loop to properly close each row)
Related
So, I need to make sure this sets the right parameters to the DB when pressing the buttons. I just want to get the calls and comparisons right so it does what it should when I hit the buttons. There should be one delete button for each row from database, and update page when I press it.
It should be possible to update the text/numbers in the forms presented by MySQL by changing the forms and press Save-button, then refresh the page.
$counter = 1;
if(isset($_POST['save']) and something is changed from the forms compared to DB) {
Update MySQL
Refresh page
<script>
window.location.replace("thispage.php");
</script>
}
if(isset($_POST['del'])) {
DELETE MySQL
Refresh page
<script>
window.location.replace("thispage.php");
</script>
}
echo "<tr><td>ID</td><td>Namn</td><td>Platser</td><td>Fullbokad</td><td>Ta bort</td></tr>";
$sqlListSections = "SELECT * FROM avdelningar WHERE user = {$_SESSION['id']}";
$queryListSections = mysqli_query($mysqli, $sqlListSections);
$del = [];
while($rowListSections = mysqli_fetch_array($queryListSections))
{
if($counter%2)
{
echo "\n<tr bgcolor=#F1F1F2>\n\n";
}else
{
echo "\n<tr bgcolor=#FFFFFF>\n\n";
}
$counter++;
echo "
<td>".$rowListSections['id']."</td>
<td>
<input type=text value=".$rowListSections['namn']."></td>
<td>
<input type=text value=".$rowListSections['platser']."></td>
<td>";
if($rowListSections['prio'] == 1)
{
echo "<select name=platser>
<option selected value=".$rowListSections['prio'].">".$rowListSections['prio']."</option>
<option value='0'>0</option>".$rowListSections['prio'];
}elseif($rowListSections['prio'] == 0)
{
echo "<select name=platser>
<option selected value=".$rowListSections['prio'].">".$rowListSections['prio']."</option>
<option value='1'>1</option>".$rowListSections['prio'];
}
echo "</td>
<td>
<form method=post action=thispage.php>
<input type=submit value=Delete name=del>";
</td>
</form>
</tr>";
}
echo "<form method=post action=thispage.php>
<input type=submit value=Save name=save>";
`
in your checkbox change naming as array.
<input type=checkbox name="del[]" value={$rowListSections['id']}>
like
echo $rowListSections["id"].' '.$rowListSections["namn"].' '.$rowListSections["platser"].' '.
$rowListSections["prio"].'';
and in your if(isset($_POST)) you can get a del array so you can loop this array like below.
foreach($del as $val){
$id = $val;
$sql_query_for_update = "update table_name set field = 1 where id= '$id' ";
}
I am trying to create some code to do a list of components in mysql.
Something like this
decod - 1,2,3,4,5
and to make it in drop-down listlike this
decod (dorplist) 1
2
3
4
5
I tried this one:
<?php
$mysqli = mysqli_connect("host", "username", "password", "name");
$result = mysqli_query($mysqli, 'SELECT * FROM componnets') or die(mysqli_error(mysqli));
while ($row = mysqli_fetch_array($result)) {
$org = str_replace("," , "'>", $row['fill']."");
$replace = str_replace("," , "
".$org."</option><option value='", $row['fill']."'>");
echo "<input type='hidden' name='compo' value='".$row['compon']."'>".$row['compon']."";
echo "<select name='fill'>";
echo "<option value='".$replace."'>".$replace."</option>";
echo "</select>";
}
// Free result set
mysqli_free_result($result);
/* close connection */
$mysqli->close();
?>
and do make it output to an XML
but I don't know how! I output like this
<input type='hidden' name='compo' value='DECODERS'>DECODERS
<select name='fill'>
<option value='1010'>20'>30'>40</option>
<option value='2010'>20'>30'>40</option>
<option value='3010'>20'>30'>40</option>
<option value='40'>'>1010'>20'>30'>40</option>
<option value='2010'>20'>30'>40</option>
<option value='3010'>20'>30'>40</option><option value='40'></option>
</select>
<input type='hidden' name='compo' value='DECODERS'>DECODERS
<select name='fill'>
<option value='10'>10</option>
</select>
First create the <select> outside the while loop. Then iterate over the rows to add the elements.
I'm not sure what you're trying to accomplish with your str_replace calls, so I removed them.
Something like this might get you started in the right direction:
<?php
$result = mysqli_query($mysqli, 'SELECT * FROM componnets') or die(mysqli_error(mysqli));
echo "<select name='fill'>";
echo "<input type='hidden' name='compo' value='".$row['compon']."'>".$row['compon']."";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='".$row['fill']."'>".$row['fill']."</option>";
}
echo "</select>";
mysqli_free_result($result);
$mysqli->close();
?>
I am having issues with creating radio buttons for each query result. For each line returned I want to have an extra column that consists of 2 radio buttons. the user should only be able to select 1 of these 2 buttons. Right now I have the column of radio buttons but the user can only select 1 of each radio button, not 1 for each result of the query. I hope that makes sense.
Should I be even using radio buttons? Or should I use check boxes?
<!DOCTYPE html>
<html>
<head>
<title>games</title>
</head>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<select name="weekNo">
<option value="1">week 1</option>
<option value="2">week 2</option>
<option value="3">week 3</option>
<option value="4">week 4</option>
<option value="5">week 5</option>
<option value="6">week 6</option>
<option value="7">week 7</option>
<option value="8">week 8</option>
<option value="9">week 9</option>
<option value="10">week 10</option>
<option value="11">week 11</option>
<option value="12">week 12</option>
<option value="13">week 13</option>
<option value="14">week 14</option>
</select>
<input type="submit" name="submit" value="Get Games" />
</form>
<br>
<hr>
<?php
$conn =
or die('Could not connect: ' . pg_last_error());
if(isset($_POST['submit'])) //submit button pressed
{
$query=NULL; //prevent compile error
$weekNum = $_POST['weekNo'];
$query = "SELECT a.game_no AS game_number, a.home AS home_team,
homeTeam.wins AS home_wins, homeTeam.losses AS home_losses,
a.away AS away_team, awayTeam.wins AS away_wins,
awayTeam.losses AS away_losses, a.spread AS spread
FROM weekly_stats AS a
INNER JOIN team AS homeTeam ON a.home = homeTeam.name
INNER JOIN team AS awayTeam ON a.away = awayTeam.name
WHERE a.week_no = $weekNum";
$result = pg_query($query) or die ('Query failed: ' .pg_last_error());
$query2 = "SELECT week_no, game_no FROM weekly_stats";
$result2 = pg_query($query2) or die ('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<br>There are " . pg_num_rows($result) . " records found.\n<p></p>\n";
echo "<table border=1>\n\t<tr>\n";
for($i=0; $i<pg_num_fields($result); $i++)
{
echo "\t\t<th>" . pg_field_name($result, $i) . "</th>\n";
}
echo "\t\t<th>Picks</th>\n";
echo "\t</tr>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC))
{
echo "\t<tr>\n";
foreach ($line as $col_value)
{
echo "\t\t<td>$col_value</td>\n";
}
echo "<td><input type=\"radio\" name=\"picks\" value=\"home\">Home
<input type=\"radio\" name=\"picks\" value=\"away\">Away</td>";
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
pg_free_result($result);
}
// Closing connection
pg_close($conn);
?>
</body>
</html>
The input name is the same on all of your radio buttons so the web browser is assuming they all answer the same question (Home or Away for ALL records) rather than a set of questions (Home or Away for EACH record). You need to differentiate the names for each pair of radio buttons for each row.
$row=0;
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC))
{
echo "\t<tr>\n";
foreach ($line as $col_value)
{
echo "\t\t<td>$col_value</td>\n";
}
echo "<td>"
."<label>Home <input type='radio' name='picks[$row]' value='home'></label>"
."<label>Away <input type='radio' name='picks[$row]' value='away'></label>"
."</td>";
echo "\t</tr>\n";
$row++;
}
On the server side it will interpret the submission as an array. The $row variable above could be a unique index or ID for each row rather than simply a counter as I demonstrated.
Radio buttons with the same names cannot be selected simultaneously. Radio buttons for each query should have unique names but names for two buttons of the same query should have the same name.
The code should be modified to:-
echo "<td><input type=\"radio\" name=\"picks[$row_number]\" value=\"home\">Home
<input type=\"radio\" name=\"picks[$row_number]\" value=\"away\">Away</td>";
I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The "include('connect.php');" connects to the mysql database and I know it works on separate pages. Any suggestions?
Below is the entire code.
listCustomer
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result))
{
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
}
echo "</select>";
?>
<BR>
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.
Edit
Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:
<?
//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR />
<?php
// BEGIN ADDED CONNECTION HACKY GARBAGE
$con=mysql_connect("localhost","root","root");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("sample",$con)
or die("Could not select examples");
// END ADDED CONNECTION HACKY GARBAGE
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
<BR />
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
First off, you are missing an option opening tag, as correctly mentioned by stslavik. But this is not causing the issue here as it seems (it's auto-corrected by the browser - in my tests atleast).
Secondly, this wont work (problem causer):
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
You should use
echo "<option value=".$r["Cnum"].">".$r["CName"]."</option>";
or, as I always prefer single quotes to enclose echo or print output strings:
echo '<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';
Third alternative (complex syntax: What does ${ } mean in PHP syntax?)
echo "<option value={$r["Cnum"]}>{$r["CName"]}</option>";
assuming you get data from the database try this
echo "<option value={$r['Cnum']}>{$r['CName']}</option>";
try,
echo "<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>";
instead of
echo "<option value=$r[Cnum]>$r[CName]</option>";
the below code shows a table with users to be accepted or declined. the code as it is has a dropdown on the end allowing to either accept it, deny it or leave as is. there is a button on the bottom to submit the form and after that there should be a php script that decides which user is accepted, denied or still pending.
what would be the best approach to make this work since i find it hard to link the dropdown box and the id.
<table align='center' width='90%'>
<tr>
<center><td><strong>ID:</strong></td></center>
<center><td><strong>Name:</strong></td></center>
<center><td><strong>Level:</strong></td></center>
<center><td><strong>Job:</strong></td></center>
<center><td><strong>Guild:</strong></td></center>
<center><td><strong>Date:</strong></td></center>
<center><td><strong>Action:</strong></td></center>
</tr>
<?php
$id = 0;
$result=mysql_query("SELECT * FROM accounts WHERE active ='0' ORDER BY date LIMIT 10") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$id = $id + 1;
?>
<form method='POST' action=''>
<tr>
<center><td><?php echo $row['id']; ?></td></center>
<center><td><?php echo $row['user']; ?></td></center>
<center><td><?php echo $row['level']; ?></td></center>
<center><td><?php echo $row['job']; ?></td></center>
<center><td><?php echo $row['guild']; ?></td></center>
<center><td><?php echo $row['date']; ?></td></center>
<td>
<select name='action_<?php echo $row['id']; ?>'>
<option value='none'>None</option>
<option value='accept'>Accept</option>
<option value='decline'>Decline</option>
</select>
</td>
</tr>
<?php } ?>
<tr>
<br /><td align='right'><input type='submit' value='Submit' name='submit' /></td>
</tr>
</form>
</table>
<?php
if(isset($_POST['submit'])) {
if(isset($_POST['action_'.$row['id'].'']) && $_POST['action_'.$row['id'].''] == "accept" ) {
$acc = mysql_query("UPDATE `accounts` SET `active`='1' WHERE `id` = '".$row['id']."'");
echo "<meta http-equiv=refresh content=\"5; url=?wesofly=main&page=recruitment\">";
}elseif(isset($_POST['action_'.$row['id'].'']) && $_POST['action_'.$row['id'].''] == "decline" ) {
$dec = mysql_query("DELETE * from `accounts` WHERE '".$row['id']."'");
echo "<meta http-equiv=refresh content=\"0; url=?wesofly=main&page=recruitment\">";
}
}
?>
Your code is confusing. Your WHILE loop starts before the form tag, but the WHILE loop closes before the form tag closes. This means that you are going to have multiple form elements.
So, first of all, change that in your code. Next the way you want this to work is using arrays.
Your drop down box should look like this
<select name='action[<?php echo $row['id']; ?>]'>
<option value='none'>None</option>
<option value='accept'>Accept</option>
<option value='decline'>Decline</option>
</select>
Then on the PHP side, you should be able to do something like
foreach ($_REQUEST['action'] as $key => $value) {
$id = $key;
$status = $value;
// put your code here to update the specific database item
}
I think you'll be better off with HTML form arrays, for instance:
<select name="action[<?php echo $row['id']; ?>]">
<option value="none">None</option>
<option value="accept">Accept</option>
<option value="decline">Decline</option>
</select>
And inside your php code, you would simply loop through the submitted action field in your $_POST array to find which id had which action. Each entry would be presented as an array, the key would be the user's id.