-4

This is my Php code. My else if statement is not working. Please help me.Thanks in advance.

if($row=mysql_fetch_array($result1,MYSQL_ASSOC)){
     $pv=$row['pv'];
     $cv=$row['cv'];

     if($pv=="First_name"){
         $sql1="update User set fname='$cv' where ID='$id'";
         $result1=mysql_query($sql1);
      }elseif($pv=="Lastname"){
         $sql1="update User set lname='$cv' where ID='$id'";
         $result1=mysql_query($sql1);
      }
 }
Cayce K
  • 2,288
  • 1
  • 22
  • 35
Rekha
  • 1
  • 1
  • looks good, it works. What version of PHP are you using? In mine (5.6.8) the elseif works perfectly. – Jack M. Nov 04 '14 at 18:10
  • 2
    do you really have `First_name` and `Lastname`.... if so why not `First_name` and `Last_name` – Cayce K Nov 04 '14 at 18:12
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Nov 04 '14 at 18:18

1 Answers1

1

Your code looks okay so you could just get rid of the elseif

if($row=mysql_fetch_array($result1,MYSQL_ASSOC)){
$cv=$row['cv'];
$column="fname";
if($row['pv']=="Lastname"){
$column="lname";
}
$sql1="update User set $column='$cv' where ID='$id'";
$result1=mysql_query($sql1);
}
Funk Doc
  • 1,473
  • 1
  • 11
  • 12