Independent radiobuttons for each row read from server - php

I'm currently trying to generate independent radio buttons for each row read from my server. They are supposed to symbolize ON and OFF Switches. My current issue is that I manage to generate buttons for each row but these are not independent. I can only turn one off or on at a time.
I know that radio buttons become truly "independent" when the name of each is different. But I can't seem to figure out a way to create a new name for each row generated.
Currently my code looks like this
res = $db->query($query);
if ($res->num_rows > 0) {
while ($row = $res->fetch_object()) {
$query = "SELECT * FROM device_status
WHERE device = ($row->id)";
$status = $db->query($query);
$device_status = $status->fetch_object();
$content .= <<<END
<tr>
<td>{$row->devices}</td>
<td>{$row->description}</td>
<td>{$row->room}</td>
<td><input type='radio' name='radiobutton' id='myCheck'></td>
<td><input type='radio' name='radiobutton' id='myCheck' checked='checked'></td>
<!-- <td>{$device_status->status}</td> -->
<td>Delete
Edit</td>
</tr>
END;
}
}
echo $content;
I've been trying different methods such as using <td><input type='radio' name='$row->id' id='myCheck'></td>
But have not gotten it to work. Any help on how to solve this issue is greatly appreciated.
Thank you!

If you want the unique names for all the checkboxes generated you can have them by just incrementing $i for each row.
res = $db->query($query);
if ($res->num_rows > 0) {
$i = 0;
while ($row = $res->fetch_object()) {
$query = "SELECT * FROM device_status
WHERE device = ($row->id)";
$status = $db->query($query);
$device_status = $status->fetch_object();
$content .= <<<END
<tr>
<td>{$row->devices}</td>
<td>{$row->description}</td>
<td>{$row->room}</td>
<td><input type='radio' name='radiobutton<?php echo $i?>' id='myCheck'></td>
<td><input type='radio' name='radiobutton<?php echo $i?>' id='myCheck' checked='checked'></td>
<!-- <td>{$device_status->status}</td> -->
<td>Delete
Edit</td>
</tr>
END;
$i++;
}
}
echo $content;

If you do not care about the name of the radio button, you could use a "php random number generator", each row you asign a new random number to your radio button name:
<?php $x = rand(5, 100000); ?> <input type='radio' name='<?php echo $x; ?>' id='myCheck'>

Related

Can't save table information to MySQL

I have been working with this code for a whole day but still can't understand where I made a mistake. So basically I want to edit whole mysql table.
I'm easily fetching it, and I need to change some of the data and save it again to the table. But everytime I'm trying to save it - everywhere getting "Array" and nothing more.
It's look like the code is not seeing that I'm making changes there.
Also sometimes when I'm changing bits, only last raw was saving and duplicating everywhere after.
<form method="POST" action="index.php">
<table><thead><tr><th>ID</th><th>Name</th><th>Live</th><th>AQ</th><th>Up</th><th>Down</th><th>Cptch</th><th>VID</th><th>UpOrDo</th><th>UTool</th><th>STool</th></tr><thead><tbody>
<?php
while ($row = $result->fetch_array()) {
echo "<tr><td><input size='1' name='id[]' value='".$row["ID"]."' readonly></td><td>".$row["Name"]."</td><td>".$row["Live"]."</td><td>".$row["AccQuantity"]."</td><td>".$row["Upvote"]."</td><td>".$row["Downvote"]."</td><td>".$row["Captcha"]."</td><td><input name='VoteID[]' type='text' size='5' value='".$row["VoteID"]."'></td><td><input name='UpOrDown[]' type='text' size='5' value='".$row["UpOrDown"]."'></td><td><input name='UpvoteTool[]' type='text' size='5' value='".$row["UpvoteTool"]."'></td><td><input name='SignupTool[]' type='text' size='5' value='".$row["SignupTool"]."'></td></tr>";
}
?>
</tbody>
</table>
<input type='submit' name='update' value='UPDATE' />
</form>
</body>
</html>
<?php
if(isset($_POST['update'])){
$ID = $_POST['id'];
$VoteID = $_POST['VoteID'];
$UpOrDown = $_POST['UpOrDown'];
$UpvoteTool = $_POST['UpvoteTool'];
$SignupTool = $_POST['SignupTool'];
for($i = 1; $i < 7; $i++) {
$user_id = $ID[$i];
$sql = "UPDATE Servers SET `VoteID`='".$VoteID."', `UpOrDown`='".$UpOrDown."', `UpvoteTool`='".$UpvoteTool."', `SignupTool`='".$SignupTool."' WHERE `ID`=".$user_id."";
echo $sql."<br>";
}
}
It seems you are not running the query. Also you are not picking out the data from the array. I would do this:
for($i = 1; $i < 7; $i++) {
$user_id = $ID[$i];
$_VoteID = $VoteID[$i];
$_UpOrDown= $UpOrDown[$i];
$_UpvoteTool= $UpvoteTool[$i];
$_SignupTool= $SignupTool[$i];
$sql = "UPDATE Servers SET `VoteID`='".$_VoteID."', `UpOrDown`='".$_UpOrDown."', `UpvoteTool`='".$_UpvoteTool."', `SignupTool`='".$_SignupTool."' WHERE `ID`=".$user_id."";
mysqli_query($sql);//or whatever way you are runing your query.
echo $sql."<br>";
}

