Display results after option selected - php

How can I create an SQL statement that will only show the options based upon one of the three options selected (C/E/W). At the moment, it just shows every result.
Also, how do i do this so the result will drop below once a C/E/W has been selected without the need for a submit button?
Any help would be appreciated.
<select id="combobox">
<option value="Central" id="C">C</option>
<option value="East" id="E">E</option>
<option value="West" id="W">W</option>
</select>
</p>
</div>
<div>
<?php
$query = "SELECT * FROM ROOMS";
$result = mysql_query($query);
if ($result == FALSE) die ("could not execute statement $query<br />");
echo "<table>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['roomCode'] . "</td><td>" . $row['Park'] . "</td><td>" . $row['Capacity'] . "</td><td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td><td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td><td>" . $row['wheelchairAccess'] . "</td><td>" . $row['lectureCapture'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>

Related

Form entry table search results in opening a new page and error

Shoots me to the /index.php with a error 404. I don't want it to navigate to another page, just display the results in a table.
I am not really sure what I am doing wrong, I just started php and have been using w3 to navigate the syntax. I managed to make a plugin that would display the a entire table but now I am trying to use a form to search a table and display the results.
...
if( isset($_GET['submit']) ){
$ssterm = $_POST["name"];
$query = "SELECT ID, Retailer, City, State, Contact, Address1, Address2, Country, Zip, Email, Website FROM Stores WHERE Retailer LIKE '%" . $rssterm . "%' OR City LIKE '%" . $ssterm ."%'";
$result = $conn->query($query);
if($result->num_rows > 0){
echo "<table>
<tr>
<th>ID</th>
<th>Retailer</th>
<th>City</th>
<th>State</th>
<th>Contact</th>
<th>Address1</th>
<th>Address2</th>
<th>Country</th>
<th>Zip</th>
<th>Email</th>
<th>Website</th>
</tr>";
while($row = $result->fetch_assoc()){ //Creates a loop to loop through results
echo "<tr><td>" . $row['ID'] . "</td><td>" . $row['Retailer'] . "</td><td>" . $row['City'] . "</td><td>" . $row['State'] . "</td><td>" .
$row['Contact'] . "</td><td>" . $row['Address1'] . "</td><td>" . $row['Address2'] . "</td><td>" . $row['Country'] . "</td><td>" . $row['Zip'] .
"</td><td>" . $row['Email'] . "</td><td>" . $row['Website'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
}
else {
echo "No Results Found...";
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="search">
</form>
<?php
$conn->close(); //Make sure to close out the database connection
} // end of a shortcode brace
?>
I thought this would for sure work, but it just shoots me to the /index.php with a error 404. I don't want it to navigate to another page, just display the results in a table. I am using this on a wordpress site and as a plugin using a shortcode to implement it into a page.

retrieving data from mysql DB to a drop down list

This is my code but I don't seem to get anything in the dropdown list. Is there something else I'm supposed to do besides this? Or is there something wrong with my code?
<div class="span10 offset1">
<div class="row">
<h3> Add catagory</h3>
</div>
<select class="selectpicker" data-style="btn-success" >';
<?php
include('database.php');
$query = "SELECT cat_name FROM catagory";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}
echo "</select>";
?>
</div>
You're referencing $row but assigning the result to $r. Just change the variable:
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $r['cat_name'] . " '>" . $r['cat_name'] . " </option>";
}
Your variables names look wrong
while($r = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}
You loop through the results using $r but use $row[] within the loop. It should probably read
while($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['cat_name'] . " '>" . $row['cat_name'] . " </option>";
}

PHP: include variable in $_POST

I have a table with some information from my database. I have buttons next to each row in the table. If the button is clicked it should send an email to the user. I can give each button a unique value but can not have this value in the if isset.... How should i get the value of the button name to be variable and how could i get the value at the end of my url.
echo "<table>";
echo "<tr><td>Naam</td><td>EmailAdress</td><td>Datum</td><td>Type</td><td>Producent</td><td>SerieNummer</td><td> imei</td><td>Manager</td><td>bevestigd</td></tr>";
while($row = mysqli_fetch_row($result)){
$sqlid = "SELECT Bevestigd FROM tbl_bevestiging WHERE IDbewijs = " . $row[0];
$resultid = mysqli_query($conn, $sqlid);
$row2 = mysqli_fetch_row($resultid);
echo "<tr><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><td>" . $row[4] . "</td><td>" . $row[5] . "</td><td>" . $row [6] . "</td><td>" . $row[7] . "</td><td>" . $row[8] . "</td><td>" . $row2 [0] . "</td><td><form method=\"post\"><input name=\"" . $row[0] . "\" type=\"submit\" value=\"" . $row[0] . "\"</form></td></tr>";
}
echo "</table>";
if (isset($_POST[$row[0]])){
$admin_email = "adminmail#mail.com," . $row[2];
mail($admin_email, "Ontvangstbewijs geleverde hardware herverstuurd", "url?id=" . $row [0] , "From:" . "example#email.com");
mysqli_close($conn);
}
?>
Theres a few things I think are worth pointing out firstly.
Table header tags should be in a THEAD tag. See example here.
Try and avoid SQLs within a loop. If possible create a SQL that does everything for you to reduce the number of queries sent to MySQL.
Ok back to your question. Personally, what I would do is have a FORM tag in a TD tag. This way each button can have it's own unique id set in a hidden INPUT tag.
For example:
<tr>
<td>
<form action="[[YOUR-PHP-FILE]]" method="post">
<input type="hidden" name="id" value="[[ID]]" />
<input type="submit" value="Submit" />
</form>
</td>
</tr>
That way you don't need to focus on the submit button but rather the id.

Produce a PHP result from a select tag

I am struggling to produce a SQL result. I am trying to display my results in a table from one of the select options.
Any help?
HTML:
<form action="" method="post" name="parkname">
<select id="combobox" name="parkname" class="park-choice">
<option hidden value="C">C</option>
<option class="hidden" value="E">E</option>
<option class="hidden" value="W">W</option>
</select>
<span style="display:inline-block; width: 200px;"></span>
Capacity:
<select id="foo" style="display:inline-block; width: 80px;" id="combobox">
<input type="submit" value="submit">
</select>
</form>
PHP code:
<?php
$selectOption = $_POST['parkname'];
$query = "SELECT * FROM `ROOMS` WHERE `Park` = '$selectOption%';";
$result = mysql_query($query);
echo $result;
if ($result == FALSE) die ("could not execute statement $query<br />");
echo "<table>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['roomCode'] . "</td><td>" . $row['Park'] . "</td><td>" . $row['Capacity'] . "</td><td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td><td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td><td>" . $row['wheelchairAccess'] . "</td><td>" . $row['lectureCapture'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
First, you need to clear your query from % sign.
$query = "SELECT * FROM `ROOMS` WHERE `Park` = '$selectOption';";
Or if you want to find all matches, you can use LIKE operator:
$query = "SELECT * FROM `ROOMS` WHERE `Park` LIKE '%$selectOption%';";
Second, for displaying html combobox with Park names (in your case), use code like:
<select name="park">
<option value="Park1">Display name</option>
<option value="Park2">Display name</option>
<option value="Park3">Display name</option>
</select>
Park1,Park2,Park3 - Values that would be sent to your php code inside $_REQUEST['park'] variable.
Display name - Just visually display name for options in select

Edited all entries in database [php]

I have a problem in editing data in my database. I only want to edit one entry but after I clicked edit, it shows that all the data in my database were edited.
eventlist.php
<?php
$con=mysqli_connect("localhost","root","root","chess");
$result = mysqli_query($con,"select * from events");
//echo "<a href='dashboard.php'>Home</a><br>";
echo "<table border=1 id='hor-minimalist-a' width='100%'>";
echo "<tr align='center'><td><b>Date</b></td><td><b>Event</b></td><td><b>Special Note</b></td><td colspan='2'>Options</td></tr>";
$a=0;
while($row = mysqli_fetch_array($result))
{
if($a%2==0){
echo "<tr bgcolor='#b2d5ff' width='100'>"."<td>" . $row['date'] . "</td> <td>" . $row['event'] . "</td> <td>" . $row['note'] . "</td>"
. "</td><td><a href='editevent.php?id=" . $row['id'] . "'>Edit</a></td><td><a href='deleteevent.php?id=" . $row['id'] . "'>Delete</a></td></tr>";
}
else{
echo "<tr>"."<td>" . $row['date'] . "</td> <td>" . $row['event'] . "</td> <td>" . $row['note'] . "</td>"
. "</td><td><a href='editevent.php?id=" . $row['id'] . "'>Edit</a></td><td><a href='deleteevent.php?id=" . $row['id'] . "'>Delete</a></td></tr>";
}
$a++;
}
echo '</table>';
echo "<center><a href='addevent.php'><button type='submit' class='button'>Add New</button></a></center>";
?>
editevent.php
<?php
while($row = mysqli_fetch_array($result))
{
echo "ID: <input type='text' name='id' value='$row[id]'><br/>";
echo "Date: <input type='text' name='date' value='$row[date]'><br/>";
echo "Event: <textarea type='text' name='event'>".$row['event']."</textarea><br/>";
echo "Note: <input type='text' name='note' value='$row[note]'><br/>";
}
?>
updateevent.php
<?php
$id = $_POST['id'];
$date = $_POST['date'];
$event = $_POST['event'];
$note = $_POST['note'];
$con = mysqli_connect("localhost","root","root","chess");
mysqli_query($con,"update events set date='$date', event='$event', note='$note' where id = id");
header('location: eventlist.php');
?>
"update events set date='$date', event='$event', note='$note' where id = id"
where id = id is true for all rows, so all rows get updated. You probably ment to write where id = $id.
Also note that your query is open to SQL injection. Use prepared statements instead.

Categories