如何在更新时删除逗号?

时间:2017-03-21 10:30:44

标签: javascript php jquery html5

代码:

<?php
if(isset($_POST['save']))
{
    $checkbox1=$_POST['company_name'];  
    $chk="";  
    foreach($checkbox1 as $chk1)  
       {  
          $chk .= $chk1.",";  
       }  
    $sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
    $value = mysqli_query($link,$sql);
    if($value == true)
    {
        $msg .="<h5 style='color:green'>Successfull</h5>";
    }
    else
    {
        $msg .="<h5 style='color:red'>Error!</h5>";
    }
}

&GT;

当我点击保存按钮时,数据将存储为a,b,c,。所以,我的问题是如何在存储数据库时删除最后一个逗号,即(c,)。

谢谢

8 个答案:

答案 0 :(得分:3)

在使用它之前修剪它:

$chk = rtrim($chk, ",");
// $sql = "update all_u...

答案 1 :(得分:3)

请改用阵列。然后,您可以使用逗号来压缩值:

$chk = array();  
    foreach($checkbox1 as $chk1){  
          $chk[] = $chk1;  
    } 
$chk = implode(",",$chk);

或类似的东西:

$chk = implode(",",$checkbox1);

答案 2 :(得分:0)

不要单独添加每个值,而应使用phps implode

$chk = implode(",", $checkbox1);

答案 3 :(得分:0)

您可以使用$chk = rtrim($chk, ",");

答案 4 :(得分:0)

以下是更新后的代码

 <?php
    if(isset($_POST['save']))
    {
        $checkbox1=$_POST['company_name'];  
        $chk="";  
        foreach($checkbox1 as $chk1)  
           {  
              $chk .= $chk1.",";  
           }  
        $chk = substr($chk, 0,-1);
        $sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
        $value = mysqli_query($link,$sql);
        if($value == true)
        {
            $msg .="<h5 style='color:green'>Successfull</h5>";
        }
        else
        {
            $msg .="<h5 style='color:red'>Error!</h5>";
        }
    }
    ?>

答案 5 :(得分:0)

就像使用implode()函数一样简单,不需要像a,b,c那样使用循环来处理数组。如果您认为某些company_name可能也是空白,那么您的array_filter()。喜欢:$_POST['company_name'] = array_filter($_POST['company_name']);

我担心:$chk = implode(",", $_POST['company_name']);

完整示例:

if(isset($_POST['save'])) {
    $chk = implode(",", $_POST['company_name']);  
    $sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
    $value = mysqli_query($link,$sql);
    if($value == true){
        $msg .="<h5 style='color:green'>Successfull</h5>";
    } else {
        $msg .="<h5 style='color:red'>Error!</h5>";
    }
}

答案 6 :(得分:0)

清洁代码是

<?php
if(isset($_POST['save']))
{        
   $chk=""; 

   if(isset($_POST['company_name']) != '') {
      $chk = implode(", ", $_POST['company_name']);
   }

   $sql = "update all_university set placement = '$chk' where  university_name = '".$_POST['university_name']."'";
   $value = mysqli_query($link,$sql);
   if($value == true)
   {
        $msg .="<h5 style='color:green'>Successfull</h5>";
    }
    else
    {
        $msg .="<h5 style='color:red'>Error!</h5>";
    }
}

答案 7 :(得分:0)

<?php
if(isset($_POST['save']))
{
    $checkbox1=$_POST['company_name'];  
    $chk="";  
    /*foreach($checkbox1 as $chk1)  
       {  
          $chk .= $chk1.",";  
       }
*/

//use this code instead your foreach loop
$chk = implode(",",$checkbox1);  
    $sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
    $value = mysqli_query($link,$sql);
    if($value == true)
    {
        $msg .="<h5 style='color:green'>Successfull</h5>";
    }
    else
    {
        $msg .="<h5 style='color:red'>Error!</h5>";
    }
}