doujian3132 2016-01-02 10:42
浏览 36
已采纳

我在变量中有一些值(以逗号分隔)。 我想逐个在数据库中插入这些值

My Code is given below :

$trend=5,6,7;
$variableAry=explode(",",$trend); 
            foreach($variableAry as $var)
            {
    $sql="insert into trending_lawyer(id,lawyers_id)values('','$var')";
    $query=mysql_query($sql);
}

Please suggest me to what to do/modify in this code to insert values in database.

  • 写回答

2条回答 默认 最新

  • dsm0688 2016-01-02 10:49
    关注

    What about doing it something like:

    $trend='5,6,7';
    $variableAry=explode(",",$trend); 
    foreach($variableAry as $var)
    {
        $sql="insert into trending_lawyer(id,lawyers_id)values('','$var')";
        $query=mysql_query($sql);
    }
    

    There are other ways to optimize you code, but, for starters, this should work for you.

    Just realized that you are trying to insert id as part of insert statement, if id is a PR and AI column, you can skip it in you insert statement, so, you code will look like:

    $trend='5,6,7';
    $variableAry=explode(",",$trend); 
    foreach($variableAry as $var)
    {
        $sql="insert into trending_lawyer(lawyers_id)values('$var')";
        $query=mysql_query($sql);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?