【问题标题】:Fetch MySQL data using radio buttons and joining tables使用单选按钮和连接表获取 MySQL 数据
【发布时间】:2015-03-23 11:07:03
【问题描述】:

我的桌子:

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

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

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

没有结果可显示!

添加电影后,我可以在数据库中看到电影,但在浏览器中看不到,它们都在第一个表中得到 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();

【问题讨论】:

  • "INSERT INTO filmer (titel, director, year, catid) VALUES (?, ?, ?, ?)"
  • 记得抓住id $catid= htmlentities($_POST['id'], ENT_QUOTES); 的帖子,然后插入(如上)

标签: php mysql button join radio


【解决方案1】:

将新电影添加到您的表格时,您没有指定它的类别。 谁会猜你需要什么类别? 所以你需要用这个查询来澄清这一点:

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

我添加了字段名称catid,如您的问题中所述,额外添加了? 接下来,您必须获取类别 ID。这可以通过这种方式完成:

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

之后你绑定参数:

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

【讨论】:

    【解决方案2】:

    如果我理解正确,您想将猫 ID 与其他字段一起发布。我添加了猫 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");
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2011-06-20
      • 2016-03-23
      • 1970-01-01
      • 2011-11-03
      相关资源
      最近更新 更多