PHP form submit with SQL query results in blank table? - php

I've currently got a page with a form, and I'd like to populate a table based off a SQL query using a variable. I know the value from the select dropdown is being extracted correctly, and I believe I've got bindValue implemented correctly. However, upon submitting the form, nothing shows up in the table. I know the SQL statement is correct as well, as I've run it directly in MySQL and gotten the results I'm looking for.
Here's my code currently:
echo '<form align="center" method="post">';
echo '<select name="flightSelect">';
$flightstmt = $conn->query('select * from flight');
$flightstmt->execute();
while($row = $flightstmt->fetch(PDO::FETCH_ASSOC))
{
echo "<option value='".$row['flightnum']."'>";
echo $row['origination']."->".$row['destination'];
echo '</option>';
}
echo '</select>';
echo '<table>';
if (isset($_POST['flightSelect']))
{
$flightSelect = $_POST['flightSelect'];
$flightPassengers = $conn->prepare('select * from passenger where passnum in (select passnum from manifest where flightnum=:flightSelect)');
$flightPassengers->bindValue(':flightSelect', $flightSelect, PDO::PARAM_INT);
$flightPassengers->execute();
while($row = $flightPassengers->fetch(PDO::FETCH_ASSOC))
{
echo '<tr>';
echo '<td>';
echo $row['lastname']." ".$row['firstname'];
echo '</td>';
echo '</tr>';
}
}
echo '</table>';
echo '<input type="submit" name="submit" value="Find passengers on this flight">';
echo '</form>';
Thanks in advance for any feedback!

<form align="center" method="post"> Form lacks action attribute.

Open the Network Panel in Chrome DevTools and check if the HTTP status code is 500
if yes, then check the error log, you'll find the stack trace atleast.

You need to verify that the passenger table should have flight data you select while submitting.
Debug the query by removing flight condition and see are you get the result for flight.
Rest of the code is correct

Related

Get the values of the ticked checkboxes in php

