Jump to content

Data can not be inserted to MYSQL database !!

- - - - -

  • Please log in to reply
4 replies to this topic

#1
NumOne

NumOne

    Newbie

  • Members
  • Pip
  • 9 posts
Hi

I have been designing a website for teachers to help them to enter the students' marks and the students can access the website to get their marks. I have a problem which is the data are not inserted to the database.

$count1=100;

$count2=200;

$count3=300;

$count4=400;


echo "<form action='ConfirmEnter.php' method='post'>";

echo "<table border cellpadding=3>"; 

echo "<tr>"; 

echo "<th>ID</th>";

echo "<th>MidTerm</th>"; 

echo "<th>Project</th>"; 

echo "<th>Final</th>";

echo "<th>Total</th>". "</tr>";


while($row1 = mysql_fetch_array($result1)) 

{ 

echo "<tr>";

echo "<td><input name='".$count1."' readonly='readonly' value='". $row1['ID'] ."'  size=5/></td> "; 

echo "<td><input type='text' name='".$count2."' size=5 value='0.0' /></td>";

echo "<td><input type='text' name='".$count3."' size=5 value='0.0' /></td>";

echo "<td><input type='text' name='".$count4."' size=5 value='0.0' /></td>";

echo "</tr>";

$count1++;

$count2++;

$count3++;

$count4++;

} 

echo "</table>"; 

echo "<input type='submit' value='Submit' />";

echo "</form>"; 

This is the action page

$count=1;

$count1=100;

$count2=200;

$count3=300;

$count4=400;


function addtwo($a = 0.0 , $b = 0.0 , $c = 0.0)

   {

          return ($a + $b + $c);

   }


