Hello. I am having this problem with a php project i'm working on, and needed help and found this site from google 🙂.
It is very strange; take a look at my code.
Whats basically happening is I am inputing data from an sql database, and there is a main screen ( from the file showsc.php ), which is included by sc.php, and it prints the data of the rows fine.
Now, another page is a page to add a row to the table. I click add and it adds the data to the table fine, but when it goes back to the view page, using the exact same code as before, the second two rows of the second column do not show up. If I just reload that page, they show up. Very Strange! I hope you understand what I'm trying to say. I've include a picture that illustrates what is happening. Here is the code of the two files I am using.
showsc.php file:
<?php
$sql = "SELECT * FROM tblSC";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$c = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$c = $c + 1;
$scid[$c] = $row['SCID'];
$studentid[$c] = $row['StudentID'];
$tcid[$c] = $row['TCID'];
}
$total = $c;
for($c = 1; $c <= $total; $c += 1)
{
$sql = "SELECT ClassID FROM tblTC WHERE TCID = '$tcid[$c]'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$classid[$c] = $row['ClassID'];
$sql = "SELECT ClassName FROM tblClass WHERE ClassID = '$classid[$c]'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$classname[$c] = $row['ClassName'];
}
for($c = 1; $c <= $total; $c += 1)
{
$sql = "SELECT Username FROM tblStudent WHERE StudentID = '$studentid[$c]'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$studentname[$c] = $row['Username'];
}
?>
<table width="60%" border="1">
<tr>
<td><b>Class Name</b></td>
<td><b>Student Name</b></td>
<td> </td>
</tr>
<?php for($c = 1; $c <= $total; $c += 1)
{
?>
<tr>
<td><?php echo $classname[$c]; ?></td>
<td><?php echo $studentname[$c]; ?></td>
<td><form action="?form=delete&scid=<?php echo $scid[$c]; ?>" method="post"><input name="Delete" type="submit" value="Delete"></form></td>
</tr>
<?php
}
?>
sc.php file:
<?php
include 'library/config.php';
include 'library/opendb.php';
$form = $_GET['form'];
if($form == 'addform')
{
$form = '';
include 'addsc.php';
}
elseif($form == 'add')
{
$form = '';
$susername = $_POST['selectStudent'];
$teacherclassid = $_POST['selectClass'];
$type = 'student';
include 'library/findid.php';
$sql = "INSERT INTO tblSC values ('','$studentid','$teacherclassid');";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
include 'showsc.php';
}
elseif($form == 'delete')
{
$form = '';
$scid = $_GET['scid'];
$sql = "DELETE FROM tblSC WHERE SCID = '$scid'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
include 'showsc.php';
}
else
{
include 'showsc.php';
};
include 'library/closedb.php';
?>
addsc.php :
<html>
<form action="?form=add" method="post" name="frmAdd" id="frmAdd">
<table width="400" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td width="150">Student Username</td>
<td>
<select name="selectStudent">
<?php
include 'library/config.php';
include 'library/opendb.php';
$query = 'SELECT Username FROM tblStudent';
$result = mysql_query($query) or die('Query failed. ' . mysql_error());
$c = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$c = $c + 1;
$student[$c] = $row['Username'];
$id[$c] = $row['StudentID'];
}
$total = $c;
for($c = 1; $c <= $total; $c += 1)
{
?>
<option value="<?php echo $student[$c]; ?>"><?php echo $student[$c]; ?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td width="150">Class Name</td>
<td> <select name="selectClass">
<?php
$query = 'SELECT * FROM tblTC';
$result = mysql_query($query) or die('Query failed. ' . mysql_error());
$c = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$c = $c + 1;
$tcid[$c] = $row['TCID'];
$classid[$c] = $row['ClassID'];
}
$total = $c;
for($c = 1; $c <= $total; $c += 1)
{
$query = "SELECT ClassName FROM tblClass WHERE ClassID = '$classid[$c]'";
$result = mysql_query($query) or die('Query failed. ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$classname[$c] = $row['ClassName'];
}
for($c = 1; $c <= $total; $c += 1)
{
?>
<option value="<?php echo $tcid[$c]; ?>"><?php echo $classname[$c]; ?></option>
<?php } ?>
</select> </tr>
<tr>
<td width="150"> </td>
<td><input name="btnSubmit" type="submit" id="btnSubmit" value="Link"></td>
</tr>
</table>
</form>
</html>