MySQL

Update Multiple Rows at Once in PHP

  • Oleh admin
  • 3 menit baca
  • Belum ada komentar

First step :  Create Database with the name course.  

Second step : Download from this Link To Download Database.  And import to your database.

Last step : Create File with the name view.php in your PHP text editor, and copy this script :

<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>List Grade per Subject</title>
<style type=”text/css”>
table.hovertable {
font-family: verdana,arial,sans-serif;
font-size:12px;
color:#222222;
border-width: 1px;
border-color: #888888;
border-collapse: collapse;
}
table.hovertable th {
background-color:#87f79e;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #7c91d6;
}
table.hovertable tr {
background-color:#9ae59b;
}
table.hovertable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #7c91d6;
}
input {
alignment-adjust: central;
}
</style>
</head>
<body>
<?php
$con = mysql_connect(‘localhost’, ‘root’, ”) or die (mysql_error());
mysql_select_db(‘course’, $con) or die (mysql_error());
?>
<table class=”hovertable”>
<th>Number</th>
<th>Student Number</th>
<th>Student Name</th>
<th>Subject Code</th>
<th>Subject Name</th>
<th>Grade</th>
<form action=”view.php” method=”POST”>
<?php
$query_all = “select student.number_st as number_st, student.name_st as name_st, subject.code_sub as code_sub,
subject.name_sub as name_sub, grade.grade as grade
from student, grade, subject
where student.number_st = grade.number_st &&
subject.code_sub = grade.code_sub “;
$result_all = mysql_query($query_all) or die (mysql_error());
$count = mysql_num_rows($result_all);
$number = 1;
while ($rows = mysql_fetch_object($result_all)){
?>
<tr onmouseover=”this.style.backgroundColor=’#c98876′;” onmouseout=”this.style.backgroundColor=’#9ae59b’;”>
<td><?php echo $number++; ?></td>
<td><?php echo $rows->number_st; ?></td>
<td><?php echo $rows->name_st; ?></td>
<td><?php echo $rows->code_sub; ?></td>
<td><?php echo $rows->name_sub; ?></td>
<td>
<input type=”hidden” id=”number_st” name=”number_st[]” value=”<?php echo $rows->number_st; ?>”>
<input type=”hidden” id=”code_sub” name=”code_sub[]” value=”<?php echo $rows->code_sub; ?>”>
<input type=”text” size=”2″ name=”grade[]” id=”grade” value=”<?php echo $rows->grade; ?>”
</td>
</tr>
<?php
}
?>
<tr>
<td>
<input type=”submit” name=”submit” value=”SAVE”>
</form>
</td>
</tr>
</table>
<?php
if(isset($_POST[‘submit’])){
$grade = $_POST[‘grade’];
$number_st = $_POST[‘number_st’];
$code_sub = $_POST[‘code_sub’];
for($i = 0; $i < $count; $i++){
$query_gradeupdate = “UPDATE grade set grade = ‘”.$grade[$i].”‘ where
number_st = ‘”.$number_st[$i].”‘ && code_sub = ‘”.$code_sub[$i].”‘”;
mysql_query($query_gradeupdate, $con) or die (mysql_error());
}
echo “Data Update Successfully <a href=view.php>Refresh</a>”;
mysql_close($con);
}
?>
</body>
</html>

And in the end result like this :

https://microaltech.wordpress.com/wp-content/uploads/2015/07/multiple-row-update.jpg

 

Bagikan

Tentang Penulis

admin

Butuh Bantuan Mengembangkan Website atau Aplikasi?

Diskusikan kebutuhan proyek Anda dan tentukan solusi yang paling sesuai.

0 comments on Update Multiple Rows at Once in PHP

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *