i want update multiple rows my table sql server use php 7 on iis, i have script like this
this is my script
this my action when click "submit" button
$getResults= sqlsrv_query($conn, $tsql, $params);
$rowsAffected = sqlsrv_rows_affected($getResults);
if ($getResults == FALSE or $rowsAffected == FALSE)
die(FormatErrors(sqlsrv_errors()));
echo ($rowsAffected. " row(s) updated: " . PHP_EOL);
sqlsrv_free_stmt($getResults);
}
}
else
{
echo "Data belum berubah";
}
?>
this is my form
<form action='' method='POST'>
<div class="box-body">
<div style='overflow:auto;width:1300px;'>
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>my column name</th>
</tr>
</thead>
<?php
$tampil=sqlsrv_query($conn,'SELECT top 10 * FROM ws_manado ORDER BY Region', array(), array( "Scrollable" => 'static' ));
$count=sqlsrv_num_rows($tampil);
if (!$tampil)
{
printf("Error: %s\n", msql_error($conn));
exit();
}
$no=1;
while ($r=sqlsrv_fetch_array($tampil,SQLSRV_FETCH_ASSOC))
{
echo "$count
<tr>
<td id='Id[]' value=$r[Id]>$no</td>
<td id='Site_ID_Implementation[]' contenteditable='true'>$r[Site_ID_Implementation]</td>
<td id='Implementation_Date[]' contenteditable='true'>$r[Implementation_Date]</td>
<td id='PR_Number[]' contenteditable='true'>$r[PR_Number]</td>
<td id='PR_Amount[]' contenteditable='true'>$r[PR_Amount]</td>
</tr>";
$no++;
}
?>
</table>
</div>
<input type="submit" name="submit" id="submit" value="submit">
</div>
</form>
but when i click submit button my table not update
Related
I'm new to web development and I'm following a couple of tutorials. I'm learning that how to display mySQL data into HTML by using HTML, PHP, jQuery and AJAX. Below example is my practice work where I'm successfully loading data into HTML table.
<table id="main" border="1px" cellspacing="0">
<tr>
<td id="header">
<h1>PHP with AJAX</h1>
</td>
</tr>
<tr>
<td id="table-form">
<form id="addform">
Student First Name: <input type="text" id="st-first-name">
Student Last Name: <input type="text" id="st-last-name">
<input type="submit" value="Save Data" id="save-data"><br>
</form>
</td>
</tr>
<tr>
<td id="table-data">
<table border="1px" width="100" cellspacing="0px" cellpadding="10px">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr>
<td align="center">1</td>
<td>Abid Durrani </td>
</tr>
</table>
</td>
</tr>
</table>
Here is my AJAX Code.
$("document").ready(function(){
function loadData(){
$.ajax({
url: "ajaxload.php",
type: "post",
success: function(data){
$("#table-data").html(data);
}
});
} });
And this is my ajaxload.php code:
$myConn = mysqli_connect("localhost", "root", "", "test") or die("Your Connection Failed");
$sqlQuery = "select * from students";
$result = mysqli_query($myConn, $sqlQuery) or die ("Your Query Execution Failed");
$output = "";
if(mysqli_num_rows($result) > 0) {
$output = "
<table border='2' cellpadding='5px' cellspacing='5px'>
<tr>
<th width='100px'>ID</th>
<th>Name</th>
<th width='100px'>Delete</th>
</tr>";
while($row = mysqli_fetch_assoc($result)){
$output .= "
<tr>
<td>{$row['st_id']}</td>
<td>{$row['st_first_name']}</td>
<td><button class='btn-delete' data-myid={$row['st_id']}>Delete</button></td>
</tr>";
}
$output .= "</table>";
mysqli_close($myConn);
echo $output;
} else
{
echo "sorry, no record found";
}
Question: Here my data is directly loading into HTML table through its css ID. What I want is to set my database data into a textBox and a dropDownMenu along with this table. For table, I've created the table in ajaxload.php but what do I need to do for dropDownMenu and textBox?
Thanks.
Following is my form:
<form method="POST" action="../controller/assignsubteacher.php">
<table class="table table-bordered table-striped table-hover table-condensed" id="coursedetail" >
<thead>
<tr>
<th>Sub Id</th>
<th>Sub Name</th>
<th>Teacher Name</th>
</tr>
</thead>
<tbody id="table_ajax">
</tbody>
<tfoot>
<tr>
<th>Sub Id</th>
<th>Sub Name</th>
<th>Teacher Name</th>
</tr>
</tfoot>
</table>
<div class="col-md-2">
<button type="submit" class="btn btn-primary btn-block btn-flat">Submit</button>
</div>
</form>
and table in the form is populated by following response:
while($row=mysqli_fetch_array($result))
{
$list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
<option value = "UNKNOWN" selected="select">-SELECT-</option>';
$get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
$teacher_list = mysqli_query($con,$get_teacher);
while($row_Teacher=mysqli_fetch_array($teacher_list))
{
$list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
}
$list.='</select>';
$Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
//$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
$Subject_Name=$row["Subject_Name"];
$tr.='<tr>
<td>'.$Subject_ID.'</td>
<td>'.$Subject_Name.'</td>
<td>'.$list.'</td>
</tr>';
$COUNT=$COUNT+1;
}
echo $tr;
I am not able to use the posted data to insert in to database. Is there any way i can send the data as Array and retrieve it.
following is the AJAX to populate the table body:
xhr.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
console.log(xhr.responseText);
Table.innerHTML=xhr.responseText;
}
};
I was thinking of using foreach to insert data in the POST controller, but have no idea how to achieve that.
any help would be appreciated.
Use SQL insert query to do so.
while($row=mysqli_fetch_array($result))
{
$list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
<option value = "UNKNOWN" selected="select">-SELECT-</option>';
$get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
$teacher_list = mysqli_query($con,$get_teacher);
while($row_Teacher=mysqli_fetch_array($teacher_list))
{
$list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
}
$list.='</select>';
$Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
//$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
$Subject_Name=$row["Subject_Name"];
$tr.='<tr>
<td>'.$Subject_ID.'</td>
<td>'.$Subject_Name.'</td>
<td>'.$list.'</td>
</tr>';
$COUNT=$COUNT+1;
$sql = "INSERT INTO `table` (column1, column2, column3) VALUES ($list, $subject_name, $tr)";
//Replace columns & table name
if(mysqli_query($con, $sql)) {
echo "Inserted";
}
}
echo $tr;
When I click the delete button, the values of those checkboxes that are checked in the table are not shown. It is suppose to delete records on the database based on the id but i'm stuck in getting the value of the checkboxes. Please help correct my code. I'm using bootstrap for your information.
PHP
$sql = "SELECT id, title FROM box";
$query = mysqli_query($db, $sql);
if(isset($_POST['btnDelete'])) {
$checkbox = isset($_POST['chkDelete']) ? $_POST['chkDelete'] : array();
for($i=0;$i<count($checkbox);$i++)
{
$message = $checkbox[$i];
}
}
HTML
<div class="content">
<div id="table">
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th></th>
<th>#</th>
<th>Title</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
while($result = mysqli_fetch_array($query))
{
$html = '<tr>
<td data-title="">
<input name="chkDelete[]" type="checkbox" value="' . $result['id'] . '">
</td>
<td data-title="#">' . $result['id'] . '</td>
<td data-title="Title">' . $result['title'] . '</td>
<td data-title="Action">
Edit
</td>
</tr>';
echo $html;
}
?>
</tbody>
</table>
</div>
<form action="" method="POST">
<input type="submit" name="btnDelete" value="Delete" />
</form>
<div class="message"><?php echo $message;?></div>
</div> <!--//END content-->
PHP (Write Delete Query inside for loop to delete all checked checkbox.)
<?php
if(isset($_POST['btnDelete']))
{
$checkbox = isset($_POST['chkDelete']) ? $_POST['chkDelete'] : array();
$id = 0;
for($i=0;$i<count($checkbox);$i++)
{
$id = $checkbox[$i];
$deleteQuery = "DELETE FROM box WHERE id='$id'";
$DeleteQueryExec = mysqli_query($db, $deleteQuery);
}
}
?>
HTML (Put entire table inside <form></form>)
<div class="content">
<form action="" method="POST">
<div id="table">
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th></th>
<th>#</th>
<th>Title</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT id, title FROM box";
$query = mysqli_query($db, $sql);
while($result = mysqli_fetch_array($query))
{
$html = '<tr>
<td data-title="">
<input name="chkDelete[]" type="checkbox" value="' . $result['id'] . '">
</td>
<td data-title="#">' . $result['id'] . '</td>
<td data-title="Title">' . $result['title'] . '</td>
<td data-title="Action">
Edit
</td>
</tr>';
echo $html;
}?>
</tbody>
</table>
</div>
<input type="submit" name="btnDelete" value="Delete" />
<div class="message"><?php echo $message;?></div>
</form>
</div> <!--//END content-->
I have a 2 tables like this when I search the member name.
link :
I wanted the search result to be shown only after search.
my code:
<div id = "subtitle">
View Members
</div>
<div id = "searchbox">
<form method="post">
<center><input type="text" maxlength="100" required placeholder="Enter Full Name" name ="search" autocomplete="off" value="">
<input type="submit" name="btn" value="SEARCH NOW!"></p></center>
</form>
</div>
<?php
if(isset($_POST["btn"]))
{
$search = $_POST["search"];
$sql = "select * from member where Member_Name like '$search%' ";
$result = mysqli_query($conn,$sql);
$rowcount = mysqli_num_rows($result);
if($rowcount==0)
echo "Sorry ,no records found!";
else
{
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result)) //display
{
?> <tr>
<td></td>
<td><?php echo $row["Member_ID"]?></td>
<td><?php echo $row["Member_Name"]?></td>
<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>
</tr>
</table>
</center>
<?php
}
}
}
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
$sql = "select * from legoclub_guesthouse.member";
$result = mysqli_query($conn,$sql);
$rowcount= mysqli_num_rows($result);
while($row=mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td></td>";
echo "<td>$row[Member_ID]</td>";
echo "<td>$row[Member_Name]</td>";
echo "<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>";
echo "</tr>";
}
?>
</table><center>
</div>
Please include the php file for me too!
To do it in easiest way just try to use this code:
<div id = "subtitle">
View Members
</div>
<div id = "searchbox">
<form method="post">
<center><input type="text" maxlength="100" required placeholder="Enter Full Name" name ="search" autocomplete="off" value="">
<input type="submit" name="btn" value="SEARCH NOW!"></p></center>
</form>
</div>
<?php
if(isset($_POST["btn"]))
{
$search = $_POST["search"];
$sql = "select * from member where Member_Name like '$search%' ";
$result = mysqli_query($conn,$sql);
$rowcount = mysqli_num_rows($result);
if($rowcount==0)
echo "Sorry ,no records found!";
else
{
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result)) //display
{
?> <tr>
<td></td>
<td><?php echo $row["Member_ID"]?></td>
<td><?php echo $row["Member_Name"]?></td>
<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>
</tr>
<?php
}
$sql = "select * from legoclub_guesthouse.member";
$result = mysqli_query($conn,$sql);
$rowcount= mysqli_num_rows($result);
while($row=mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td></td>";
echo "<td>$row[Member_ID]</td>";
echo "<td>$row[Member_Name]</td>";
echo "<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>";
echo "</tr>";
}
?>
</table>
</center>
<?php
}
}
?>
</div>
But I would recommend you to use SQL UNION Operator.
It will make your life easy for these type of problems
I am trying to add a delete button to my DataTables table, but I don't know how I can post the username of the selected row when Delete button is pressed.
Here is how the DataTables is printed:
<div class="panel-body">
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Fullname</th>
<th>Class ID</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
while($listDetailsRow = mysql_fetch_array($listDetails)){
?>
<tr>
<td class="success"><?=$listDetailsRow['username']?></td>
<td class="info"><?=$listDetailsRow['email']?></td>
<td class="success"><?=$listDetailsRow['fullname']?></td>
<td class="info"><?=$listDetailsRow['class_id']?></td>
<td>
<form id='delete' action='deleteUser.php' method='post'>
<button type="submit" name="submit" class="btn btn-danger" href="deleteUser.php">
Delete
</button>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
So it is printed by php until all has been printedm then it stops...
The deleteUsers.php file is as follows:
include('../details.php');
$userDel = $_POST($listDetailsRow['username']);
echo $userDel;
// mysql_query("DELETE * FROM users WHERE username='$userDel'") or die(mysql_error());
I am just trying to get it to print the username from the row of which the delete button was pressed.
Is this possible? Not very good with JS or Ajax so all help is appreciated.
<form id='delete' action='deleteUser.php' method='post'>
<input type='hidden' name='username' value="<?=$listDetailsRow['username'];?>" > <--add this line
<button type="submit" name="submit" class="btn btn-danger" href="deleteUser.php">
Delete
</button>
</form>
php:
$userDel = $_POST['username'];
You can use an a element instead of a form within a button element. See this code:
<tbody>
<?php
while($listDetailsRow = mysql_fetch_array($listDetails)){
?>
<tr>
<td class="success"><?=$listDetailsRow['username']?></td>
<td class="info"><?=$listDetailsRow['email']?></td>
<td class="success"><?=$listDetailsRow['fullname']?></td>
<td class="info"><?=$listDetailsRow['class_id']?></td>
<td>
<!------------- New -------------->
<?php
$params = array( 'username' => $listDetailsRow['username'] );
$href = "deleteUser.php?" . http_build_query($params);
?>
<a class="btn btn-danger" href="<?php echo $href; ?>" > Delete </a>
<!------------- End New -------------->
</td>
</tr>
<?php
}
?>
</tbody>
In deleteUser.php do this:
$userDel = isset($_GET['username']) ? $_GET['username'] : null;