• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

mysql database not updating

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

raident30

Member
Joined
Sep 8, 2011
Location
Las Pinas, Philippines
hi guys! i have these codes.. the select from database is working but it wont update.. any chance that im doing wrong?
here are the codes


configdb.php
Code:
<?php
$hostname ="localhost";
$username ="root";
$password ="";
$db ="enrollmentdb";
mysql_connect($hostname,$username,$password) or die("cannot connect to server");
mysql_select_db($db) or die("database not found!");
?>

prelimgrading.php
Code:
<?php




	
include "configdb.php";
$sql = "SELECT enrollment.student_num, students.lastname, students.firstname, students.middlename, enrollment.subject_num, prelim
		FROM enrollment
		LEFT OUTER JOIN students ON enrollment.student_num = students.student_num;";
$result = mysql_query($sql) or die ("cannot execute query!");
	$count = 0;
	$vline = 1;
	echo "<form action = 'updateprelimgrade.php' method = 'POST' onsubmit = 'return checkform(this)'; >";
	while($row = mysql_fetch_row($result)){
		$student_num = $row[0];
		$lastname = $row[1];
		$firstname = $row[2];
		$middlename = $row[3];
		$subject_num = $row[4];
		$prelim = $row[5];
		
		
	
		
		$count = $count+1;
		
		if($vline%2==1) {
			$bg_color = "#3BB9FF";
			$vline = 2; }
			
					else
					{
						$bg_color="#2554C7";
						$vline=1;
						}
		
		
		

		print "<div class='tablecontents' style='float:left';>";
		print "<table align = 'left' height = '25' width='750' 'fontko'>";
		print "<center>";
		print "<tr>";
		echo "<td width = '100' bgcolor = '$bg_color'><p align = 'left'><font size = '2' face = 'Century Gothic' color ='white'> <input type='text' id='student_num' name='student_num' value=$student_num></font></td> </font></td>";
		print "<td width = '100' bgcolor = '$bg_color'><p align = 'left'><font size = '2' face = 'Century Gothic' color ='white'> $lastname </font></td>";
		print "<td width = '100' bgcolor = '$bg_color'><p align = 'left'><font size = '2' face = 'Century Gothic' color ='white'> $firstname </font></td>";
		print "<td width = '100' bgcolor = '$bg_color'><p align = 'left'><font size = '2' face = 'Century Gothic' color ='white'> $middlename </font></td>";
		echo "<td width = '150' bgcolor = '$bg_color'><p align = 'left'><font size = '2' face = 'Century Gothic' color ='white'><input type='text' id='prelim' name='prelim' value=$prelim></font></td>";
			
		print"</tr>";
		print "</center>";
		print"</table>";
		print"</div>";
		}
	print "<input type='submit' value='Submit'>";
	echo "</form>";
						
	?>

updateprelimgrade.php
Code:
<?php
$con = mysql_connect('localhost','root','') or die('Unable to select database');

mysql_select_db("enrollmentdb", $con);
$student_num=$_POST['student_num'];
$prelim=$_POST['prelim'];
$query="update enrollment SET prelim=$prelim where student_num=$student_num";
$result = mysql_query($query) or die ("Cannot Execute Query!");
if (!mysql_query($result))
{
echo"Failed to Update the Grading";

}
else
header('Location: prelimgrading.php');

?>

these are the database tables from the "enrollmentdb" database
Code:
CREATE TABLE IF NOT EXISTS `enrollment` (
  `STUDENT_NUM` varchar(10) NOT NULL DEFAULT '',
  `SUBJECT_NUM` varchar(10) NOT NULL DEFAULT '',
  `PRELIM` float(10,2) DEFAULT NULL,
  `MIDTERM` float(10,2) DEFAULT NULL,
  `FINALS` float(10,2) DEFAULT NULL,
  `FINAL_GRADE` float(3,2) DEFAULT NULL,
  PRIMARY KEY (`STUDENT_NUM`,`SUBJECT_NUM`))

Code:
CREATE TABLE IF NOT EXISTS `students` (
  `STUDENT_NUM` varchar(10) NOT NULL,
  `LASTNAME` varchar(50) DEFAULT NULL,
  `FIRSTNAME` varchar(50) DEFAULT NULL,
  `MIDDLENAME` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`STUDENT_NUM`)
)

Code:
CREATE TABLE IF NOT EXISTS `subjects` (
  `SUBJECT_NUM` varchar(10) NOT NULL,
  `SUBJECT_TITLE` varchar(100) DEFAULT NULL,
  `DEPARTMENT` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`SUBJECT_NUM`)
)

any help will be greatly appreciated
 
did you delete the password from the first code box? that could be a problem if not lol
 
Why are you including configdb.php in one of the files, but not in the other?

Try putting the following lines at the top of both prelimgrading.php and updateprelimgrade.php, below the opening <?php tag:
Code:
ini_set('display_errors', 1);
error_reporting(E_ALL);

Do any warnings or errors display in the browser when you do this?

EDIT: Another thing to try is removing the onsubmit = 'return checkform(this)'; from your <form> tag. I'm thinking that because the Javascript function checkform doesn't exist your code, Javascript is crashing right there and perhaps not submitting the form.
 
Last edited:
did you delete the password from the first code box? that could be a problem if not lol
i didn't put a password on my databse :D
Why are you including configdb.php in one of the files, but not in the other?

Try putting the following lines at the top of both prelimgrading.php and updateprelimgrade.php, below the opening <?php tag:
Code:
ini_set('display_errors', 1);
error_reporting(E_ALL);

Do any warnings or errors display in the browser when you do this?

EDIT: Another thing to try is removing the onsubmit = 'return checkform(this)'; from your <form> tag. I'm thinking that because the Javascript function checkform doesn't exist your code, Javascript is crashing right there and perhaps not submitting the form.

the warnings don't display, just my own warning "failed to update the grading".

i removed the javascript tag, its still not updating...:bang head:bang head:bang head
 
Replace every single one of your die() functions with die(mysql_error()). That should give you a better idea of where it's failing.
 
The value for student_num needs to be wrapped in single quotes because it's a varchar.
Try:
Code:
$query="update enrollment SET prelim=$prelim where student_num='$student_num'";
 
BTW I would advise using more logging with different levels (debug, info, warn etc), can help solve problems like this faster... For example when debugging try to log the actual failed sql and then try entering it manually if you can't spot a problem with it.
 
Back