I am currently building a web application. In my application, a load some data from mysql and I display them as a table in my website. Additionally I add another column that consists of different checkboxes. My source code of displaying the table is called by a function that is located in another page. The source code odf the function is the following :
function user_clients_table() {
$con = mysql_connect("localhost","root",'');
if(!$con){
die("Cannot Connect" . mysql_error());
}
mysql_select_db("client_app",$con);
$get_user_clients = "SELECT `ID`,`Name`,`SurName` FROM `clients` ";
$clients = mysql_query($get_user_clients,$con);
echo "<table border=2>
<tr>
<th>ID</th>
<th>Name</th>
<th>SurName</th>
<th>Receive Message</th>
</tr>";
while($record = mysql_fetch_array($clients)){
echo "<form action=pushnotification.php method=post>";
echo "<tr>";
echo "<td>".$record['ID']." </td>";
echo "<td>".$record['Name']." </td>";
echo "<td>".$record['SurName']." </td>";
echo "<td>"."<input type=checkbox name=checkbox[] value=".$record['ID']." />". "</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close();
}
The function works fine, after i call the function my webpage looks like this:
I want next to display the client number whose check box has been checked after i click the button send. For example if i checked only the first check box and submit it, i want to echo the client id that matches thsi checkbox, in this case i will echo '2'. My approach to this is the following:
if(isset($_POST['send'])){
if(!empty($_POST['checkbox'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['checkbox']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['checkbox'] as $selected) {
echo "<p>".$selected ."</p>";
}
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
}
else{
echo "<b>Please Select Atleast One Option.</b>";
}
}
It works but only for the first checkbox, if I select the other checkboxes without the first one I doesn't display anything.
Can someone please help me?
Thanks in Regards
Each checkbox is in it's own form, so when you are submitting you are submitting the first form (the first checkbox), and that is why you are getting the current action. Put the form tags outside the loop

How can I sort Items in a database created chart in PHP?

I have created a database which contains people from a run with the amount of rounds which they ran. After that I created this script which creates a html chart.
The script:
<?php
$db = new PDO("mysql:host=HIDDEN;dbname=HIDDEN", 'HIDDEN', 'HIDDEN') or die ("Verbindung nicht möglich");
$query_temp = $db->prepare("SELECT * FROM runners");
$query_temp->bindParam(':id', $id);
$id = 1;
$query_temp->execute();
echo '<table class="table table-hover">';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Name</th>';
echo '<th>Runden</th>';
echo '</tr>';
while($id_table = $query_temp->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo '<td>'.$id_table['runner_id'].'</td>';
echo '<td>'.$id_table['runner_name'].'</td>';
echo '<td>'.$id_table['runner_rounds'].'</td>';
echo '</tr>';
}
echo '</table>';
?>
The Database:
http://img.prntscr.com/img?url=http://i.imgur.com/WklMcTG.png
Screenshot how it looks:
http://img.prntscr.com/img?url=http://i.imgur.com/v2zbOSu.png
Now I want to give the users the ability to sort the people for their amount of rounds which they ran. Like the most person is at the first place. How can I do this?
To make this you need a js plugin and turn the HTML dynamic. There is a lot of plugins to make it and will list some of them to you:
datatables
tablesorter
Each plugin work differently, but in the end they have almost the same behaviour, then you need to check the documentation of them and try to run...
You should use DataTables with jQuery

Creating a HTML table with MYSQL fetching data doesn't work

I'm attempting to create a dynamic table with MYSQL's function: while(mysql_fetch_assoc).. However, when it fetches more than one result, it doesn't create the table anymore (or fill in the tags. Excuse me for explaining this incorrectly)
This is my code. Ignore the Dutch words :)
$sql2 = mysql_query("SELECT * FROM kostendb WHERE ProjectID = '$_GET[id]'") or die (mysql_error());
echo '
<table border="1" style="width:60%">
<tr>
<th>Kostencode</th>
<th>Datum</th>
<th>Bedrag</th>
</tr>';
while($res = mysql_fetch_assoc($sql2))
{
echo '<tr>';
echo '<td>' .$res['KostenID']. '</td>';
echo '<td>' .$res['Datum']. '</td>';
echo '<td>' .$res['Bedrag']. '</td>';
echo '</tr>';
}
echo '</table>';
When it finds more than one result, the while-loop doesn't do anything. When it finds just one result, it works fine.
What is causing this, and how can I fix this?
I've checked out an example script, but it's exactly using my method.
Thanks
You might have mixed mysql with mysqli.
Choose one, don't mix and this might fix your problem.
make a variable like $project = $_GET['ID'];
then put in sql statement as ....WHERE Project_ID = $project");
Try this

Dynamic dropdown menu don't display the rest of the html codes, Whats wrong?

Hello again codes masters,
I'm stuck at this piece of codes for dropdown menu. Here is the codes
echo '<h3>Select Supplier</h3>';
$deliver_sql = mysql_query("SELECT supplier_name FROM delivery") or die(mysql_error());
echo '<div align="left">';
echo '<form class="forms" action="returns.php" method="post" name="companyform">';
echo "<select class=\"input\" name=\"companyNames\" onChange=\"this.form.submit()\">";
while($row = mysql_fetch_array($deliver_sql) or die(mysql_error()))
{
echo '<option value="'.$row['supplier_name'].'">'.$row['supplier_name'].'</option>';
}
break;
echo '</select>';
echo '</form>';
echo '</div>';
My question is, Is there something wrong with this code? Because when I open this particular page, the footer aint displaying.
Its like there is something that is breaking the whole html codes that causes the footer to doesnt display, even if I used $_POST['companyform'] cannot also detected.
Can someone please find what causes this error.
The problem is in this line
while($row = mysql_fetch_array($deliver_sql) or die(mysql_error()))
when there are no more rows in the $deliver_sql, mysql_fetch_array will return false and die(mysql_error()) will be executed. mysql_fetch_array returning false is not an error so you should not call die in this case
Just use
while($row = mysql_fetch_array($deliver_sql))
try echo '<div style="float:left">'; instead of echo '<div align="left">';
or remove break;
and remove or die(mysql_error()) from while($row = mysql_fetch_array($deliver_sql))

Grabbing the text within text area with php html

Hey so I am trying to grab the user input text within a textarea but it is not working out too well. What is happening is that we are grabbing a text (movie review) from our server and we want the user to be able to update it and then send it back to the server. Anyone know what we are doing wrong??
We arent getting any error, it just that we are unable to grab the textarea field data. We are pretty new to php and html so I am assume it is some small typeo we are overlooking.
UPDATE: Full fills here.
http://dl.dropbox.com/u/21443163/Reviews.php
http://dl.dropbox.com/u/21443163/updateReview.php
while($RecordSetMovieRow = odbc_fetch_array($RecordSetMovie))
{
echo "<tr>";
$review = $RecordSetMovieRow['Review'];
echo "<td align = 'center'>" . $RecordSetMovieRow['FirstName']. $RecordSetMovieRow['LastName'] . "</td>";
echo "<td align = 'center'><textarea name = 'textarea' rows = '5' cols= '40'>" . $review . "</textarea></td>";
$textarea = $_GET['textarea'];
$u = $Re[0];
echo "<td><form action = 'updateReview.php?id=".$RecordSetMovieRow['ReviewID']."&review=$textarea' method = 'POST'><input type='submit' value='Update'></form></td>";
echo "</tr>";
}
echo "</table>";
odbc_close($Conn);
If you want to send large blocks of data to the database then enclose everything in a form with the method=POST name/attribute
<form action="updatingScript.php" name="myForm" method="POST" >
<textarea name="textArea" rows="5" cols="40"><?=$review ?></textarea>
</form>
Then in your updatingScript.php do this
if(isset($_POST['myForm'])) {
$textInfo = mysql_real_escape_string($_POST['textArea']);
//move this info in your database
mysql_connect("localhost", "root", "");
mysql_select_db("myDb")
$query="UPDATE myTable SET userTextInfo='$textInfo' WHERE userId='$userId' ";
$result=mysql_query($query);
}
Also set error_reporting(E_ALL); at the beginning of your PHP script as this will display what went wrong (in response to your "we aren't getting any errors")
You mention method='POST' in your form definition (which is right), but attempt to check $_GET['textarea'] (which is wrong either way). I'd suggest fixing the latter: sending large blocks of text in URL itself is usually not great.
Don't forget to get rid of the &review=$textarea as well; no need to send the content twice, in two different variables. )
Your code, with just a few minor tweaks to make it get the proper data from the form. The credit goes to raina77ow, though - his answer is absolutely correct. I just saw that you requested some code, so here it is.
Also, you need to have the form tags such that the textarea is WITHIN them, otherwise it is not part of the form, and it's data does not get posted (that edit is included below).
echo '<form action = 'updateReview.php?id=".$RecordSetMovieRow['ReviewID']."' method = 'POST'>'; // Moved this outside of the while - BUT it needs to be BEFORE the <table> tag also!
echo '<table>'; // If this is not where you want your opening table tag, that's fine - but move the opening FORM tag to BEFORE the opening Table tag
while($RecordSetMovieRow = odbc_fetch_array($RecordSetMovie))
{
echo "<tr>";
$review = $RecordSetMovieRow['Review'];
echo "<td align = 'center'>" . $RecordSetMovieRow['FirstName']. $RecordSetMovieRow['LastName'] . "</td>";
echo "<td align = 'center'><textarea name = 'textarea' rows = '5' cols= '40'>" . $review . "</textarea></td>";
$textarea = $_POST['textarea']; // Changed from $_GET["textarea"] because you are using method='post' in form
$u = $Re[0];
echo "<td><input type='submit' value='Update'></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>'; // Moved this to the end of the form, so data from form will get passed
odbc_close($Conn);

Categories