Select multiple options from option

So I have a selector which gets its information from a database.
When I select something from the selector and press: Add to list, it generates a table with the selected information.
Now this what it should do. But now, when I select another result and press Add to list. It removes the old one and replaces it with the new one.
But I actually don't want it to remove the old one, but make a new row under it. So that table gets bigger. How do I do this?
Code for selector:
<!--Selector-->
<?php
//Get name and id data from the db. In an assoc array
$results = $database->Selector();
echo "<form name='form' method='POST' id='selector'>";
echo "<select name='train_name' id='train_name' multiple='multiple'>";
// Loop trough the results and make an option of every train_name
foreach($results as $res){
echo "<option value=" . $res['train_name'] . ">" . $res['train_name'] . "</option>";
}
echo "</select>";
echo "<br />" . "<td>" . "<input type='submit' name='Add' value='Add to list'/>" . "</td>";
echo "</form>";
if(isset($_POST["train_name"])){
//Get all data from database, in an assoc array
$results = $database->getAllAssoc();
//Make table headers
?>
<div id="train_select_table">
<form name="selector" method="post" action="customer_list.php?user_id=<?php echo $_SESSION['user_id']?>">
<table>
<tr>
<th>Train name</th>
<th>Number of bogies</th>
<th>Number of axles</th>
<th>Delete</th>
<th>More info</th>
<th>Check</th>
<!--Only for admins (later!)-->
<!--<th>XML</th>
<th>SQL</th> -->
</tr>
<div id="looprow">
<?php
foreach($results as $res){
//Loop trough results, generate a tablerow every time
?>
<tr>
<td name="train_name"><?php echo $res['train_name']?></td>
<td><?php echo $res['number_of_bogies']?></td>
<td><?php echo $res['number_of_axles']?></td>
<td>Delete</td>
<td>More Information</td>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $res['train_id']?>"></td>
<!--Only for admins (later!)-->
<!--<td>XML</td>
<td>SQL</td>-->
</tr>
<?php
}
?>
</div>
</table><br />
<input name="Add to list" type="submit" id="add_to_list" value="add_to_list">
</form>
</div>
<?php
}
?>
Function:
function getAllAssoc() {
$sql = "SELECT * FROM train_information WHERE train_name = :train_name";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":train_name", $_POST["train_name"]);
$sth->execute();
return $sth->fetchAll();
}
function selector() {
$sql = "SELECT train_name, train_id FROM train_information";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
I understand why it keeps replacing the old row. this is because I send a new query. but I don't know how to keep the old one.
as each time user will chose a single value while not mutiple value, so if you want to use the new value and the old value too, you should store the old value using cookie or session or hidden input in that form. It depends on you.
E.g. at the beginning, do this:
<?php
session_start();//make it be before your output if you want to use session or cookie
if(!$_SESSION['train_name']) $_SESSION['train_name'] = array();
if(isset($_POST["train_name"])) $_SESSION['train_name'] = array_merge ($_SESSION['train_name'], $_POST["train_name"]);
.......
//next step, inquiry with $_SESSION['train_name']
Simply change the name of your multiple select list from train_name to train_name[] as follows:
...
echo "<select name='train_name[]' id='train_name' multiple='multiple'>";
...
By this way your $_POST['train_name'] will be an array passed to bindParam

php modify database from html table select by checkbox

Good morning everybody
I made a table in html that contains users and the select box contains profiles.
I want to assign every users profile in database according to the profile selected in html.
My table in html contains checkbox next to every user. I want every user that is checked to be assigned.
<form action="PHP/modifier.php" method="POSt">
<table>
<thead>
<tr>
<th><input type="checkbox" id="checker" name="all"></th>
<th>Username</th>
<th>Nom</th>
<th>Prenom</th>
</tr>
</thead>
<?php
$query="SELECT Username,Nom,Prenom from user";
$result = mysql_query($query);
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++){
$row =mysql_fetch_array($result);
$Username = $row['Username'];
$Nom = $row['Nom'];
$Prenom = $row['Prenom'];
echo "<tr id='row'>";
echo "<td><input type='checkbox' id='check' name='check'".$Username."></td>";
echo"<td>".$Username."</td>";
echo "<td>".$Nom."</td>";
echo "<td>".$Prenom."</td>";
echo "<td><div id='MAJ'>";
echo "</div></td>";
echo "</tr>";
}
?>
<h2>Selectionner l'utilisateur et le profile</h2>
<select name="profile" id="profilez" required>
<option>Injecteur</option>
<option>Utilisateur</option>
<option>Administrateur</option>
<option>Utilisateur superieur</option>
<input type="submit" value="Associer">
</form>
In the modifer.php
I want to make update function to update users but how to get information from checked cases in the html table?
I already wrote this
<?php
include "functions.php";
$profile=$_POST['profile'];
$Username=$_POST['Username'];
$query="UPDATE user SET profile = '$profile' WHERE Username = '$Username' " ;
?>
I don't know how to do it.
thank you so much and sorry for my bad english.
there are different ways to do it. but lets go with your way
on you modifer.php page make another query to get all user names
$row =mysql_fetch_array($result);
foreach ($row as $value)
{
if (isset($_POST['check'.$value['username']]))
{
$profile = $_POST['profile'];
$Username = $POST['check'.$value['username'];
$query="UPDATE user SET profile = '$profile' WHERE Username = $value['username'] " ;
}
}

Updating an HTML combo box from MySQL DB using PHP

need some help here. It's a very simple web app i'm developing but just needed some help with something.
Here's what's setup. I have a html form with one combo box. All I need is to update this combo box with the entries from a mysql table named 'supplier'. The input to this table 'supplier' is via another form on my website which i've already setup. I need help in auto updating this combo box from the table 'supplier'. Please let me know the php code for it. I've included my code as well. Thanks in advance! I have included the html form as well.
replace your code
$result = mysql_query("SELECT supplier FROM supplier");
while($row = mysql_fetch_array($result))
{
/*echo '<form action="">';*/
echo "<select name='supplier'>";
echo "<option value = '$row[supplier]'>""</option>";
echo "</select>";
with
$result = mysql_query("SELECT supplier FROM supplier");
echo "<select name='supplier'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = '".$row[supplier]."'>".$row[supplier]."</option>";
}
echo "</select>";
So,
what is the problem? Is your script not working or do you want a active Combobox on one screen to be updated, when on another screen a new entry for supplier is entered?
Ok, some hints:
The "select"-Tag should be placed outside the loop.
Put the column name in "
Add a display-Value for the options
So, without checking it:
/*echo '<form action="">';*/
echo "<select name='supplier'>";
while($row = mysql_fetch_array($result))
{
echo "<option value = '".$row["supplier"]."'>".$row["supplier"]."</option>";
}
echo "</select>";
Your problem is here:
$result = mysql_query("SELECT supplier FROM supplier");
while($row = mysql_fetch_array($result))
{
/*echo '<form action="">';*/
echo "<select name='supplier'>";
echo "<option value = '$row[supplier]'>""</option>";
echo "</select>";
You're creating the drop down box (the Select) inside of the mysql data loop. As #Hitesh has explained. You need to create this outside of the loop and only echo out the data results within. For example:
$result = mysql_query("SELECT supplier FROM supplier");
echo "<select name='supplier'>";
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row['supplier'].">".$row['supplier']."</option>";
}
echo "</select>";
This will output your drop down box, with all your supplier names as the value and the displayed text options.
If you attempted to do the following:
$result = mysql_query("SELECT supplier FROM supplier");
while($row = mysql_fetch_array($result))
{
/*echo '<form action="">';*/
echo "<select name='supplier'>";
echo "<option value = '$row[supplier]'>""</option>";
echo "</select>";
}
You would just end up with as many drop down boxes as you had suppliers in your database. This is because you're creating a new Select box for each record found.
Also, without specifying the second $row['supplier'] between the option tags, you'd just end up with a blank (empty) drop down box.
Hope this helps.
Here is complete code:
PHP Code:
<?php
// select box open tag
$selectBoxOpen = "<select name='supplier'>";
// select box close tag
$selectBoxClose = "</select>";
// select box option tag
$selectBoxOption = '';
// connect mysql server
$con = mysql_connect("localhost","user","pass");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
// select database
mysql_select_db("rtgs", $con);
// fire mysql query
$result = mysql_query("SELECT supplier FROM supplier");
// play with return result array
while($row = mysql_fetch_array($result)){
$selectBoxOption .="<option value = '".$row['supplier']."'>".$row['supplier']."</option>";
}
// create select box tag with mysql result
$selectBox = $selectBoxOpen.$selectBoxOption.$selectBoxClose;
?>
HTML Code:
<form action="ntxn.php" method="post">
<table>
<tr>
<td>Supplier Name:</td>
<td>
<?php echo $selectBox;?>
</td>
</tr>
<tr>
<td>Bill No. :</td>
<td><input type ="text" name="billno"/></td>
</tr>
<tr>
<td>Bill Date : </td>
<td><input type="date" name="billdate"/></td>
</tr>
<tr>
<td>Bill Amount : </td>
<td><input type="text" name="billamt"/></td>
</tr>
<tr>
<td>NEFT / RTGS No. :</td>
<td><input type="text" name="rtgs"/></td>
</tr>
<tr>
<td><input type="submit" name="Save"/></td>
</tr>
</table>
</form>

php checkbox from mysql

I've managed to pull records from a mysql database using php with a checkbox to select. I'm struggling to make the selected records (with the checkboxes) appear on a new page. Here is my code so far:
<?php
include('connect.php');
$query = 'SELECT * FROM grades';
if ($r = mysql_query($query)) {
print "<form>
<table>";
while ($row = mysql_fetch_array($r)) {
print
"<tr>
<td>{$row['InstitutionName']}</td>
<td>{$row['InstitutionAddress']}</td>
<td>{$row['SubjectArea']}</td>
<td><input type='checkbox' name='check[$row{['GradeID']}] value='check' /></td>
</tr>";
}
print "</table>
</form>";
$checkbox[] = isset($_POST['checkbox']) ? true : false;
} else {
print '<p style="color: blue">Error!</p>';
}
?>
<html>
<form action="check2.php" method="POST">
<input type='submit' name='Submit' value='Submit'>
</html>
And on the page where I want the selected records to display, I have:
<?php
if(isset($checkbox))
{
foreach($checkbox as $value)
{
echo $value."<br>"; //it will print the value of your checkbox that you checked
}
}
?>
Any assistance would be appreciated!
Put checked="checked" just like you have put value="check":
<td><input type='checkbox' name='check[$row{['GradeID']}] value='check' checked="checked" /></td>
Edit:
Probably I misunderstood you. What do you expect to see?
First, you should make the form to perform POST request instead of GET (default):
print "<form method='POST' action='THE_FILE_YOU_PRINT_RESULTS.php'>"
Then in "THE_FILE_YOU_PRINT_RESULTS.php" do a print_r($_POST); to see what you get and how to display it.
A better way to do this would be to name your checkboxes check[]. Each checkbox value should then be the ID (rather than check).
Then on your results page, just loop through each instance of check[] and print the value out.
check[$row{['GradeID']}]
Seems incorrect to me, should it be:
check[{$row['GradeID']}]
Notice the moved opening curly bracket to before the $
Sorry if this is wrong haven't used PHP in long time but it stood out for me
The are a couple of error in the form print function.
You have to put the action on the first form, not on the second, you have to change the way how you print the checkbox as well.
Try to print the form in this way:
<?php
include('connect.php');
$query = 'SELECT * FROM grades';
if ($r = mysql_query($query)) {
print "
<form action=\"check2.php\" method=\"POST\">
<table>";
while ($row = mysql_fetch_array($r)) {
print
"<tr>
<td>{$row['InstitutionName']}</td>
<td>{$row['InstitutionAddress']}</td>
<td>{$row['SubjectArea']}</td>
<td><input type='checkbox' name='check[".$row['GradeID']."] value='".$row['GradeID']."' /></td>
</tr>";
}
print "</table>
<input type='submit' name='Submit' value='Submit'>
</form>";
$checkbox[] = isset($_POST['checkbox']) ? true : false;
} else {
print '<p style="color: blue">Error!</p>';
}
?>
And print the checked box reading the _REQUEST array:
<?php
if(isset($_REQUEST["check"]))
{
foreach($_REQUEST["check"] as $key=>$value)
{
echo $key."<br>"; //it will print the value of your checkbox that you checked
}
}
?>
This should work.

Categories