Uniquely identifying MySQL entries - php

I'm stumped. Here's my relevant code:
while($data = mysql_fetch_array($getdata)){
$myid = $data["myid"]; //unique entry identifier for table rows
$method = $data["method"];
$category = $data["category"];
$company = $data["company"];
$datestarted = $data["datestarted"];
$initialfee = $data["initialfee"];
$ongoingfee = $data["ongoingfee"];
echo "</tr>";
echo "<tr>";
echo "<td class='mymenu' width='14%' height='60px'>";
echo $method;
echo "</td>";
echo "<td class='mymenu' width='20%' height='60px'>";
echo $category;
echo "</td>";
echo "<td class='mymenu' width='20%' height='60px'>";
echo $company;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $datestarted;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $initialfee;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $ongoingfee;
echo "</td>";
echo "<td class='mymenu' width='10%'>";
echo "<input type='submit' name='' class='button' value='Remove'>";
echo "</td>";
}
What's happening here, is that a table is going to be displayed and each row in that table will look as coded above. There could be any number of entries, but probably in a range of 1-6. At the end of each entry is a remove button. Once clicked, the user is forwarded to a page where that entry will then be subject to removal from the table. My problem is identifying which entry the user wants to remove based on which "remove" button they clicked. If the number of rows weren't dynamic, I could just give each input button a unique name ahead of time and that could help. How can I uniquely identify what row I need to remove on the removal page after a remove button is clicked? I can't use sessions because the session variable $myid would just change each time a new row were made. Pulling from a database won't help obviously because I lack a unique identifier on the following page.
I have an idea of setting the name of each button equal to each unique $myid, which would make that unique $myid the only $_POST variable that would travel to the removal page depending on the button pressed. On the removal page I would just set $myid equal to that post variable. However, even if it's the only post variable available, how do I reference it to be equal to $myid on the removal page without knowing the name of it? Because I won't know the name of the $_POST variable that traveled to the removal page.
The answer's probably simple, but for whatever reason it isn't coming to me on this night.
Thanks for your help. Honestly don't know what I would do without this forum.

Create a new form for each row, with a hidden field containing the ID of the record you want to delete.
echo "<form>";
echo "<td class='mymenu' width='10%'>";
echo "<input type='hidden' value='$id'>";
echo "<input type='submit' name='' class='button' value='Remove'>";
echo "</td>";
echo "</form>";

