PHP, MYSQL and HTML FORMS not working together - php

I have this code but when I press the submit button No data is being transferred via Get, with the exemption on the submit.
<table cellspacing="0px">
<tr>
<td>Name</td><td>Today - <?php echo $date;?></td>
</tr>
<form method="get" action="update_reg.php">
<?php
$result = mysqli_query($con,"SELECT * FROM TABLE WHERE GROUP = 'Penguins' ORDER BY Rank, Name ");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>";
if($row['Rank'] == "a"){
$rank = "TOP ";
}
if($row['Rank'] == "b"){
$rank = "MIDDLE ";
}
if($row['Rank'] == "c"){
$rank = "SECOND ";
}
if($row['Rank'] == "d"){
$rank = "BOTTOM ";
}
if($row['Rank'] == "e"){
$rank = "";
}
echo $rank . $row['Name'] . "</td>";
$num = $num + 1;
echo "<td><input type=\"text\" class=\"today\" id=\"" . $row['id'] . "\" data-number=\"" . $num . "\" size=\"1\" maxlength=\"1\"></td></tr>";
}
?>
</table>
<input type="submit" value="submit">
</form>
For some reason this isn't working, anyone got any ideas why? Thanks in advance.

You need to add a value="" attribute and name="" attribute to your <input>s.
For example:
echo "<td><input type=\"text\" class=\"today\" id=\"" . $row['id'] . "\" name=\"" . $row['id'] . "\" data-number=\"" . $num . "\" value=\"" . $num . "\" size=\"1\" maxlength=\"1\"></td></tr>";
I'm not sure what you're trying to submit exactly, but place that in the value for the value attribute and make sure to give each one a name attribute and value. In my example, I used $num for the value and $row['id'] for the name.

None of your <input> tags have name attributes. No name, no form submission.

GROUP is a reserved keyword.
So you need to backtick it as
`GROUP`
SELECT * FROM TABLE WHERE `GROUP` = 'Penguins' ORDER BY Rank, Name
UPDATE FROM LAST COMMENT
Input need a name which is not there and if you give same name for all of them they will not work. So give a name="something[]" and on submit get the data as array

Related

Trying to create an editable HTML table using PHP and mySQL but the table won't update

