数组到字符串转换

时间:2013-09-07 13:11:50

标签: php html

尝试将表单值提交到MySQL数据库。但是我收到错误消息:

  

第45行的数组到字符串转换。

我唯一拥有的数组是捕获空字段的$error数组。

  if(isset($_POST['submitted'])){

    $errors = array(); // Initialize erroy array.

    // Check for title.
    if (empty($_POST['movie_name'])){
        $errors[] = "You forgot to enter a title.";
    } else {
        $mn = (trim($_POST['movie_name']));
    }

    if (empty($_POST['leading_actor'])){
        $errors[] = "You forgot to enter the leading actor.";
    } else {
        $la = (trim($_POST['leading_actor']));
    }

    if (empty($_POST['rating'])){
        $errors[] = "Please select a rating.";
    } else {
        $rating = ($_POST['rating']);
    }

    if (empty($_POST['review'])){
        $errors[] = "Please write a review";
    } else {
        $review = (trim($_POST['review']));
    }


    if (empty($errors)) { // If no errors were found.

        //require_once('./includes/Mysql_connect.php');

        // Make the insert query.

        $query = "INSERT INTO film (movie_title, actor, fk_movie_ratings)
     **This is line 45**   Values ('$mn', '$la', '$rating')";

       $result = mysql_query($query);


        //Report errors.
    } else {
        foreach ($errors as $msg){
            echo " - $msg <br/> ";
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为这是可行的唯一方法是当你有这样的HTML表单时:

<input type="text" name="rating[]">

由于“[]”,php会创建一个存储在$ _POST ['rating']中的数组。 但我想这不是你正在做的事情:)

顺便说一句,避免将POST / GET输入直接放入MySQL查询中。请使用预准备语句,否则会冒SQL注入风险。