I have a table generated dynamically from a database named 'submittbl'. Each row has a checkbox generated with it dynamically.
I want to be able to insert rows checked into database named 'marktbl'. What I have tried so far only gets the first row saved to database instead of the row I check/select. But if I check all rows, all the rows gets saved properly. How can I insert the checked rows?
I have tried many solutions haven't worked.
//to get records from database dynamically
<?php
if(isset($_POST['find_btn']))
{
if(empty($_POST['s_lev']) && empty($_POST['s_sem']))
{
$msgerr = "<b>Notice: </b>" . "No categories selected.";
}
else
{
$selev=$_POST['s_lev'];
$sesem=$_POST['s_sem'];
$sql = "SELECT *FROM submittbl WHERE CourseCode = ? AND Level = ? AND Semester = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sss",$pcode, $plevel, $psems);
$pcode = $_SESSION['ccode'];
$plevel = $selev;
$psems = $sesem;
$stmt->execute();
$result = $stmt->get_result();
$num_rows = $result->num_rows;
if($num_rows>0)
{
while($rows = $result->fetch_assoc())
{
$sn = $rows['SN'];
$name = $rows['StudentName'];
$regno = $rows['RegNo'];
$asmid = $rows['AssignmentID'];
$code = $rows['CourseCode'];
$lev = $rows['Level'];
$sem = $rows['Semester'];
$det = $rows['SubmissionDate'];
$doc = $rows['Document'];
$score = $rows['Score'];
?>
<tr>
<td><input type='checkbox' name='delChk[]' value='<?php echo $sn; ?>'><?php echo $sn; ?></input></td>
<td><?php echo $name; ?></td>
<td><?php echo $regno; ?></td>
<td><?php echo $asmid; ?></td>
<td><?php echo $det; ?></td>
<input type='hidden' name='sn' value='<?php echo $sn; ?>'></input>
<input type='hidden' name='name[]' value='<?php echo $name; ?>'></input>
<input type='hidden' name='regno[]' value='<?php echo $regno; ?>'></input>
<input type='hidden' name='asmid[]' value='<?php echo $asmid; ?>'></input>
<input type='hidden' name='ccode[]' value='<?php echo $code; ?>'></input>
<input type='hidden' name='level[]' value='<?php echo $lev; ?>'></input>
<input type='hidden' name='semester[]' value='<?php echo $sem; ?>'></input>
<input type='hidden' name='det[]' value='<?php echo $sub; ?>'></input>
<input type='hidden' name='doc[]' value='<?php echo $doc; ?>'></input>
<input type='hidden' name='score[]' value='<?php echo $score; ?>'></input>
<td><?php echo "<a href='download_submitted.php?id=$asmid'><img src=img/downloads.png width=10% height=10% alt='download'>Download</a>"; ?></td>
</tr>
<?php
}
}
else
{
$msgerr = "<b>Notice: </b>" . "Oops! No records found for the selected categories.";
}
}
}
?>
//to insert into markedtbl database
//this is where I'm having the whole problem
<?php
if(isset($_POST['upload']))
{
if(!isset($_POST['delChk']))
{
$msgerr = "<b>Notice: </b>" . "No records selected - select/mark records to upload.";
}
else
{
//
so I will try to write you a sample of your idea, hope will works. so you have array of users for my example with four keys
$users = [
0 => [
'selected' => true,
'name' => 'Name 1',
],
1 => [
'selected' => false,
'name' => 'Name 2',
],
2 => [
'selected' => true,
'name' => 'Name 3',
],
3 => [
'selected' => false,
'name' => 'Name 4',
],
];
foreach ($users as $user) {
if (!$user['selected]) {
continue;
}
$sql = "INSERT INTO marktbl(
NSN,
StudentName,
RegNo,
AssignmentID,
CourseCode,
Level,
Semester,
DateSubmitted,
Document,
Score
) VALUES(
:name,
:lastname,...
)";
$stmt = $conn->prepare($sql);
$params = [
':name' => $user['name'],
':lastname' => $user['lastname'],...
];
try {
$stmt->execute($params);
} catch(Exception $e) {
echo $e->getMessage();
}
}
I think your code should look like this. You can easy set a flag for every selected element and then filter data by this flag.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.