1 Replies - 791 Views - Last Post: 26 October 2010 - 05:46 AM Rate Topic: -----

#1 mshow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-October 09

Saving or updating an array of records generated from a database on a

Posted 26 October 2010 - 05:16 AM

Hi! i'm new to php, i was trying out some codes in php and i discovered that using trim and strip_tags functions on an array of fields and saving/updating them back using iteration (i.e. foreach) will not work unless trim and strip_tags are excluded. Below is a coding sample

<?php
/*code to display records on a form.
file: score_form.php*/
//Display records on html form
$sql="SELECT student_id,score FROM exam_record WHERE class_id='$class'";

$result = mysql_query($sql) or die("Couldn't execute query.");

$nrows = mysql_num_rows($result);

if ($nrows < 1)

 {

   echo "<font color='#FF0000'><b>No registered student(s) found for this class.";

   exit();

 }

  

//student assessments sheet

echo "<form action='score.php?do=save' method='post'>

      <table bgcolor='FFFFDD' align='center' border='0' width='800' 
       cellspacing='3' cellpadding='5'>

      <tr><td colspan='3' bgcolor='FFFFBB' align='center'><font size='4' 
       color='#00FF00'>
<b>Students Assessment Form</b></font></td></tr>

      <tr align='center'><td><b>S/N</b></td><td><b>Registration Number</b></td><td><b>Score</b></td></tr>";

 

$j=0;

$no = 1;

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

{

  $row = mysql_fetch_array($result);

  extract($row);

  echo "<tr align='center'><td>$no</td>

         <td><input type='text' name='student_id[$j]' size='10'
              value='$student_id' />

         <td><input type='text' name='score[$j]' value='$score' maxlength='3' 
              size='3' /></td>

	</tr>";

  ++$no;//increase counter

  ++$j;

}

echo "<tr><td align='center' colspan='3'><input type='submit' value='Save' />
      </td></tr></table>
</form>";
?>


<?php
/*codes to save records from score_form.php*/

$student_id=trim(strip_tags($_POST['student_id'])); instead $student_id=$_POST['student_id']


$score=trim(strip_tags($_POST['score'])); instead $score=$_POST['score'];


//saving records for each array
$sqla = "ALTER TABLE exam_record DROP PRIMARY KEY";

mysql_query($sqla) or die("Unable to execute query. Please try again later");	

 

echo "<table border='0'>

      <tr><td><b>Registration Number</b></td></tr>";
$j=0;

if (count($student_id)>0)

{ 

  foreach (student_id as $student_id2)//foreach input, validate

  {

    $score=$score2[$j];

    //inserting data into the database

    $sqli="UPDATE exam_record SET score='$score2' WHERE 
           student_id='$student_id2'";

    mysql_query($sqli) or die("Unable to save the record. Please try again 
    later");	



    echo "<tr><td width='200'>$current_reg</td></tr>";

    ++$j;  //increase counter for the array

   }//end foreach

 
 //add primary keys to table
 $sqla = "ALTER TABLE exam_record ADD PRIMARY KEY( student_id, class_id)";

 mysql_query($sqla) or die("Couldn't execute query. Please, try again.");

   

 echo "</table><br /><font color='#FF0000'><b>The assessments for the students 
       listed above was successfully saved.

          </b></font><br />";

}



MOD EDIT: Moved code into post. Put your code IN your post, not in an attachment.

:code:

Attached File(s)


This post has been edited by JackOfAllTrades: 26 October 2010 - 05:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Saving or updating an array of records generated from a database on a

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Saving or updating an array of records generated from a database on a

Posted 26 October 2010 - 05:46 AM

Please explain what you mean by "will not work".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1