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

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.

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);
}
$variableAry = explode(",", $trend);
$sql = "insert into trending_lawyer(id,lawyers_id)values";
for ($i = 0; $i < count($variableAry); $i++) {
    $sql.="('','$variableAry[$i]'),";
}
$sql=trim($sql,',');
$query = mysql_query($sql);

Query looks like :

insert into trending_lawyer(id,lawyers_id)values('','5'),('','6'),('','7')