Error message not displaying html forms using php [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a basic form trying to validate that a checkbox is selected, at the moment it is not allowing it to go through (expected) however my error message is not displaying on the same page, it is probably something really small that is missing.
It not displaying the error, a span class can be seen in the html below.
Where's the problem?
php:
<?php
require_once 'db/connect.php';
$error='';
$success='';
if (isset($_POST['submit'])){
if ( (isset($_POST['checkbox'])) && (isset($_POST['competitorDelete'])) ) {
$checkbox = $_POST['checkbox'];
for ($i=0;$i<count($checkbox);$i++) {
$delete_comp = $checkbox[$i];
$query = $con->query("SELECT Forename, Surname FROM student WHERE Student_ID = '" . $delete_comp . "'");
$row = mysqli_fetch_assoc($query);
$success = $row['Forename'] . ' ' . $row['Surname'] . ' has been deleted as a competitor from any events they were submitted for <br>';
$query= $con->query("DELETE FROM competitors WHERE Student_ID = '" . $delete_comp . "'");
}
}
elseif (isset($_POST['checkbox'])) {
$checkbox = $_POST['checkbox'];
for ($i=0;$i<count($checkbox);$i++) {
$delete_student = $checkbox[$i];
$query = $con->query("SELECT Forename, Surname FROM student WHERE Student_ID = '" . $delete_student . "'");
$row = mysqli_fetch_assoc($query);
$success = $row['Forename'] . ' ' . $row['Surname'] . ' has been deleted as a student <br>';
$query= $con->query("DELETE FROM student WHERE Student_ID = '" . $delete_student . "'");
}
}
else {
$error = 'A student must be selected';
}
}
?>
html:
<?php
session_start();
require_once 'db/checkuserloggedin.php';
include 'db/header.php';
include 'deletestudent.php';
echo '<h3> Delete students </h3>';
echo "<form method =\"POST\">";
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Forename, Surname, Student_ID " .
"FROM student, teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo '<table>';
while ($row1 = $student_result->fetch_assoc()) {
echo '<tr>';
echo '<td>';
echo $row1['Forename'] . ' ' . $row1['Surname'];
echo '</td>';
echo '<td>';
echo '<input type="checkbox" name="checkbox[]" value="' . $row1['Student_ID'] . '">';
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
}
echo 'Delete competitor data only<input type="checkbox" name="competitorDelete">' . '<br>';
echo '<input type="submit" name="submit" value ="Delete">';
echo '<input type="reset" value ="Reset">';
echo '<span class="error"><?php echo $error;?></span>';
echo "</form>";
?>
<!DOCTYPE html>
<html>
<head>
<title>Entry form</title>
</head>
<body>
<div id="logoutbutton">
<button class="btn" onclick="location.href='logout.php'">Logout</button>
</div>
<link rel="stylesheet" type="text/css" href="styles.css">
</body>
</html>

The reason why your error is not showing is because you're already inside PHP in doing an echo and have PHP tags.
echo '<span class="error"><?php echo $error;?></span>';
You can either do: (escaping double quotes for the CSS class name)
echo "<span class=\"error\">$error</span>";
or concatenate the $error variable:
echo '<span class="error">'.$error.'</span>';
Plus, variables do not get parsed inside single quotes, unless concatenated.

Related

Adding a validation check to a form in a while loop php

I have a form which displays like this:
Event Name : Drop down menu
I am trying to add a check that ensures that each event produced by the while loop has a student assigned to it - by selecting from the drop down menu.
I have attempted adding a check for this but its not making a difference - it loads form action page 'savecompetitors'.
I have got this for php so far:
<?php
session_start();
require_once 'db/connect.php';
require_once 'db/checkuserloggedin.php';
include 'db/header.php';
echo $_SESSION['Username'] . ' logged in successfully';
echo '<h3> Entry form </h3>';
//Query to display all events
if ($event_result = $con->query("SELECT Name FROM event")) {
echo "<form method =\"POST\" action=\"savecompetitors.php\">";
echo '<table>';
while ($row = $event_result->fetch_assoc()) {
echo '<tr>';
echo '<td>';
echo $row['Name'] . ' ';
echo '</td>';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Forename, Surname, Student_ID " .
"FROM student, teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")
) {
if ($student_result->num_rows) {
echo '<td>';
echo "<select name ='" . $row['Name'] . "'>";
while ($row1 = $student_result->fetch_assoc()) {
echo '<option value="" style="display:none;"></option>';
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
if (isset($_POST['submit'])) {
if (empty($_POST['Student_ID'])) {
$error = 'A student must be selected for every event';
}
}
}
echo "</select>";
echo '</td>';
echo '</tr>';
}
}
}
echo '</table>';
echo '<input type="submit" name="submit" value ="Submit" >';
echo '<input type="reset" value ="Reset">';
echo '<span class="error"><?php echo $error;?></span>';
echo '<span class="error"><?php echo $success;?></span>';
echo "</form>";
} else {
echo 'No student records available';
}
savecompetitors php:
<?php
require_once 'db/connect.php';
$error = '';
$success = '';
$event_result = $con->query("SELECT Event_ID, Name from event");
while ($row = $event_result->fetch_assoc()) {
$eventname = str_replace(' ', '_', $row['Name']);
print_r($row);
$con->query("INSERT INTO competitors (Event_ID, Student_ID) VALUES (" . $row['Event_ID'] . ", " . $_POST[$eventname] . ") ");
$success = 'Entry form has been successfully saved and students are entered as competitors for their submitted events';
}

