2017-03-21 101 views
-1

代码:如何在更新时删除逗号?

<?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>"; 
    } 
} 

>

当我点击保存按钮的数据将存储像,B,C,。所以,我的问题是如何删除最后的逗号,同时存储在数据库中,即(c,)。

谢谢

+0

http://php.net/manual/en /function.join.php – Rayon

+3

你也可以使用php的'implode''爆炸“功能 –

+0

为什么不尝试'trim()'函数?你也可以使用'implode()' –

回答

3

使用前只需修剪掉:

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

在这里,你去10K的家伙! – mehulmpt

+3

这是一个伟大的日子。大声笑。 –

3

使用数组来代替。然后,您可以用逗号爆值:

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

或者是这样的:

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

这将是近似最好的解决方案... –

0

而不是增加每个值separetely你建议立即进行删除使用PHPS implode的:

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

伙计,你错过了你的胶水。 – JustBaron

0

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

+0

有些人可能不熟悉'rtrim'函数。请提供解释这个答案是如何工作的。 – sorak

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>"; 
     } 
    } 
    ?> 
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>"; 
    } 
} 
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>"; 
    } 
} 
+1

你在错误的地方得到了胶水。 – JustBaron

+0

感谢justbaron,在发布后立即注意到了。但现在它是正确的。 –

+0

远离_clean密码_... –

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>"; 
    } 
}