使用单选按钮和连接表获取MySQL数据

时间:2015-01-24 14:11:03

标签: php mysql button join radio

我的桌子:

表1名称:filmer 字段:id(主键,AI),titel,director,year,catid

表2名称:猫 字段:catid(主键,AI),类别

将电影添加到数据库可以正常使用提交表单,但我希望能够添加一个类别以及这里的单选按钮问题是我不知道我需要做什么和我尝试加入表与catid之间的关系,但它不会工作,我只得到

无法显示结果!

在我添加电影之后,我可以看到数据库中的电影,但不能在浏览器中看到它们在第一个表中都看到了catid 0,而在第二个表中我有6个类别,catid值来自1-6。

我是新手,所以我真的很感激任何帮助!

这是我的HTML表单和单选按钮

   
  <form action="movies.php" method="post">
    <input type="radio" name="id" value="1" checked />Action<br>
    <br> <input type="radio" name="id" value="2" />Comedy<br>
    <br> <input type="radio" name="id" value="3" />Drama<br>
    <br> <input type="radio" name="id" value="4" />Horror<br>
    <br> <input type="radio" name="id" value="5" />Romantic<br>
    <br> <input type="radio" name="id" value="6" />Animated<br><br>

    <pre>
    Title<input type="text" name="titel">
    Director<input type="text" name="director">
    Year <input type="text" name="year">
    <input type="submit" name="submit" value="Submit" />
    </pre>
  </form>

这是我的PHP文件,用于将电影添加到数据库中。

//Post data
if (isset($_POST['submit'])){

    $title = htmlentities($_POST['titel'], ENT_QUOTES);
    $director = htmlentities($_POST['director'], ENT_QUOTES);
    $year = htmlentities($_POST['year'], ENT_QUOTES);


    // empty form = error
    if ($title == '' || $director == '' || $year == ''){

        $error = 'ERROR: Please fill in all required fields!';
        echo $error;

      } else {

    //inserts movie to database
    if ($stmt = $mysqlic->prepare("INSERT INTO filmer 
       (titel, director, year) VALUES (?, ?, ?)")){

            $stmt->bind_param("ssi", $title, $director, $year);
            $stmt->execute();
            $stmt->close();


        // show an error if the query has an error
        } else {

          echo "ERROR: Could not prepare SQL statement.";
        }

        // redirec the user
        header("Location: view.php");
    }

}

以下是显示数据库中电影的PHP文件

// get movies from the database
 if ($result = $mysqlic->query("SELECT cat.catid FROM cat 
     INNER JOIN filmer ON cat.catid=filmer.catid")){

        // display records if there are records to display
        if ($result->num_rows > 0){


        // display records in a table
        echo "<table border='1' cellpadding='10'>";

        // set table headers
       echo "<tr><th>Title</th>
             <th>Director</th>
             <th>Year</th>
             <th>Category</th>";

        while ($row = $result->fetch_object()){


        // set up a row for each record
                echo "<tr>";
                echo "<td>" . $row->titel . "</td>";
                echo "<td>" . $row->director . "</td>";
                echo "<td>" . $row->year . "</td>";
                echo "<td>" . $row->Category . "</td>";
                echo "</tr>";
               }

                echo "</table>";

        // if there are no records in the database, display an alert message
                } else {

            echo "No results to display!";
            }

        // show an error if there is an issue with the database query
                } else {

            echo "Error: " . $mysqlic->error;
            }

// close database connection
    $mysqlic->close();

2 个答案:

答案 0 :(得分:0)

在您的桌子上添加新胶片时,您没有指定它的类别。 谁会猜到你需要什么类别? 所以你需要使用这个查询来澄清这个:

INSERT INTO filmer (titel, director, year, catid) VALUES (?, ?, ?, ?)

我添加了字段名称catid,如您的问题和额外?中所述 接下来你必须获得类别ID。这可以这样做:

$cat_id = intval($_POST['id']);

之后绑定参数:

 $stmt->bind_param("ssii", $title, $director, $year, $cat_id);

答案 1 :(得分:0)

如果我已经正确理解你想要将cat id和其他字段一起发布。我添加了cat id的名称,因此它被$ _POST捕获。然后将其放入插入查询中。

//Post data
if (isset($_POST['submit'])){

$title = htmlentities($_POST['titel'], ENT_QUOTES);
$director = htmlentities($_POST['director'], ENT_QUOTES);
$year = htmlentities($_POST['year'], ENT_QUOTES);
$catid= htmlentities($_POST['id'], ENT_QUOTES); //gets the catid


// empty form = error
if ($title == '' || $director == '' || $year == ''){

    $error = 'ERROR: Please fill in all required fields!';
    echo $error;

  } else {

//inserts movie to database
if ($stmt = $mysqlic->prepare("INSERT INTO filmer 
   (titel, director, year. catid) VALUES (?, ?, ?, ?)")){

        $stmt->bind_param("ssi", $title, $director, $year, $catid);
        $stmt->execute();
        $stmt->close();


    // show an error if the query has an error
    } else {

      echo "ERROR: Could not prepare SQL statement.";
    }

    // redirec the user
    header("Location: view.php");
}

}