2017-03-21 168 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>"; 
    } 
}