I'm creating a website which is for students in university to review their school. Since there are a lot of schools, I need to build a page that can take data from the database to show on the page, at the same time, I also put a form inside it, so they could comment and rate it. I identify each school by its school_id. This is what I'm trying:
This is the code the page before entering the dynamic page:
<?php
$sql = "SELECT * FROM schools WHERE state_id=10001";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo"<form action='../school.php' method='post'><div class='states&school'>
<h3><button class='searchedschool' value=".$row['school_id']." name='id'>"
.$row['school_name']."</button>
</h3>
</div>
</form>";
}
}
?>
And this is the code on the second page
<?php $sid=$_POST['id'];
if (isset($_POST['submit'])) {
$nickname=$_POST['user_nickname'];
$email=$_POST['user_email'];
$comment=$_POST['cmt_text'];
$course=$_POST['user_course'];
$rating=$_POST['user_rating'];
$school_id=$sid;
$db="INSERT INTO
comments(user_nickname,user_email,cmt_text,user_course,user_rating,school_id) VALUES(?,?,?,?,?,?)";
$stmt=mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt,$db)){
echo "Data Error";
}else{
mysqli_stmt_bind_param($stmt,"ssssii", $nickname,$email,$comment,$course,$rating,$school_id);
mysqli_stmt_execute($stmt);
}
header("location:School.php");
exit;
}?>
This is the form
<form action="school.php" method="POST" id="myForm" class="form">
<div class="row">
<div class="input-group">
<label for="name">Name</label>
<input class="typingbox" minlength="5" type="text" name="user_nickname" id="name" placeholder="Enter your Name" required>
</div>
<div class="input-group">
<label for="email">Email</label>
<input class="typingbox" type="email" name="user_email" id="email" placeholder="Enter your Email" required>
</div>
<div class="input-group">
<label for="Course">Course</label>
<input class="typingbox" type="text" minlength="6" name="user_course" id="course" placeholder="Enter your Course" required>
</div>
<div class="input-group">
<label for="rating">Rating (1-5) </label>
<input class="typingbox" type="number" min="0" max="5" name="user_rating" id="rating" required>
</div></div>
<div class="input-group textarea">
<label for="comment">Comment</label>
<textarea id="comment" minlength="50" class="typingbox" name="cmt_text" placeholder="Enter your Comment" required></textarea>
</div>
<div class="input-group">
<button name="submit" class="btn-btm">Submit</button>
<button type="button" class="btn-btm" onclick="closeForm()">Close</button>
</div></form>
This is one of the examples of how I present data from database
<h1 class="schoolheading"><?php
$result = mysqli_query($con,"SELECT school_name FROM schools WHERE school_id=$sid");
while($row = mysqli_fetch_array($result)) {
echo $row['school_name'];}
?></h1>
I didn't show the rest of the code because there's too many.
So basically every time I clicked the submit button, the data are not inserted into the database. Then I will be redirected to the same file but not showing any content and says that 'Undefined array key "id". How can I correct it? Or I should make my page the other way, as I'm not sure this is the correct way to make a dynamic page. I'm sure that my files are connected to the database.
Aucun commentaire:
Enregistrer un commentaire