Adding css to a form

I am trying to add CSS to my form but not sure how to do this. The form is created in php and MySQL, in browser it looks like: http://gyazo.com/5d099ead9bd6ea83859a5114b2438748
I need to allign the text and drop downs so they are in the equal throughout and add some spacing. Anyone help with CSS for this?
html currently:
<div class="wrap">
<img src="/images/logo.png" alt="Highdown logo" />
<h1>Entry form</h1>
</div>
css currently:
.wrap {
position: relative;
}
The form is produced with this:
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\" action=\"save.php\"> ";
while ($row = $event_result->fetch_assoc()) {
echo $row['Name']. ' ';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo "<select name ='". $row['Name']."'>";
while ($row1 = $student_result->fetch_assoc()) {
echo '<option value="" style="display:none;"></option>';
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select> <br />";
}
}
}
echo '<input type="submit" value ="Submit">';
echo '<input type="reset" value ="Reset">';
echo '<input type="button" value = "Add student" onclick="location.href=\'http://localhost/sportsday/addstudent.php\'">';
echo '<input type="button" value = "Delete student">';
echo "</form>";
}
Use
<form>
<table>
<tr> //1st Table row
<td></td> //Table column data
<td></td> //table column data
</tr> //1st row ends
<tr> // 2nd Table row
<td></td> //Table column data
<td></td> //table column data
</tr> //2nd row ends
</table>
</form>
This will give you a better layout of the form.
This should work i did not try as i dont have the database
//Query to display all events
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\" action=\"save.php\"> ";
echo '<table>';
echo '<tr>';
echo '<td>';
while ($row = $event_result->fetch_assoc()) {
echo $row['Name']. ' ';
echo '</td>';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo '<td>';
echo "<select name ='". $row['Name']."'>";
while ($row1 = $student_result->fetch_assoc()) {
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select> <br />";
echo '</td>';
echo '</tr>';
}
}
}
echo '</table>';
echo '<input type="submit" value ="Submit">';
echo '<input type="reset" value ="Reset">';
echo '<input type="button" value = "Add student" onclick="location.href=\'http://localhost/sportsday/addstudent.php\'">';
echo '<input type="button" value = "Delete student">';
echo "</form>";
}
?>
you can directly write in css
form {
⋮ declare css
}
or give name to form
form[name="value"]{
⋮ declare css
}
or add any class or id on form
#formid{
⋮ declare css
}
.formclass{
⋮ declare css
}
First , check your database...
May be there is Another Issue not related to Tabular Output.
So , First remove Table Tag..and check whether its working ?
Then try in HTML TABLE TAG
Otherwise give me sample database .sql File and complete PHP code in google drive or on shared drive.
So that I can check and identify where is problem ?

