2013-07-05 75 views
0
$cteachers = mysql_query("SELECT * FROM teachers WHERE teachers.stats = '1'") or die(mysql_error()); 
while($info = mysql_fetch_array($cteachers)) 
{ 

Print "<li><strong>$info['id'];</strong></li><li><strong>$info['name'];</strong></li>"<li><strong>$info['rnk'];</strong></li>; 

$id = $info['id']; 
$cnme = $info['name']; 
$fnme = $info['f_name']; 
$lnme = $info['l_name']; 
$rnkk = $info['rnk']; 

    $fullname = $fnme." ".$lnme; 


if ($cnme == '') 
{ 
$que2 = "UPDATE teachers (name) VALUES('$fullname') WHERE teachers.f_name = '"$fnme"' AND teachers.l_name = '"$lnme"'" or die(mysql_error()); 
$exe2 = mysql_query($que2) or die(mysql_error()); 

} 

你好,我現在面臨一個問題,請大家幫我將提供教師,但的完整列表,如果老師全名字段爲空,則更新如何更新表通過if條件時while循環

+0

@Prix是正確的。你可以像'UPDATE ... SET ... = ...' –

+0

這樣做,並在打印前進行更新:)否則,它會首先打印空白,然後更新。但我建議先執行Kickstart的答案查詢,然後執行這個查詢,你說 – Fallen

+0

我迷路了。你想做什麼? – Strawberry

回答

0

您使用mysqli替代MySQL或PDO類採取事先準備好的聲明的優勢

if(empty($fnme) && empty($lnme)){ 
    $que2 = "UPDATE teachers set name='$fullname' WHERE id='$id'"; 
    $exe2 = mysqli_query($link,$que2);//$link is the conncetion parameter 
} 
+0

謝謝@Emilio Gort –

3

爲什麼不在單個SQL中執行它?

UPDATE teachers 
SET name = CONCAT(f_name, " ", l_name) 
WHERE stats = "1" 
AND name = "" 

(其他城市NAME = 「」名稱爲null,如果它是一個可空場)

0

你的表的主鍵? 據我所知,我們可以在更新表基地的主鍵

if (!isset($cnme)){ 
    $que2 = "UPDATE teachers set name='$fullname' WHERE teachers.f_name = '"$fnme"' AND teachers.l_name = '"$lnme"'"; 
    $exe2 = mysql_query($que2) or die(mysql_error()); 

}