I'm trying to make a HTML table as a frontend to a mySQL database. The table displays fine and I can type in the edits I want to make to each row of the table but when I press the submit button the changes aren't actually made. Can anyone see where I'm going wrong?
<?php
include("db.php");
$sql = "SELECT * FROM `artist`";
$result = mysqli_query($conn, $sql);
if (isset($_POST['update'])){
$artID = $_POST['artID'];
$artName = $_POST['artName'];
$key = $_POST['hidden'];
$UpdateQuery = "UPDATE `artist` SET `artID` = '$artID', `artName` = '$artName' WHERE `artist`.`artID` = '$key'";
mysqli_query($conn,$UpdateQuery);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
};
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
The db.php file simply includes the connection info to the mySQL database and I'm 100% sure there's nothing wrong with it as it retrieves the table correctly it just doesn't update.
You are putting form tag inside tr which is not allowed td are only allowed
so you have to remove that tr from there.
You have to use jquery or you can replace the table with some other grid structure so that it can look the same and the form can be placed there as well
One more suggestion Don't mix the php and html together separate them for the clean code
If you do all these you code will be like this
Your form is being constructed with multiple elements with the same name. When you submit the form it is using the last elements as the values so regardless of the record you want updated the last record is being updated (or throwing an error because of string encapsulation). You should use parameterized queries as well.
So instead of:
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Use:
if ($result->num_rows > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {?>
<form class='artisttable' action ='getartiststable.php' method ='post'>
<tr>
<td><input type='text' name ='artID' value ='<?php echo $row['artID'];?>' /></td>
<td><input type='text' name ='artName' value ='<?php echo $row["artName"];?>' /></td>
<td><input type = 'hidden' name ='hidden' value='<?php echo $row['artID'];?>' /></td>
<td><input type='submit' name ='update'" . " </td>
</tr>
</form>
<?php } ?>
</table>
So you get a form for each data set. Here's a link on prepared statements with mysqli: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. You also should update your mark up. Tables for formatting aren't the best approach. Your inputs also weren't closed missing >.
Also this changed artisttable from an id to class because there will be multiples. Update CSS/JS accordingly.

Selectbox for update value

I have a selectbox in PHP, where the values and one value is selected. This selected value is data from table in MYSQL. Now, I need update/change this value to another which is in selectbox and send new value in table. When I send this new value from selectbox nothing happens. Can someone write how to update values ​​with selectbox.
<?php echo "<form action=zap_azuriraj.php?y=".$_GET['y']."&b=".$_GET['b']." method=POST>";
echo $_GET['y'];
$upit = "SELECT * FROM zapis WHERE zapis_id='$_GET[y]'";
$rezultat = izvrsiUpit($upit);
$rows = mysql_fetch_array($rezultat);
$id_bilj=$rows['biljka_id'];
$upit2 = "SELECT * FROM biljka";
$rezultat1 = izvrsiUpit($upit2);
if(isset($_GET['b'])) {
echo"<select name='biljkaa_id'>"; }
while ($row = mysql_fetch_array($rezultat1)){
if ($row['biljka_id']==$id_bilj) {
$optional="selected=\"selected\"";
}
else {
$optional="";
}
echo "<option $optional value=".$row['biljka_id'].">".$row['naziv']." </option>";
}
echo"</select>";
others are all working
echo"<h3> Datum:</h3>";
echo"<input type=date name=date value=" . $rows[3] . ">";
echo "<h3> Vrijeme:</h3>";
echo"<input type=time name=vrijeme value=" . $rows[4] . ">";
echo"<h3>Opis:</h3>";
echo"<textarea name=opis >" . $rows[5] . "</textarea>";
echo"<h3> Broj parcele:</h3>";
echo"<input type=text name=br_parcele value=" . $rows[6] . ">";
echo"<h3> Broj biljke:</h3>";
echo"<input type=text name=br_biljke value=" . $rows[7] . ">";
?>
<input type="submit" value="update" name="update">
</form>
<?php
I think the problem is in the commented line
if(isset($_POST['update'])&&(isset($_POST['biljkaa_id']))){
/* $biljka_id = $row['biljka_id'];*/
$datum=$_POST['date'];
$vrijeme = $_POST['vrijeme'];
$opis = $_POST['opis'];
$br_parcele = $_POST['br_parcele'];
$br_biljke=$_POST['br_biljke'];
$id=$_GET['y'];
$upit="UPDATE zapis SET
biljka_id='$biljka_id',
datum='$datum',
vrijeme='$vrijeme',
opis='$opis',
broj_parcele = '$br_parcele',
broj_biljke = '$br_biljke'
WHERE zapis_id='$id'";
$rezultat = izvrsiUpit($upit);
echo "Ažuriran je zapis";
}
?>
I found a solution , thanks anyway
$biljka_id = $_POST['biljkaa_id'];

PHP-HTML5-Form - My query-generated dropdown posts a blank value

When I load my page, the value of the variable, $v_UpdateONE, is "Select Version". When I select a version, the value goes blank.
I need to grab the selected value for use in a DB update statement.
Thank you for any assistance. -James
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
</FORM>
You have an error in
value='" . $row['$v_software1'] . "'
Since $v_software1 is in single quotes, it will be literal $v_software1.
Try removing the quotes -
value='" . $row[$v_software1] . "'
You need to post before you can read $_POST data.
Form File
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' id='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
?>
<button type="submit"> <!-- this will draw a submit button -->
</FORM>
then on your Update.php
<?php
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
Sometimes, the ID needs to be filled up (browser dependent)

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 ?

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

Categories