duanchuan6350
2013-10-21 14:15
浏览 66
已采纳

使用PHP向DB添加多个字段

I am currently using the following function:

    if(isset($_REQUEST["function"]) && ($_REQUEST["function"] == "setnm")){
$value = $_REQUEST["value"]; //field to edit

$con=mysqli_connect("localhost", "root", "", "hike_buddy");
//Check Connection

if(mysqli_connect_errno())
{
    echo "failed to connect:".mysqli_connect_error();
}

mysqli_query($con, "INSERT INTO user_com (name) VALUES ('$value')");
mysqli_close($con);

}

How can I alter this code so it will change the value of two fields?

For instance I have a comment and a name column and I want to update them both (different values) with one function.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • dpd3447 2013-10-21 14:29
    最佳回答

    Never use un-escaped strings specified by the user in your database queries.

    So, if you're using mysqli:

    $value1 = $con->real_escape_string($_REQUEST['value_1']);
    $value2 = $con->real_escape_string($_REQUEST['value_2']);
    $query = "INSERT INTO my_table (column_1, column_2) VALUES ('$value1', '$value2')";
    $con->query($query);
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题