duancong2965
2015-06-09 08:59
浏览 198
已采纳

PHP多个if语句只执行最后一个语句

I know similar questions have been asked a lot, and I have searched through and am unable to find an answer that fixes the problem I am having.

I have a form with multiple textareas that updates a mysql entry, and I would like PHP to make sure there is something in the textarea before updating the entry. This works when I only have one entry being updated, but when I do more than one at a time only the last one is updated. I tried using else if, and in that case only the first one is updated. How can I make it update all entries where text has been entered?

In my code the form input name matches the column name.

$value = $_POST['titleLeft'];
$value2 = $_POST['textLeft'];
$value3 = $_POST['imgName'];
$value4 = $_POST['titleRight'];
$value5 = $_POST['textRight'];

if (strlen($value) > 0){
    $sql = "UPDATE classesSpecial SET titleLeft = '$value' WHERE id = '1'";
}

if (strlen($value2) > 0){
    $sql = "UPDATE classesSpecial SET textLeft = '$value2' WHERE id = '1'";
}

if (strlen($value3) > 0){
$sql = "UPDATE classesSpecial SET imgName = '$value3' WHERE id = '1'";
}

if (strlen($value4) > 0){
    $sql = "UPDATE classesSpecial SET titleRight = '$value4' WHERE id = '1'";
}

if (strlen($value5) > 0){
    $sql = "UPDATE classesSpecial SET textRight = '$value5' WHERE id = '1'";
}

图片转代码服务由CSDN问答提供 功能建议

我知道类似的问题已经被问了很多,我已经搜索过,无法找到修复的答案 我遇到的问题。

我有一个包含多个textareas的表单来更新mysql条目,我希望PHP在更新条目之前确保textarea中有一些东西。 当我只有一个条目被更新时,这种方法有效,但是当我一次只做一个条目时,只更新最后一个条目。 我尝试使用else if,在这种情况下只更新第一个。 如何更新所有输入文本的条目?

在我的代码中,表单输入名称与列名称匹配。

  $ value = $ _POST ['titleLeft']; 
 $ value2 = $ _POST ['textLeft']; 
 $ value3 = $ _POST ['imgName']; 
 $ value4 = $ _POST ['titleRight']  ; 
 $ value5 = $ _POST ['textRight']; 
 
if(strlen($ value)> 0){
 $ sql =“UPDATE classesSpecial SET titleLeft ='$ value'WHERE id ='1'  “; 
} 
 
if(strlen($ value2)> 0){
 $ sql =”UPDATE classesSpecial SET textLeft ='$ value2'WHERE id ='1'“; 
} 
 
if  (strlen($ value3)> 0){
 $ sql =“UPDATE classesSpecial SET imgName ='$ value3'WHERE id ='1'”; 
} 
 
if(strlen($ value4)> 0  ){
 $ sql =“UPDATE classesSpecial SET titleRight ='$ value4'WHERE id ='1'”; 
} 
 
if(strlen($ value5)> 0){
 $ sql =“UPDATE  classesSpecial SET textRight ='$ value5'WHERE id ='1'“; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dthtvk3666 2015-06-09 09:04
    已采纳

    It's because you keep on overwriting $sql. One solution would be to do this for each post request:

    if((strlen($value) > 0) {
      $sql[] = "UPDATE classesSpecial SET titleLeft = '$value' WHERE id = 1";
    }
    

    $sql[] means you're adding values to an array, which you can later use to process all requests. Then, at the end of your script, you'll do this for executing your SQL:

    foreach($sql as $query) {
      mysqli_query($db, $query)or die(mysqli_error($db));
    }
    
    点赞 打赏 评论
  • doutan5798 2015-06-09 09:03

    You're overwriting the sql query with each if. you should also execute the command in the if statement.

    点赞 打赏 评论
  • doujue9767 2015-06-09 09:03

    You are using the same variable $sql everytime,

    $value = $_POST['titleLeft'];
    $value2 = $_POST['textLeft'];
    $value3 = $_POST['imgName'];
    $value4 = $_POST['titleRight'];
    $value5 = $_POST['textRight'];
    
    if (strlen($value) > 0){
        $sql1 = "UPDATE classesSpecial SET titleLeft = '$value' WHERE id = '1'";
    }
    
    if (strlen($value2) > 0){
        $sql2 = "UPDATE classesSpecial SET textLeft = '$value2' WHERE id = '1'";
    }
    
    if (strlen($value3) > 0){
    $sql3 = "UPDATE classesSpecial SET imgName = '$value3' WHERE id = '1'";
    }
    
    if (strlen($value4) > 0){
        $sql4 = "UPDATE classesSpecial SET titleRight = '$value4' WHERE id = '1'";
    }
    
    if (strlen($value5) > 0){
        $sql5 = "UPDATE classesSpecial SET textRight = '$value5' WHERE id = '1'";
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题