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.
Related
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.
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 ?
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
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" />
How do i pass id to delete record in this code?
<form action="index.php">
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
</form>
The above code is in index.php and it is submitting it to itself.
Without needing javascript, seperate GET urls etc, just plain old HTML & the original POST: just add the ID to the name of the button:
<input type="submit" value="Delete" name="btnDel[<?php echo $id;?>]">
And in receiving code:
if(isset($_POST['btnDel']) && is_array($_POST['btnDel'])){
foreach($_POST['btnDel'] as $id_to_delete => $useless_value){
//delete item with $id_to_delete
}
}
Here's how I would do it:
Change
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
To
echo '<td><input type = "button" value = "Delete" onclick = "btnDel(' . $row['id'] . ')" /></td>';
Add the following field to the form (outside of the while loop of course):
<input type="hidden" name="id" id="userid" />
Define the following javascript function:
function btnDel(id) {
if (confirm("Really delete id=" + id + "?")) {
document.getElementById('userid').value = id;
document.forms[0].submit();
}
}
Then you can retrieve that value using $_GET['id'] or $_POST['id'] depending on your form's method.
EDIT: Here's a working demo, and its source
Use this to submit the id as a part of form.
<input type="hidden" id="id" name="id" value="<? echo $row['id']; ?>" />
or you can send values in URL to do the same thing
An example:
Delete
A full working sample
<?
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
switch($_GET['action']) {
case "delete":
$query = "DELETE FROM tbluser WHERE id='".$_GET['id']."'"
$result = mysql_query($query);
break;
//well other actions
}
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
You could also send it in the url at action="index.php" given that you move it below while(($row = mysql_fetch_array($query)) != NULL) { like this
echo "<form action='index.php?var=" . $row['id'] . "'>";
You would then get the variable using $_GET['var']
If the id needs to stay hidden (on the page and in the link) a hidden input element and submitting the form using method="post" like previously suggested would be the better way to go.
From the table:
echo '<input type="hidden" name="user_id" value="'.$row['id'].'" />';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
This will be sent to the server as a parameter named user_id. Ideally, you should be using the POST method in the form.
This assumes that the user id is named, well id in the table.
BTW, the != NULL is unnecessary.
It's sufficient to write:
while($row = mysql_fetch_array($query)) {
NULL evaluates to FALSE in a boolean context.
Update:
As mentioned in comments to another answer, there is an issue with your approach. With multiple rows, you won't be able to distinguish between user ids.
One solution would be use multiple forms.
Another option would be to have the name of the submit button include the id. You'd then parse this out of the name of the $_POST array keys.