At first I want to say your tr tag is not well placed, and create a form to solve your problem
Try this:
while($data = mysql_fetch_array($getdata)){
$myid = $data["myid"]; //unique entry identifier for table rows
$method = $data["method"];
$category = $data["category"];
$company = $data["company"];
$datestarted = $data["datestarted"];
$initialfee = $data["initialfee"];
$ongoingfee = $data["ongoingfee"];
echo "<tr>";
echo "<td class='mymenu' width='14%' height='60px'>";
echo $method;
echo "</td>";
echo "<td class='mymenu' width='20%' height='60px'>";
echo $category;
echo "</td>";
echo "<td class='mymenu' width='20%' height='60px'>";
echo $company;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $datestarted;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $initialfee;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $ongoingfee;
echo "</td>";
echo "<td class='mymenu' width='10%'>";
echo "<form>";
echo "<input type='hidden' value='".$id."'>";
echo "<input type='submit' name='' class='button' value='Remove'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}

try this #Lock forgot to put " in form.
<?php
echo "<form>";
echo "<table><tr>";
echo "<td class='mymenu' width='10%'>";
echo "<input type='hidden' value='$id'>";
echo "<input type='submit' name='' class='button' value='Remove'>";
echo "</td>";
echo "</tr></table>";
echo "</form>";
?>

Related

How to set a table cel color based on its value in PHP?

<?php
$res=mysqli_query($con,"select * from tab_cont");
while($rows = mysqli_fetch_assoc($res))
{
echo "<tr>";
echo "<td>"; echo $rows["id"]; echo"</td>";
echo "<td>";echo $rows["date"]; echo "</td>";
echo "<td>";echo $rows["category"];echo "</td>";
echo "<td>";echo $rows["issue"];echo "</td>";
echo "<td>";echo $rows["cr_status"];echo "</td>";
echo "<td>";echo $rows["priority"];echo "</td>";
echo "<td>"; echo $rows["assigned"]; echo "</td>";
echo "<td>"; echo $rows["escalation"]; echo "</td>";
echo "<td>"; echo $rows["action"]; echo "</td>";
echo "<td>"; echo $rows["resol"]; echo "</td>";
echo "<td>"; ?> <a href="edit.php?id=<?php echo $rows["id"];?>"><button type="button" class=btn btn-success">Edit</button> <?php echo "</td>";
echo "<td>"; ?> <a href="delete.php?id=<?php echo $rows["id"];?>"><button type="button" class=btn btn-danger">Delete</button> <?php echo "</td>";
}
?>
Here I want to set the color for cr_status based on its value "open:green" and "red:close".I tried foreach() code, after echo $rows["cr_status"];echo..... . But I got "unexpected foreach()" error in the browser. The code I used is:
foreach($rows["cr_status"] as $cell)
{
if($cell=="OPEN")
{
echo '<td style="background:#FF6347">'.$cell.'</td>';
}
elseif ($cell=="CLOSE")
{
echo '<td style="background:#228B22">'.$cell.'</td>';
}
}
Can anyone, Plz help me with this code?
$rows["cr_status"] isn't an array, so it makes no sense to loop through it. That's the reason for your error. It's not quite clear what you thought this code was supposed to be doing.
If you just want to set the colour of the cr_status cell (and open/close are the only two statuses), then
echo "<td style='background:".($rows["cr_status"] == "OPEN" ? "#FF6347" : "#228B22")."'>".$rows["cr_status"]."</td>";
should do what you need.

Getting database data into a html table

I need to get the data of the database to an HTML table.
There were similar questions asked before but none of them were similar to my requirement.
I need to get the first row of the database to the first row of the table.
So I did this.
echo "<table id='main-table' border='1px solid'> <thead> ";
echo "<tr>";
echo "<th>Board No</th>";
echo "<th>Number</th>";
echo "</tr></thead><tbody>";
while($query->fetch()){
echo "<form action='' class='' method='post'><tr>";
line 55 --> echo "<td>" . Board Code:".$row["board_code"] . Number:".$row["status"] . "</td>";
echo "</tr> </form>";
}//End of While
echo "</tbody></table>";
There is a syntax error in line 55 as mentioned above in the code.
Check this code:
echo "<table id='main-table' border='1px solid'> <thead> ";
echo "<tr>";
echo "<th>Board No</th>";
echo "<th>Number</th>";
echo "</tr></thead><tbody>";
while($query->fetch()){
echo "<form action='' class='' method='post'><tr>";
echo "<td> Board Code:".$row["board_code"]. "</td>";
echo "<td>Number:".$row["status"]. "</td>";
echo "</tr> </form>";
}
echo "</tbody></table>";
"<td>" . Board Code:".$row["board_code"] ." Number:".$row["status"] . "</td>";
edit your code like if it does not work please share your whole code will help you for sure
Check with this code
echo "<form action='' class='' method='post'><tr>";
line 55 --> echo "<td>" . Board Code:".$row["board_code"] ." Number:".$row["status"] . "</td>";
echo "</tr> </form>";

Update sql table of only rows where checkbox is checked php

I have a table of values that I am trying to update only the date of. I have a checkbox that I want only the select rows to updated when its checked and user clicks the submit button. The issue I am having is let's say I have 4 records and I only want record 1,2 and 4 updated. What is happening is records 1,2 and 3 are being updated. I know it has something to do with the array in the foreach loop. Side note I know the sql code allows for injection I am trying to get this to work first before worrying about that. Thank you for any and all help and time.
Table Code
$sql = "SELECT * FROM tblUnitMaster WHERE JobNumber = '" . $JobNumber . "' AND `EngReleaseDate` IS NULL OR `EngReleaseDate` = '' ORDER BY UnitID";
$result=$conn->query($sql);
echo "<style>";
echo "th {";
echo "background-color: #6EB4FF;";
echo "color: white;";
echo "}";
echo "</style>";
echo "<form action=pmReleaseEngineering3.php method=post>";
echo "<input type=hidden name=JobNumber value=$JobNumber />";
echo "<table border=2 cellpadding=4 cellspacing=10>";
echo "<tr>";
echo "<th><b><font size=4></font></b></th>";
echo "<th><b><font size=4>"."Job"."</font></b></th>";
echo "<th><b><font size=4>"."Code"."</font></b></th>";
echo "<th><b><font size=4>"."Model Number"."</font><?b></th>";
echo "<th><b><font size=4>"."Serial Number"."</font><?b></th>";
echo "<th><b><font size=4>"."Scope"."</font></b></th>";
echo "<th><b><font size=4>"."Date"."</font></b></th>";
echo "</tr>";
while ($row=$result->fetch_array())
{
echo "<tr>";
//echo "<td><font color=silver size=4>".$row[0]."</font></td>"; // Unit Number
echo "<td><input type=checkbox name=check[] value=".$row[0]." checked></td>";
echo "<td><input name=UnitID type=text font color=silver size=3 value=".$row[0]." readonly/></td>"; // Unit Number
echo "<td><font color=silver size=4>".$row[5]."</font></td>"; // Job Code
echo "<td><font color=silver size=4>".$row[2]."</font></td>"; // Model Number
echo "<td><font color=silver size=4>".$row[3]."</font></td>"; // Serial Number
echo "<td><font color=silver size=4>".$row[4]."</font></td>"; // Scope
echo "<td><input name=EngReleaseDate size=6 type=datetime value=" . date('Y-m-d'). " /></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
echo "<button class=button style=vertical-align:middle><span>Submit</span></button>";
echo "</form>";
echo "<br>";
PHP SQL Update TBL Code
include('pmconnect.php');
$JobNumber=$_REQUEST['JobNumber']; //array
$UnitID=$_POST['UnitID']; // array
$EngReleaseDate=$_POST['EngReleaseDate']; // array
if(isset($_POST['check'])){
//if(!empty($_POST['check'])) {
foreach($_POST['check'] as $a => $B){
$sql = "UPDATE tblUnitMaster SET " .
"EngReleaseDate='" . $EngReleaseDate . "'" .
"WHERE UnitID='" . $UnitID . "'";
//$result=$conn->query($sql);
if ($conn->multi_query($sql) === TRUE)
echo "<table border=1 cellpadding=4 cellspacing=10>";
echo "<tbody>";
echo "<tr>";
echo "<td><b><font size=4>Unit ID</font></b></td>";
echo "<td><b><font size=4>Eng. Release</font></b></td>";
echo "</tr>";
echo "<tr>";
echo "<td><font size=4> $UnitID </font></td>";
echo "<td><font size=4> $EngReleaseDate </font></td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
echo"<br>";
}
}

How to random the choices in php

I am creating a random generated quiz with 10 questions. In generating the random question is fine, but I want to display also the choices in random, same with the questions.
This is my code that I am currently working with:
<?php
generate();
function generate(){
include('connection.php');
mysql_select_db('exam');
$result=mysql_query("SELECT * FROM questionaires
INNER JOIN choices ON questionaires.q_id=choices.q_id
WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires)
ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>
";
while($row = mysql_fetch_array($result)){
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
echo "<input type='radio' name='ans'>".$row['choice_a']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_b']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_c']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_d']."</input><br>";
echo "</td>";
echo "</tr>";
//}
//}
}
echo "</table>";
}
?>
Little help will highly appreciated.
You could change script to this:
echo "<td>";
echo $row['question'] . "<br>";
$ans=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans);
foreach ($ans as $choice) {
echo "<input type='radio' name='ans'>".$choice."</input><br>";
} unset($choice);
echo "</td>";

