douben8492 2016-06-22 11:41
浏览 122
已采纳

如何在插入数据库之前根据第一个字母(表达式)修剪前三个字符

my php file

This is my code to retrieve data from database.

Here i need to remove first three characters based on the first expression(+) and followed by two numbers i.e(91). So totally i need to remove phone numbers which have +91 in database. Can anyone help me regarding this.

    <?php
    session_start();


    $response = array();

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

    if(!mysqli_connect_errno()){

        $error_flag = false;

    $contacts = json_decode($_POST['contacts'], true);
    foreach($contacts as $contact){

            //$trimmed = $contact['phone'];

            //$title = str_replace("+91", "", trim($trimmed));
            // $prefix = '+91';
            // $str = $contact['phone'];
            // if (substr($str, 0, strlen($prefix)) == $prefix) 
            //     { echo $str = substr($str, strlen($prefix)); }


            $sql = "INSERT INTO contacts (vault_no , name, phone, created_at)
            VALUES ('".$contact['vault_no']."', '".$contact['name']."', REPLACE('".$contact['phone']."','+91',''), NOW())";

                if(mysqli_query($con,$sql)){

                    echo "Successfully Saved";

                }else{
                    $response["error"] = true;
                    $response["error_msg"] = "INSERT operation failed";
                    echo json_encode($response);
                }
                    //}
    }

    }else{
        $response["error"] = true;
        $response["error_msg"] = "Database connection failed";
        echo json_encode($response);
    }
?>

this is my contact list in database

  • 写回答

4条回答 默认 最新

  • doutangdan3588 2016-06-22 11:43
    关注

    You can simply use REPLACE() :

    SELECT REPLACE(t.mobile,'+91','') as mobile
    FROM YourTable t
    

    Or if you want to change it in the database :

    UPDATE YourTable t
    SET t.mobile = REPLACE(t.mobile,'+91','')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM