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

}

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

3
  • @Prix is right. You do something like UPDATE ... SET ...=... Commented Jul 5, 2013 at 23:49
  • and do update before printing :) Otherwise it'll print empty first and then update. But I suggest to execute the query of Kickstart's answer first and then executing this query you stated Commented Jul 5, 2013 at 23:50
  • I'm lost. What are you trying to do? Commented Jul 5, 2013 at 23:52

3 Answers 3

3

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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
}

Comments

0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.