while($row1 = mysql_fetch_array($result1)) 

 { 

  $id[$count] = $_POST['$count1'];

  $mt[$count] = $_POST['$count2'];

  $pr[$count] = $_POST['$count3'];

  $fi = $_POST['$count4'];

  $tot[$count] = addtwo($mt[$count]+$pr[$count]+$fi);

  echo $fi;

  mysql_query("INSERT INTO Marks (ID, Name, MidTerm, Project, Final, Total)

  VALUES ('$id[$count]', 'EMPTY', '$mt[$count]', '$pr[$count]', '$fi', '$tot[$count]')");

  $count++;

  $count1++;

  $count2++;

  $count3++;

  $count4++;

  }



#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Are you showing us all of your code?
while($row1 = mysql_fetch_array($result1)) 

There is no $result1, why are you wrapping mysql_query in this while loop if you are not even accessing $result1 or $row1 on your action page?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
NumOne

NumOne

    Newbie

  • Members
  • Pip
  • 9 posts
This is the complete code

<html>

<head>


</head>

<body>

<title>Num One Website</title>


<?

$con = mysql_connect("","","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


mysql_select_db("", $con);


$result1 = mysql_query("SELECT * FROM Students") ;


$count1=100;

$count2=200;

$count3=300;

$count4=400;


echo "<form action='ConfirmEnter.php' method='post'>";

echo "<table border cellpadding=3>"; 

echo "<tr>"; 

echo "<th>ID</th>";

echo "<th>MidTerm</th>"; 

echo "<th>Project</th>"; 

echo "<th>Final</th>";

echo "<th>Total</th>". "</tr>";


while($row1 = mysql_fetch_array($result1)) 

 { 

 echo "<tr>";

 echo "<td><input name='".$count1."' readonly='readonly' value='". $row1['ID'] ."' size=5/></td> "; 

 echo "<td><input type='text' name='".$count2."' size=5 value='0.0' /></td>";

 echo "<td><input type='text' name='".$count3."' size=5 value='0.0' /></td>";

 echo "<td><input type='text' name='".$count4."' size=5 value='0.0' /></td>";

 echo "</tr>";

 $count1++;

 $count2++;

 $count3++;

 $count4++;

 } 

echo "</table>"; 

echo "<input type='submit' value='Submit' />";

echo "</form>"; 

mysql_close($con);



?>


</body>

</html>



The action code is

<html>

<body>

<title>Num One Website</title>


<?php


$con = mysql_connect("","","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


mysql_select_db("", $con);


$result1 = mysql_query("SELECT * FROM Students") ;


$count=1;

$count1=100;

$count2=200;

$count3=300;

$count4=400;


function addtwo($a = 0.0 , $b = 0.0 , $c = 0.0)

       {

              return ($a + $b + $c);

       }


while($row1 = mysql_fetch_array($result1)) 

 { 

$id[$count] = $_POST['$count1'];

$mt[$count] = $_POST['$count2'];

$pr[$count] = $_POST['$count3'];

$fi = $_POST['$count4'];

$tot[$count] = addtwo($mt[$count]+$pr[$count]+$fi);

echo $fi;

mysql_query("INSERT INTO Marks (ID, Name, MidTerm, Project, Final, Total)

VALUES ('$id[$count]', 'EMPTY', '$mt[$count]', '$pr[$count]', '$fi', '$tot[$count]')");

$count++;

$count1++;

$count2++;

$count3++;

$count4++;

 }



echo "Marks have been entered into the system";


mysql_close($con);


?>


<form action="EnterMarks.php" method="post">

<br><br><input type="submit" value="Enter Marks" /><br>

</form>




</body>

</html>



#4
om sa

om sa

    Newbie

  • Members
  • PipPip
  • 11 posts
I think the problem is in the action page,
try this out,

EnterMarks.php
<html>

<head>


</head>

<body>

<title>Num One Website</title>


<?

$con = mysql_connect("","","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


mysql_select_db("", $con);


$result1 = mysql_query("SELECT * FROM Students") ;


$counter=0;


echo "<form action='ConfirmEnter.php' method='post'>";

echo "<table border cellpadding=3>"; 

echo "<tr>"; 

echo "<th>ID</th>";

echo "<th>MidTerm</th>"; 

echo "<th>Project</th>"; 

echo "<th>Final</th>";

echo "<th>Total</th>". "</tr>";


while($row1 = mysql_fetch_array($result1)) 

 { 

 echo "<tr>";

 echo "<td><input name='ID".$counter."' readonly='readonly' value='". $row1['ID'] ."' size=5/></td> "; 

 echo "<td><input type='text' name='MidTerm".$counter."' size=5 value='0.0' /></td>";

 echo "<td><input type='text' name='Project".$counter."' size=5 value='0.0' /></td>";

 echo "<td><input type='text' name='Final".$counter."' size=5 value='0.0' /></td>";

 echo "</tr>";


$counter++;

 } 


echo "</table>"; 

echo "<input type='hidden' name='counter' size=5 value='".$counter."' />";

echo "<input type='submit' value='Submit' />";

echo "</form>"; 


?>


</body>

</html>



ConfirmEnter.php
<html>

<body>

<title>Num One Website</title>


<?php


$con = mysql_connect("","","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


mysql_select_db("", $con);


$total_lines = $_post['counter'];


function addtwo($a = 0.0 , $b = 0.0 , $c = 0.0)

       {

              return ($a + $b + $c);

       }


for($i=0; $i <= $total_lines; $i++)

{

$id = $_POST['ID'.$i];

$midterm = $_POST['MidTerm'.$i];

$project = $_POST['Project'.$i];

$final = $_POST['Final'.$i];


$total = addtwo($midterm, $project, $final);


$sql_statment = "INSERT INTO Marks VALUES ('$id', 'EMPTY', '$midterm', '$project', '$final', '$total')";


mysql_query($sql_statment);

}



echo "Marks have been entered into the system";



?>



<a herf="EnterMarks.php">Enter Marks</a>




</body>

</html>


Also, in future try to simplify you code by using meaningful names, also try to avoid complicating you code with overuse of counters, loops, etc.


good luck :)

#5
om sa

om sa

    Newbie

  • Members
  • PipPip
  • 11 posts
the PHP code in file: ConfirmEnter.php has an error:

change: for($i=0; $i <= $total_lines; $i++) to -> for($i=0; $i < $total_lines; $i++)

Edited by om sa, 13 June 2011 - 12:48 AM.
fixing error,





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users