我正在尝试创建一个页面,当用户在他们的博客帖子上点击提交时,它会将其发布到预先存在的数据库表。我一直得到的错误是我没有正确地从博客文章到博客表引用表id(tid)。我想如果我将单选按钮的值更改为相应的表id值并使用它们进行一些循环它会起作用,但我仍然不确定如何引用它。真的卡在这一点上。非常感谢任何帮助!
这是我的单选按钮,用于选择要查看的表格:
first.php
<form name ="myForm" action ="second.php<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method = "POST">
<h3> Guitar Blog Topics </h3><br>
<input type ="radio" name = "radio1" value ="Topic1" /> Topic1 <br>
<input type ="radio" name ="radio1" value ="Topic2" /> Topic2 <br>
<input type ="radio" name = "radio1" value ="Topic3" /> Topic3 <br>
<input type = "submit" value = "Select"/>
</form>
我认为我的问题可能就在这里,硬编码主题名称而不是仅将标题ids(tid)作为值1,2,3。
在我的$ sql的本页底部是我不知道如何引用我的外键(tid)。
second.php
$answer = $_POST['radio1'];
$query = "SELECT * from blog_posts join blog_topics using (tid) where topic = '$answer'";
$result = mysqli_query($dbconnect, $query);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) // build a table to show results
{
echo "<table border='1'>";
echo "<tr>";
echo "<th>Post ID </th>"; echo "<th>Author</th>";
echo "<th>Title</th>"; echo "<th>Post</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['pid'] . "</td>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['post'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else{
echo "No rows returned.";
}
?>
<form name ="myForm" action ="" method = "POST"> <br><br><br>
<h3> Create a Post </h3>
Author <input type ="text" size ="40" name ="author"/><br>
Title <input type ="text" size ="30" name ="title"/><br><br>
Post <br><textarea rows ="15" cols ="10" name ="post"></textarea><br>
<input type ="submit" name = "submitpost" value ="Submit Post"/>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['submitpost'])){
$sql = mysqli_query($dbconnect, "INSERT INTO blog_posts (pid, author, title, post, tid)
VALUES (NULL, '$_POST[author]', '$_POST[title]', '$_POST[post]')") //THIS IS WHERE I NEED TO REFERENCE TID
or die(mysqli_error($dbconnect));
}
答案 0 :(得分:0)
您的列列表与值列表不匹配,因此您尝试输入不存在的内容。尝试
$tid = 'my table id'; // if the datatype of the tid column is varchar
$tid = 3456; // if the datatype of the tid column is int
$tid = $_POST[tid];// make sure when using the $_POST variable that the value matches the datatype
$sql = mysqli_query($dbconnect, "INSERT INTO blog_posts (pid, author, title, post, tid) VALUES (NULL, '$_POST[author]', '$_POST[title]', '$_POST[post]', $tid)")