Unique radio name in php while loop

Well, I've following Php code which generate a table with radio button so that user can select only one radio button in EVERY row. But to select only one radio button in Every row it's must be a unique name in EVERY row, Right ? But I can't get any idea how do i set a unique name in while loop. Can you guys give me any idea or solutions ?
Php code:
$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";
echo "<form method='post' action='$action' name='attendence'/>";
echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'>Check All <input type= 'checkbox'
onclick='checkAll(this)' /></td>";
echo "</tr>";
while($res2 = mysql_fetch_array($sql2))
{
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>
<input type='radio' name='ch[]' value='1' /> <input type='radio' name='ch[]'
value='1' />
</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
}
echo "<tr>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit'
class='submit' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
Thanks for your help.
Update:
echo "<td class='tdhead2' valign='top'>";
echo '<input type="radio" name="ch'.$counter++.'[]" value="1">';
echo " ";
echo '<input type="radio" name="ch'.$counter.'[]" value="0">';
echo "</td>";
you can set a variable counter:
$counter = 1;
while($res2 = mysql_fetch_array($sql2)){
echo '<input type="radio" name="ch'.$counter++.'[]" value="'.$counter++.'">';
}
Instead of making the name just ch[] you can make the name ch[SOME_UNIQUE_VALUE] for each row. Usually something like an id number is used. Perhaps $roll if that is unique to each student. That will also allow you to know which id the radio button pertains to. On form submit, you can foreach($_POST['ch'] as $id=>$value) and get the id out.
Edit:
Example code:
//start the counter variable
$counter = 1;
while($res2 = mysql_fetch_array($sql2))
{
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>";
//echo each radio with the counter for this row
echo "<input type='radio' name='ch_{$counter}[]' value='0' /> ";
echo "<input type='radio' name='ch_{$counter}[]' value='1' />";
echo "</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
//add one to the counter
$counter++;
}
Please note, unless you do like I suggested above, you will not be able to know which student each radio group goes to. You can assume that the radio buttons on group row 1 belongs to the student in row 1, but you might not know which student is in row 1. Which is why I suggest you put a unique id that goes with each student with each radio button instead of a counter.
You can store a counter $count and increase it after each iteration. Or if you have some unique id from the database per row (for example the primary_key), you can use that instead.$id = $res['id']
Then concat that value to the name=ch".$id."...
you should use student name as an unique array and put that value in the name=""
and it will auto increment according to the name and you can only choose single option at single row.
try this :
<input type='radio' name='<?php echo $res2['sname'];?>' value='0' /> ";
<input type='radio' name='<?php echo $res2['sname'];?>' value='1' />";
this solutio will work.because it work's for me in an attendance system.

Categories