I am trying to create a class record. I created a table, with column of names and activities.
the table looks like this.
Name | Q1 | Q2 |
--------------------------------
Prinz | 20 | (input text)
My aim is to save the input data into my database, but I need to have the id of the student(Prinz). I already managed to save the grade but want the id of that particular student to be save as well. My MYSQL table has 4 att. namely, grade_id(auto_inc), stud_id, grade and the activity type(Quiz, Assignment, Exam).
Here's my code
<table border="0" cellspacing="0" cellpadding="0" style="width: 845px; margin-top: 20px; margin-bottom: 65px;">
<tr style="background-color: #caccca; text-align: center;">
<td style="width: 130px;" class="td-1">NAME</td>
<?php
$result = mysql_query("select * from subject_grade ");
$numrows = mysql_num_rows($result);
while ($row = mysql_fetch_object($result)) {
$act_type = $row->act_type;
echo '<td style="width: 130px;" class="td-1">'.$row->act_type .'</td>';
}
?>
<td class="td-1">GPA</td>
</tr>
<?php
$result = mysql_query("select * from student_db ");
while ($row = mysql_fetch_object($result)) {
echo '<tr class="border" style="background-color: #f9f9f9;"><td style="width: 150px;" class="td-2" name="student"><p>'.$row->firstname .' '. $row -> lastname .'</p></td>';
$id = $row->student_id;
for ($i=0; $i<$numrows; $i++){
echo '<form action ="record.php" method="POST"><td style="text-align: center;"><input type="text "style="font-size: 12px; width: 20px;" name="grade"><input style="display: none;" type="text" name="id" value="'.$id.'"><input type="submit"></td></form>';
}
echo '<td style="width: 80px;"class="td-2"></td>';
}
?>
</tr>
</table>
RECORD.PHP File
if($_POST['grade']){
$sql="INSERT INTO class_record (grade, student_id)
VALUES
('$_POST[grade]','$_POST[id]')";
$rs1 = mysqli_query($con,$sql);
echo $_POST['grade'] .' '. $studID;
} else echo 'ERROR';
?>
I hope you can help me, this is for our school project.
What data type are you storing student_id as?
('$_POST[grade]','$_POST[id]')";
use this query
$sql="INSERT INTO class_record (grade, student_id)
VALUES
('".$_POST['grade']."','".$_POST['id']."')";
Related
Trying to build a html table, which will be displaying the buildings a player is allowed build in a game. And there should also be a column that displays how many the player already own.
The code to display the table works as well as all the buildings you can build, but I can't get it to display how many of what building you already own.
Code is a bit messy now with some out commented queries, but I kept them in there to show what I have already tried to get them to display correctly.
EDIT(With C Würtz code):
// Fetch buildings
$result = $database->query("SELECT id, name, description, cost, power_use FROM buildings;");
$buildings = array();
while($building = $database->fetch($result)) {
$buildings[$building['id']] = $building;
}
// Buildings Owned
$buildings_owned = $database->query("SELECT building_id, count(*) as n FROM player_buildings WHERE owner_id = '$user_id'");
$buildings = array();
while($owned = $database->fetch($buildings_owned)) {
$owned_buildings[$owned['id']] = $owned;
}
$playerBuildings = array();
while($owned = $database->fetch($buildings_owned)) {
$playerBuildings[$owned['buildings_id']] = $owned['n'];
}
// Display form
echo "For every 10 building you construct, it will also cost you 1 turn!";
echo "<table style='width:900px;'>
<tr>
<th style='border: solid black 1px;width:40% text-align:left;'>Name</th>
<th style='border: solid black 1px;width:50%;'>Description</th>
<th style='border: solid black 1px;width:5%;'>Price</th>
<th style='border: solid black 1px;width:5%;'>Power Usage</th>
<th style='border: solid black 1px; width:5%;'>Buildins Owned</th>
<th style='width:5%;'> </th>
</tr>";
foreach ($buildings as $building) {
$bid = $building['id'];
$building['player_count'] = isset($playerBuildings[$bid])
? $playerBuildings[$bid]
: 0;
echo "<tr>
<td style='border: solid black 1px;'>{$building['name']}</td>
<td style='border: solid black 1px;'>{$building['description']}</td>
<td style='border: solid black 1px;'>{$building['cost']}</td>
<td style='border: solid black 1px;'>{$building['power_use']}</td>
<td style='border: solid black 1px;'>{$owned['amount']} </td>
<td>
<form action='$self_link' method='POST'>
<input type='hidden' name='building_id' value='$id' />
<input style='width:40px' type='number' name='amount' value='amount' />
<input type='submit' name='build' value='Build' />
</form>
</td>
</tr>";
}
echo "</table>";
The first query is like SELECT id, name, etc FROM buildings; to PHP array $buildings.
The second could be like SELECT buildings_id, count(*) as n FROM player_buildings WHERE owner_id = :user_id;. Then order the PHP array:
$playerBuildings = [];
while($owned = $database->fetch($buildings_owned)) {
$playerBuildings[$owned['buildings_id']] = $owned['n'];
}
In the view, just loop on $buildings and check if there is a relative $playerBuildings.
foreach ($buildings as $building) {
$bid = $building['id'];
$building['player_count'] = isset($playerBuildings[$bid])
? $playerBuildings[$bid]
: 0;
// render something
}
SELECT count(*) as owned_buildings FROM player_buildings WHERE owner_id = '$user_id'
hi this is my code which updates an attendance system, but I dont know how to insert attendance for first time. Where should I use insert query? it also shows error and not updating the table. Somebody please guide me
<?php
$connection=mysql_connect("localhost","root","") ordie(mysql_error()
$db=mysql_select_db("Project") or die(mysql_error());
$sql = "SELECT Fac_name FROM Faculty ORDER BY Fac_name ASC ";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<form name="Attendence" method="post" action="A.php">
<table style="text-align: left; padding: 5px;" cellpadding="0px" cellspacing="0px">
<tbody>
<tr>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Faculty Name</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Abesent</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Present</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Total present</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)) {
?>
<tr>
<td class="table1">
<? $id[] = $rows['Fac_name']; ?><? echo $rows['Fac_name'];?>
</td>
<td class="table1">
<input name="date[<? echo $rows['Fac_name']; ?>]" type="text" >
</td>
<td id="present">
<input type="radio" name="Present[<? echo $rows['Fac_name']; ?>]" checked="checked" >Present
</td>
<td id="absent">
<input type="radio" name="Absent[<? echo $rows['Fac_name']; ?>]" value="ABSENT">Absent
</td>
<td style="text-align: left; padding: 5px; border: 1px #000000 solid; height: 33px;"></td>
</tr>
<?php }?>
<tr>
<td colspan="7" style="vertical-align:middle; text-align: center;">
<br><br>
<input id="Submit" type="submit" name="Submit" value="Insert" style="text-align: center; background-color: #000000; color: #ffffff; border: 1px #000000 solid;">
</td>
</tr>
</tbody>
</table>
</form>
<?php
if(isset($_POST['Submit'])) {
foreach($_POST['Present'] as $id => $value) {
$date=$_POST['date'];
$present=$_POST['Present'];
$absent=$_POST['Absent'];
$sql = "INSERT INTO Attendence(Fac_name, date, Present, Absent) VALUES ('".$id."', '$date[$value]', '$present[$value]', '$absent[$value]', '".$value."') ";
$result = mysql_query($sql);
}
}
if($result) {
//header("location:A.php");
} else {
//print_r ($_POST);
echo "Your entry is not completed at this time.............";
}
if(isset($_POST['submitattend'])) {
set_time_limit(0);
$class1 = $_SESSION['bra'];
$q3 = mysql_query("Select Id from `Faculty` order by `Id` ASC"); // get all roll numbers
$count = mysql_num_rows($q3);
$j = 1;
while($q4 = mysql_fetch_array($q3)) {
if(isset($_POST['chk'.$j])) {
$v2 = $q7['finalattend']+1; //total attendance of student
$v3 = $q7['totalattend']+1; //total attendance taken by teacher
mysql_query("UPDATE `Attendence` SET `finalattend`='".$v2."', `totalattend`='".$v3."' where `attenduser`='".$v1."'") or die(mysql_error());
} else {
$v2=$q7['totalattend']+1;
mysql_query("UPDATE `Attendence` SET `totalattend`='".$v2."' where `attenduser`='".$v1."'") or die(mysql_error());
}
$j=$j+1;
}
header("Location: 12.html"); //logout after taking attendance..
}
?>
$date_value = $date[$value];
$present_value = $present[$value];
$absent_value = $absent[$value];
$check_result = mysql_query("selct count(*) from Attendence where Fac_name = '$id' AND date = '$date_value' AND Present = '$present_value' AND Absent = '$absent_value' ");
if($check_result == 0)
{
// insert query
}
else
{
// update query
}
I think your INSERT query line has Syntax error Please correct it . You have only three row names - Fac_name, date, Present, Absent but are inserting 5 values. Besides that quotes are not used correctly
I have a page to view and delete data from database. The delete is a link that clicked and pass the variable in url to another page for delete query. For example i have 3 rows in total in my database, i have no problem in deleting the last two rows but i was unable to delete the first row because the delete is deleting the record based on the "description" column, not the "ID" column. Below is the submission page and delete query page
viewreportdb.php
<!doctype html>
<head>
<meta charset="utf-8">
<title>Report Database </title>
<link href="style/fg_membersite.css" rel='stylesheet' type='text/css'>
</head>
<?php
include('config.php');
include('adminconfig.php');
$sql = "SELECT * FROM report ORDER BY ID";
$result = mysql_query($sql);
?>
<body>
<form method="POST" action="reportsave.php" onSubmit="return validate(this);">
<table id='fcolor2' class="viewdb3">
<tr>
<td style='border:2px solid black; background-color:#FF9933;' align=center> ID </td>
<td style='border:2px solid black; background-color:#FF9933;' align=center> Name </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Department </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Description </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Date </td>
</tr><?!-- missing close tr -->
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['ID'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['name'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['department'] ?></td>
<td><textarea rows="5" cols="50" name="description[]"><?php echo $row['description']; ?></textarea></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['date'] ?></td>
</tr><!-- missing close tr -->
<tr>
<td><input class='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>' maxlength="50"/></td>
<td><input class='edit' type = 'text' name="name[]" value='<?php echo $row['name'] ?>' maxlength="50"></td>
<td><input class='edit' type = 'text' name="department[]" value='<?php echo $row['department'] ?>' maxlength="50"></td>
<td> </td>
<td><input class='edit' type = 'text' name="date[]" value='<?php echo $row['date'] ?>' maxlength="50"></td>
<?php echo "<td><a href='http://localhost/kinhock/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
</tr><!-- missing close tr -->
<?php
} ?>
<tr class='btn2'><td><input type='submit' name='submit' value='Submit' /></td></tr>
<tr class='btn2'><td> Back to Description </td></tr><!-- missing open & close tr-->
<tr class='btn2'><td> Logout</td></tr>
</table>
</form>
<?php
$ID=$row['ID'];
$name=$row['name'];
$department=$row['department'];
$date=$row['date'];
$description=$row['description'];
?>
</body>
</html>
deletereport.php
<?php
include('adminconfig.php');
include('config.php');
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
$result=mysql_query($sql);
if(!$result){
die('invalid query:'.mysql_error());
}else
?>
<p style="font-family:arial;color:#0066CC;font-size:30px;">One row deleted...</p>
<?php
header('Refresh:3; url=viewreportdb.php');
die;
?>
Do not delete using description field because urlencoding and many more factors can break this code, even duplicate descriptions are quite possible, isn't it? Also your code is
liable to easiest SQL Injection methods!
Change your code here:
<?php echo "<td><a href='http://localhost/kinhock/
deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
And your SQL query:
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`='$id'";
STEP 1
Send the ID,not the description, for deletion.
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
STEP 2
Delete using the ID, not the description.
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`=$id";
You should probably use the ID instead of the description within your where clause and in this line:
(change)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
(to)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
and finally change:
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
to:
$id = $_GET['id'];
$sql="DELETE FROM `report` WHERE `ID`='$id'";
Using the ID will be faster and more accurate assuming it is a primary or unique ID.
You may also want to use:
$id = mysql_real_escape_string($_GET['id']);
to prevent SQL injection.
I believe your report table's ids are based on the users IDs right? Why don't you make it such that each report had an ID(report ID), User_ID(eg. admin001) and a description. In this way if admin001 makes report A it will have ID 1, if admin001 makes report B it will have ID 2. si when you want to delete Report A you use
$sql = "delete from {tablename} where ID={report ID}";
Overview
I have some data stored in MySql database related to Products. I am trying to retrieve this data and display it on a page using HTML table.
The PHP and MySql has gone well and all the data is retrieved but it is displayed in a very messy manner.
Here is what I have as a layout:
What I am aiming to achieve is to further divide the results table add more columns rows to make the data more readable
Something like this;
The code: PHP, MySQL & HTML:
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products");
echo "<table id='display'>";
while($rows = mysql_fetch_array($sql))
{
echo"<br>";
echo"<tr><td>";
echo"$rows[$product_name]<br></td>";
echo"<td><img src=$rows[$product_image] height='200px' width='200px'><br></td>";
echo"<td>Avalible: $rows[$product_qua]<br></td>";
echo"<td>Price: $rows[$product_price]<br></td>";
echo"<td>Description: $rows[$product_des]<br></td>";
echo"</tr>";
}
echo "</table>";
?>
CSS responsible for this part:
#display{
float:left;
border: 5px solid black;
margin-left:100px;
}
just add some padding or a border to the table cells:
table#display td{
border: 1px solid black;
padding:0 8px;
}
Edit: What you could do as well:
<table id='display'>
<?php while($rows = mysql_fetch_array($sql)): ?>
<!-- <br> <- why a break? it's not in the correct spot anyway -->
<tr><td>
<?php echo $rows[$product_name]; ?><br>
</td>
<td> - </td>
<td><img src="<?php echo $rows[$product_image]; ?>" height='200px' width='200px'><br></td>
<td> - </td>
<td>Avalible: <?php echo $rows[$product_qua]; ?><br></td>
<td> - </td>
<td>Price: <?php echo $rows[$product_price]; ?><br></td>
<td> - </td>
<td>Description: <?php echo $rows[$product_des]; ?><br></td>
</tr>
<?php endwhile; ?>
</table>
Tip: I prefer to use the while/endwhile; approach rather than using brackets when displaying data to the user.
First of all don't echo so much HTML using PHP, instead do it like this
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products"); ?>
<table id='display'>
<?php
while($rows = mysql_fetch_array($sql)) {
?>
<tr><td><?php echo $rows[$product_name]; ?></td>
<!-- And so on-->
<?php
}
?>
</table>
Secondly by seeing your inmage, it seems like you need a border for your table so use
table, td {
border: 1px solid #000000;
}
add the following css to apply border on your table cells:
#display td{ border: 2px solid red; }
and optionally add to your #display { :
border-collapse: collapse;
I'm getting a table appearing after the first result of the nested while loop.
What I'm trying to do is display the usernames as a list by selecting from the "users" table, then comparing that list with the project engineers from the "current projects" table.
This way there is a list of project engineers with their assigned projects underneath their names.
Unfortunately, I'm after the first nested while loop runs, it spits out a empty table and I'm not sure how to get rid of it.
I'm suspecting it has to do with the initial variable of $proj_engineer = "";
Does anyone know where this empty table is coming from and how to get rid of it?
Below is the code:
<?php
$query = mysql_query("SELECT * FROM `users` ORDER BY `username` ASC") or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$user_id = $row['user_id'];
$username = $row['username'];
$proj_engineer = "";
$query1 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_engineer` ASC") or die(mysql_error());
while ($row1 = mysql_fetch_assoc($query1)) {
$proj_id = $row1['proj_id'];
$proj_engineer = $row1['proj_engineer'];
}
echo "<table border=1><tr><td colspan=12><p class='bold18'>" . $proj_engineer . "</p></td></tr>";
$query3 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_id` DESC") or die(mysql_error());
while ($row3 = mysql_fetch_assoc($query3)) {
$proj_id = $row3['proj_id'];
$proj_number = $row3['proj_number'];
$proj_name = $row3['proj_name'];
$proj_sort = $row3['proj_sort'];
$proj_start = $row3['proj_start'];
$proj_finish = $row3['proj_finish'];
$proj_overstat = $row3['proj_overstat'];
$proj_dwgstat = $row3['proj_dwgstat'];
$proj_soostat = $row3['proj_soostat'];
$proj_substat = $row3['proj_substat'];
$proj_engineer = $row3['proj_engineer'];
$proj_drafter = $row3['proj_drafter'];
$proj_rating = $row3['proj_rating'];
$proj_pending = $row3['proj_pending'];
$proj_notes = $row3['proj_notes'];
$start_time = date("m/d/y", $proj_start);
$finish_time = date("m/d/y", $proj_finish);
echo "
<tr>
<td width=40>$proj_number</td>
<td width=100>$proj_engineer</td>
<td width=100><a href='./project-page.php?proj_id=$proj_id'>$proj_name</a></td>
<td width=40>$start_time</td>
<td width=40>$finish_time</td>
<td width=110>
<div style='position:relative; background:url(images/bar01.gif); width:$proj_overstat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_overstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_overstat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar02.gif); width:$proj_dwgstat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_dwgstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_dwgstat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar03.gif); width:$proj_soostat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_soostat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_soostat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar04.gif); width:$proj_substat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_substat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_substat%</div>
</div>
</td>
<td width=40 align='center'><a href='project-notes.php?proj_id=$proj_id'><img src='images/note.png' border=0></a></td>
<td width=40 align='center'><a href='./project-edit.php?proj_id=$proj_id'>EDIT</a></td>
<td width=40 align='center'><a href='./project-delete.php?proj_id=$proj_id'>DELETE</a></td>
</tr>
";
}
echo "</table><br>";
}
?>
This might be because, your query: -
$query1 = mysql_query("SELECT * FROM `current_projects` WHERE
`proj_engineer`='$username' ORDER BY `proj_engineer` ASC")
or die(mysql_error());
fetched you an empty value for $proj_engineer. You can do a check before printing your table, whether your variable contains a value instead. Try printing the $proj_engineer before printing the table.
You can enclose your code echoing the table inside an if construct, which will print table only when your value is not empty.