move_uploaded_file doesn't work in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm doing an examination system using PHP. I'm trying to add questions with images and move the image to a designated folder for retrieving purposes. I have the following code:
$sql = 'SELECT q_category, q_image, q_question, q_correct, q_answer2, q_answer3, q_answer4 FROM tblquestions WHERE q_question = "' . $question . '"';
$retval = mysql_query($sql,$conn);
if(!$retval)
{
die('Could not get data: ' . mysql_error());
}
while($row1 = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$cat = $row1['q_category'];
$quest = $row1['q_question'];
$image = $row1['q_image'];
$correct = $row1['q_correct'];
$ans2 = $row1['q_answer2'];
$ans3 = $row1['q_answer3'];
$ans4 = $row1['q_answer4'];
echo '<tr>';
echo '<td colspan="2"><div class="selectText" align="center">Edit Questions</div></td></tr>';
echo '<tr>';
echo '<td>Category</td>';
echo '<td><input type="text" name="txtCategory" value = "' . $cat . '" size="38"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Question</td>';
echo '<td><textarea rows="4" cols="34" name="txtQuestion" id="txtQuestion" class="addNew">' . $quest . '</textarea>';
echo '</tr>';
echo '<tr>';
echo '<td>Image</td>';
echo '<td><input type="file" rows="4" cols="28" name="txtImage" id="txtImage" class="addNew"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Correct Answer</td>';
echo '<td><input type="text" name="txtCorrect" value = "' . $correct . '" size="38"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Answer 2</td>';
echo '<td><input type="text" name="txtChoice2" value = "' . $ans2 . '" size="38"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Answer 3</td>';
echo '<td><input type="text" name="txtChoice3" value = "' . $ans3 . '" size="38"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Answer 4</td>';
echo '<td><input type="text" name="txtChoice4" value = "' . $ans4 . '" size="38"></td>';
echo '</tr>';
echo '<input type="hidden" name="hiddenQuestion" value = "' . $question . '">';
}
if(isset($_POST['submit1']))
{
$hiddenQuestion = $_POST['hiddenQuestion'];
$addCategory = $_POST['txtCategory'];
$addQuestion = $_POST['txtQuestion'];
$addCorrect = $_POST['txtCorrect'];
$addChoice2 = $_POST['txtChoice2'];
$addChoice3 = $_POST['txtChoice3'];
$addChoice4 = $_POST['txtChoice4'];
$filetmp = $_FILES['txtImage']['tmp_name'];
$filename = $_FILES['txtImage']['name'];
$filepath = "questionImages/".$filename;
move_uploaded_file($filetmp,$filepath);
After I click submit, the $filename is empty in the database and the move_uploaded_file doesn't work. If I change $filename = $_FILES['txtImage']['name'] to $filename = $_POST['txtImage'], it adds the the name to the database but still the move_uploaded file doesn't work.
QUESTION
I'm thinking that there is something wrong with my $filetmp and $filename. Can anyone point out what's wrong in my code? Thanks in advance :)
$filename and $filetype seems ok.
Check in your upload form whether you included enctype="multipart/form-data".
Also include your form code in the question.

values from form not being passed php html mysqli

