0
     <?php 
 $id=$_REQUEST['id'];
 $sub=array();
 $sub=$_REQUEST['sub'];
 $total=0;
 for($i=0;$i<count($sub); $i++)
    {
        $total=$total+$sub[$i];
    }
 $link=mysql_connect("localhost","root","") or die("Cannot Connect to the    database!");

 mysql_select_db("nps_exam",$link) or die ("Cannot select the database!");
 $query= "UPDATE four SET 'sub[1]'='".$sub[0]."' , 'sub[2]'='".$sub[1]."' , 'sub[3]'='".$sub[2]."' , 'sub[4]'='".$sub[3]."' , 'sub[5]'='".$sub[4]."' , 'sub[6]'='".$sub[5]."' , 'sub[7]'='".$sub[6]."' , 'sub[8]'='".$sub[7]."' , 'sub[9]'='".$sub[8]."' , 'Music'='".$sub[9]."' , 'Arts'='".$sub[10]."' , 'total'='".$total."' WHERE Registration_no=='".$id."'";

      if(!mysql_query($query,$link))
      {die ("An unexpected error occured while saving the record, Please try again!");}
      else
     {
      echo "Record updated successfully!";}
 ?>

I am new to php.While updating records from this above php code. I always get error message saying An unexpected error occured while saving record,also I cannot get my data updated..please anyone help..

  • replace the die line with `die("Error with query: $query");` so we can see what the query looks like. – Jhong May 14 '12 at 16:46
  • 2
    sub[1] and sub[2] etc are not valid column names. If the columns really are named like that you'll have to quote them in backticks like so: `sub[1]` – Roland Bouman May 14 '12 at 16:47
  • you mean 'sub[1]'..if so then I think I have written the same in my code – Bishu Bohara May 14 '12 at 18:10

2 Answers2

2

You need to change the WHERE clause from:

WHERE Registration_no=='".$id."'    

to

WHERE Registration_no='".$id."'
andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • It's also worth getting your code to output the actual SQL, and entering it directly into the database. If nothing else, it'll tell you whether the problem is with the SQL or with your database connection. – andrewsi May 14 '12 at 16:49
1

Do this :

if(!mysql_query($query,$link)) {
   die (mysql_error());
} else {
  echo "Record updated successfully!";
}

mysql_error() will give you the exact error message - the issue could just about be anything ... connection problem / query syntax error / missing data etc

Manse
  • 37,765
  • 10
  • 83
  • 108
  • thanks for your response I did exactly what you say but I couldnot find out what error message said as- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''sub[1]'='2' , 'sub[2]'='2' , 'sub[3]'='56' , 'sub[4]'='2' , 'sub[5]'='2' , 'sub' at line 1 – Bishu Bohara May 14 '12 at 18:03
  • Are the columns in your table called 'sub[1]' to 'sub[10]'? – andrewsi May 14 '12 at 18:14
  • @bishubohara what are the column names ?? The error is correct - you cannot use square brackets as a column name without escaping them with back quotes. Not single quotes – Manse May 14 '12 at 21:38
  • yup that was my mistake..thanks for your help.and one thing If I need to use square brackets in column name what should I have to do can you explain me with an small example please.. – Bishu Bohara May 16 '12 at 01:55
  • @BishuBohara see here http://stackoverflow.com/questions/261455/using-backticks-around-field-names – Manse May 16 '12 at 07:54