-2

It is showing all the students in database and textbox with them. But when I enter marks of every student it picks only the first record and save only marks.

I want:

  1. all name of student should print on page with text-box in which I will enter marks of each student.
  2. I want to save the record of every student with their marks.

--

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
    die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM stunr WHERE stucou = '".$_POST["cls"]."'";

mysql_select_db('college-db');
$retval = mysql_query( $sql, $conn );
if($retval === FALSE) { 
   die(mysql_error());
}

$row = mysql_fetch_array($retval);
echo "<div style='margin-top:120px; margin-left:180px;'>";
$cla=$row['stucou'];
echo "<h3>Class"."<b>&nbsp;&nbsp;$cla</b>"."</h3>";
echo"</div>";

echo "<div style='margin-top:50px; margin-left:180px;'>";
while($row = mysql_fetch_array($retval)) {
    echo"<form method='post' action='mst-marks-insert.php'>";
    $clsnm  = $row['stufname'];
    echo"$clsnm";
    echo "<br>";
    echo"<input type='text' name='mas' class='textbox' placeholder='Enter Marks'/><br><br>";
}
echo "<input type='text' name='tot' />";
echo"<input type='submit' />";
echo"</form>";
echo"</div>";
Jocelyn
  • 11,209
  • 10
  • 43
  • 60
Gundeep
  • 21
  • 7
  • 1
    you need to show the code that handles the saving. – Marc B Jul 29 '16 at 15:05
  • I suggest to use a table structure for printing the fetched data of student. – Jees K Denny Jul 29 '16 at 15:10
  • as well, since ever student gets output as a separate form, only one student's data will EVER get submitted. multiple forms on a page are perfectly fine, but submitting one only submits that ONE form, not any others. – Marc B Jul 29 '16 at 15:12
  • 1
    There are a lot of issues with this code, most important of which are [mysql functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), [code formatting](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks), [separating logic from presentation](http://stackoverflow.com/questions/18641738/how-i-separate-logic-from-presentation) and [using php as a template engine](http://stackoverflow.com/questions/4977529/using-php-as-a-template-engine). – php_nub_qq Jul 29 '16 at 15:13
  • `echo"

    "; ` please pass array to the action page like the above code
    – Jees K Denny Jul 29 '16 at 15:25

2 Answers2

0

I would start with moving the beginning form tag before the while loop.

echo"<form method='post' action='mst-marks-insert.php'>";
 while($row = mysql_fetch_array($retval))
    {
Abrucius
  • 101
  • 6
  • Abrucis is correct, You'll have multiple opening form tags (one for every record shown) and only one closing form tag, so only the last entry would have a fully working form. – Duane Lortie Jul 29 '16 at 15:11
0

Do something like this:

  1. Move out the beginning form before while loop
  2. Make field "mas" an array

This could look like this

<?php

mysql_select_db('college-db');
$retval = mysql_query($sql, $conn);
if ($retval === FALSE) {
    die(mysql_error());
}


$row = mysql_fetch_array($retval);
echo "<div style='margin-top:120px; margin-left:180px;'>";
$cla = $row['stucou'];
echo "<h3>Class" . "<b>&nbsp;&nbsp;$cla</b>" . "</h3>";
echo"</div>";

echo "<div style='margin-top:50px; margin-left:180px;'>";
echo "<form method='post' action='mst-marks-insert.php'>";
while ($row = mysql_fetch_array($retval)) {
    $clsnm = $row['stufname'];
    echo "$clsnm";
    echo "<br>";
    echo "<input type='text' name='mas[$clsnm]' class='textbox' placeholder='Enter Marks'/><br><br>";
}
echo "<input type='text' name='tot' />";
echo "<input type='submit' />";
echo "</form>";
echo "</div>";

?>

In your "mst-marks-insert.php" the $_POST var then looks something like this:

<?php
var_dump($_POST['mas']);
array (
  'clsnm1' => 'test',
  'clsnm2' => 'test2',
)
?>

So you could do something like this (it's just an example what would be possible, because I don't know how your SQL table looks like):

<?php
foreach ($_POST['mas'] as $clsnm => $mas) {
    mysql_query("INSERT INTO `mastable` (`clsnm`, `mas`) VALUES ('".mysql_real_escape_string($clsnm)."', '".mysql_real_escape_string($mas)."')");
}
?>
simialbi
  • 507
  • 3
  • 10