2014-11-04 102 views
-4

這是我的PHP代碼。 我其他如果語句不起作用。 請幫助我。提前感謝。elseif在php代碼不起作用

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); 
     } 
} 
+0

看起來不錯的,它的工作原理。你使用的是什麼版本的PHP?在我的(5.6.8)中,elseif完美地工作。 – 2014-11-04 18:10:33

+2

你真的有'First_name'和'Lastname' ....如果是的話,爲什麼不''First_name'和'Last_name' – 2014-11-04 18:12:11

+2

請[不要使用'mysql_ *'功能](http://stackoverflow.com/問題/ 12859942/why-shouldnt-i-use-mysql-functions-in-php),它們不再被維護,並且[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。 [本文](http://php.net/manual/en/mysqlinfo.api.choosing.php)將幫助你決定。 – 2014-11-04 18:18:10

回答

1

您的代碼看起來不錯,所以你可以只擺脫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); 
}