Sam Powers Sam Powers - 3 months ago 11x
MySQL Question

Form entry inserts a blank into database

I created an attendance form and when I submit the form it redirects me to submit.php. However, when I check in phpmyadmin I get a blank row except for the timestamp. I asked Stackoverflow about entering multi-row queries . Also I looked at Submitting Form Returns Blank info


Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
$link = mysqli_connect("localhost", "root", "passowrd", "database");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());

// Escape user inputs for security
$FName = mysqli_real_escape_string($link, $_POST['FName']);
$LName = mysqli_real_escape_string($link, $_POST['LName']);
$Mark = mysqli_real_escape_string($link, $_POST['Mark']);

// attempt insert query execution
$sql = "INSERT INTO database (FName,LName,Mark) VALUES ('$FName','$LName','$Mark')";

if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not take attendence " . mysqli_error($link);

// close connection




<?php if(isset($_POST[ 'search'])) { $valueToSearch=$ _POST[ 'valueToSearch']; // search in all table columns // using concat mysql function $query="SELECT * FROM `students` WHERE CONCAT(`FName`, `LName`) LIKE '%" .$valueToSearch. "%'"; $search_result=f
ilterTable($query); } else { $query="SELECT * FROM `students`" ; $search_result=f ilterTable($query); } // function to connect and execute the query function filterTable($query) { $connect=mysqli_connect( "host", "username",
"password", "database"); $filter_Result=m ysqli_query($connect, $query); return $filter_Result; } ?>
<form action="submit.php" method="post">
<table border="1" align="center">
<th>First Name</th>
<th>Last Name</th>

<?php while($row=mysqli_fetch_array($search_result)):?>
<!---<tdphp echo $row['FName'];></td>--->
<input type="text" name="FName" value="<?php echo $row['FName']?>" />
<!---<td><php echo $row['LName'];?></td>--->
<input type="text" name="LName" value="<?php echo $row['LName']?>" />
<select name="Mark" id="Mark">
<option value="Present">Present</option>
<option value="Tardy">Tardy</option>
<option value="Absent">Absent</option>

<?php endwhile;?>
<table align="center">
<input type="submit" value="Submit">




This is The Database Structure, I thought it would help.


There are several things wrong with this code.

  1. You have the submit button in a loop, which will end up with multiple submit buttons for the same form, that actually do the same thing. You should never have multiple submit buttons, unless you want to handle different scenarios on click on each of them. If you want to submit only one row on each button click, then you should also have separate forms for each. In that case, you should also move the opening/closing tags of the form inside the loop.

  2. You don't have the form method set, so it defaults to GET. You should set the form method to POST if you want to access the values using the global $_POST variable.

  3. The parameters you're trying to access through $_POST are not items of the form you're submitting. Based on what you have now, if you set the form method to POST you'll only have $_POST['name'] set. I am guessing you want to get the data for FName and LName as set in the $row variable and to do that, you need to add inputs with names FName and LName respectively and values set to the values in $row. It would be something like <input type="text" name="FName" value="<?php echo $row['FName']?>"/>. You can also have type="hidden" if you don't want them to be visible.

You should also consider using AJAX for the submission, so you don't need to reload the page on every update.