M.RK
M.RK

Reputation: 21

How To Update table through if condition during while Loop

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

}

hello i am facing a problem please help me it will be provide full list of teacher's but if teacher full name field is empty then update it

Upvotes: 0

Views: 144

Answers (3)

Angripa
Angripa

Reputation: 157

What the primary key of your table ? As I know we can update table base on the primary key

or

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

}

Upvotes: 0

Emilio Gort
Emilio Gort

Reputation: 3470

You should use mysqli instead mysql or PDO Class to take advantage of prepared statement

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

Upvotes: 0

Kickstart
Kickstart

Reputation: 21513

Why not do it in a single piece of SQL?

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

(change name = "" to name IS NULL if it is a nullable field)

Upvotes: 3

Related Questions