如何在循环期间通过条件更新表

时间:2013-07-05 23:44:37

标签: php mysql

 $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());

}

你好,我正面临一个问题,请帮助我,它将提供教师的完整列表,但如果教师姓名字段为空则更新

3 个答案:

答案 0 :(得分:3)

为什么不在单个SQL中执行此操作?

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

(将 name =“”更改为名称IS NULL ,如果它是可以为空的字段)

答案 1 :(得分: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());

}

答案 2 :(得分:0)

您应该使用mysqli而不是mysql或PDO Class来利用预准备语句

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