I can't figure out why my values aren't being passed from the form. I can't spot an error.
The Form Code:
$table = $_POST['table'];
$id = $_POST['id'];
$count = 0;
$query = "SELECT * FROM `" . $table . "` WHERE id = " . $id;
$result1 = mysqli_query($link, $query);
echo '<center><table style="text-align:center">';
echo '<form action="edit-process.php" method="post">';
while($row = mysqli_fetch_assoc($result1)){
foreach($row as $key => $val){
if ($count > 0){
echo "<tr>";
echo "<td>" . $key . "</td>";
echo '<td><input type="text" name="' . $key . '" value="' . $val . '"></td>';
echo "</tr>";
$count++;
}
else $count++;
}
}
echo '<input type="hidden" name="table" value="' . $table . '" />';
echo '<input type="hidden" name="id" value="' . $id . '" />';
echo '<tr><td><input type="submit" value="Save Changes" /></td></tr>';
echo "</form>";
echo "</table>";
The php file:
$table = $_POST['table'];
$id = $_POST['id'];
$count1 = 0;
$count2 = 0;
$result = mysqli_query($link, "SHOW COLUMNS FROM `" . $table . "`");
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$keyNames[$count2] = $row['Field'];
$count2++;
}
}
while ($count1 < $count2){
if ($count1 > 0) {
$value = mysqli_real_escape_string($_POST[$keyNames[$count1]]);
$query2 = "UPDATE `" . $table . "` SET `" . $keyNames[$count1] . "` = '" . $value . "' WHERE id = " . $id;
echo $query2 . "<br>";
$result2 = mysqli_query($link, $query2);
$count1++;
}
else $count1++;
}
I am avoiding displaying the id column with all the counts. The output of the echo-ed queries are:
Any ideas?
EDIT
I'll take care of changing everything over to procedural style once I figure out this issue. If I get rid of the mysqli_real_escape_string, it passes all the data except those columns with spaces in them. I thought that's what backticks were for? Is there something else I can do to make the columns with two words pass data like those with one word?
You need to switch these rows -
echo '<center><table style="text-align:center">';
echo '<form action="edit-process.php" method="post">';
....
echo "</form>";
echo "</table>";
to
echo '<form action="edit-process.php" method="post">';
echo '<center><table style="text-align:center">';
....
echo "</table>";
echo "</form>";
Having the <form> inside the <table> is invalid code. It either needs to wrap the <table> or be inside <td></td>.
see also -
form inside table
Form inside a table
Update #1-
On your Edit
Spaces in <input name=""> will be replaced with _ so your $_POST[] name will not match your <input name="">. from the manual - http://www.php.net/manual/en/language.variables.external.php
Note:
Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].
see also -
Can <input> elements have capital letters and spaces in PHP

Check Checkbox depending on value in database

I have an code that gets the 'branches' from the database. Each company can have multiple 'branches'.
Only thing is, that is doesn't work. Can you guys figure out what's wrong?
$getbranches = "SELECT * FROM branches ORDER BY naam ASC";
$querygetbranches = mysql_query($getbranches);
while($rijbranche = mysql_fetch_assoc($querygetbranches))
{
echo "<tr>";
echo "<td width='400'>";
echo $rijbranche['naam'];
echo "</td>";
echo "<td>";
$get2 = "SELECT * FROM bedrijf_branche WHERE bedrijf_id = '$id'";
$query2 = mysql_query($get2);
while ($rij20 = mysql_fetch_assoc($query2))
{
$branche_id = $rij20['branche_id'];
}
if($branche_id == $rijbranche['id_branche']){
?>
<input type="checkbox" name="branche[]" value="<?php echo $rijbranche['id_branche']; ?>" CHECKED></input>
<?php
}
else
{
?>
<input type="checkbox" name="branche[]" value="<?php echo $rijbranche['id_branche']; ?>"></input>
<?php
}
echo "</td>";
}
Try the following code
<?php
$id = $_GET['id'];
// Output BRANCHES
$getbranches = "SELECT * FROM branches ORDER BY naam ASC";
$querygetbranches = mysql_query($getbranches);
while ($rijbranche = mysql_fetch_array($querygetbranches)) {
echo ' <tr>' . "\n";
echo ' <td width="400">' . $rijbranche['naam'] . '</td>' . "\n";
// Output CHECKBOX
$get2 = mysql_query("SELECT * FROM bedrijf_branche WHERE bedrijf_id = '" . $id . "' AND branche_id = '" . $rijbranche['id_branche'] . "'");
$rij20 = mysql_fetch_array($get2);
$branche_id = $rij20['branche_id'];
if ($branche_id == $rijbranche['id_branche']) {
$checkbox = '<input type="checkbox" name="branche[]" value="' . $rijbranche['id_branche'] . '" checked="checked" />';
}
else {
$checkbox = '<input type="checkbox" name="branche[]" value="' . $rijbranche['id_branche'] . '" />';
}
echo ' <td>' . $checkbox . '</td>' . "\n";
echo ' </tr>' . "\n";
}
?>
Found a couple of errors I fixed in the above code.
You're closing the <input> fields incorrectly
Your second while() loop is unnecessary as there should only be one row returned
You have to add branche_id to your second mysql_query!
Don't close and re-open your <?php ?> tags for every HTML line when you can just add an echo
Your HTML-syntax is wrong.
The way you close the input tag and the way you want to check the chechbox is wrong
Try this
<input type="checkbox" name="branche[]" value="<?php echo $rijbranche['id_branche']; ?>" checked="checked" />

Categories