PHP form processing issue - php

PHP newbie here. I have struggling with this code for the past few days-
I have a dropdown menu. The options are coming from a table in my database-
<?php
include('Macintosh HD/Applications/MAMP/htdocs/Deals/processform3.php');
$host = 'localhost';
$username = '';
$password = '';
$database = 'database';
$conn = mysqli_connect($host, $username, $password, $database);
$query = mysqli_query($conn,"SELECT * FROM DealCat");
echo "<form action='processform.php' method='POST'>
<select name = 'dealcat'>/n";
while ($row = mysqli_fetch_assoc($query))
{
echo "<option value='{". $row['dealcat']."}'>" .$row['dealcat']."</option>";
}
echo "</select>\n";
?>
The navigation menu shows up fine on the webpage. However, I am not able to process user-input. I want the user to click on one of the options on my dropdown and PHP runs a script to get the results. I know this could be done with Javascript but I don't know that so trying to use only PHP.
Here is the form process script-
<?php
$host = 'localhost';
$username = '';
$password = '';
$database = 'database';
$conn = mysqli_connect($host, $username, $password, $database);
$dealcat=$_POST["dealcat"];
$query = "SELECT * FROM Deals WHERE dealcategory=\"{$_POST['$dealcat']"");
$result=mysqli_query($conn,$query) or die ("Couldn’t execute query.");
while($row = mysqli_fetch_assoc($result))
{
echo "<p>" . $row['description'] ."</p>";
echo "<br>";
echo "<a href =' {$row['weblink']}'> {$row['Header']}</a>";
echo "<br>";
echo "<br>";
echo "<button >Get Deal</button>";
echo "<hr>";
}
?>
Is there a way that PHP shows results based on user clicking on a dropdown option? Thanks a lot!

Try this
<select name="fieldname">
while ($row = mysqli_fetch_assoc($query))
{
echo "<option value=".$row['dealcat'].">".$row['dealcat']."</option>";
}
</select>

Related

Relate database and PHP through links

I'm doing a code where the main page has titles of some exercises: 'Exercises.php' (stored in a Mysql database) and depending on what title the user clicks (with links), I want the title and the question itself in another page: ‘question.php’. The questions will also be taken from the database. Im trying to use a GET parameter in the exercise link with the id of the exercise. Then in ‘question.php’, get the exercise with that id from the database.
This is some of the code that I've done so far but I'm stuck. Could you help me?
Thank you.
Exercises.php – In here I have all of the titles of the exercises displayed.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises";
$result = $conn->query($sql);
?>
<?php
while($row = $result->fetch_assoc())
{
?>
<tr>
<td><?php echo $row["exercise_id"]; ?></td>
<td><a name="search" href="http://localhost/PHP%20Pages/2.php" target="_blank"><?php echo $row["title"]; ?></a></td>
<td><?php echo $row["difficulty"]; ?></td>
</tr>
<?php
}
?>
question.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises"; /*Select from table name: exercises*/
$result = $conn->query($sql); /*Check connection*/
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["exercise_id"] . ". " . $row["title"] . $row["text"] . "<br>";
}
}
?>
If you want to get the excericise_id from excercise.php you can change your href like this
Add Topic</td>
And you can get that exercise id in your question.php page like this
$id=$_GET['id'];
I would like you to suggest you to keep your db connection in separate file and try to include and use. that is not the good way to write database connection in every page.

Data Not Displaying in Drop Down by mysqli

I have a small data entry form which works well, but someone asked me if I can have the 'name' field as a drop down box of users as opposed to having to type in a name and risk a spelling mistake - yep makes sense.
This is pretty new to me and following some information on here and other sites I have tried to accomplish the first part.. populating the drop down box.. nope. No errors, just nothing in the box.
To power this I have tblStaffNames (userID, txtName)
The code I am using looks like this;
<?php
include("connect-db.php");
$queryNames = "SELECT txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
?>
<select name="personname">
<?php
while ($rowNames = $resultNames->fetch_assoc()) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
?>
</select>
The $conn is all good as on another page I can display data in a table from the database, including tblStaffNames - so I can rule out any sort of connection issues.
When I run the page, the little drop down box appears, very simple like but it's there, just no values.
I will end up using the value like this as part of the data entry form;
<td><select name="personname" style="width:100px" ><?php echo $RowNames; ?></select></td>
But I can't actually get to the point of displaying data.
Can anyone help me out with what I am doing wrong here?
I tested your code with slight changes. It works for me. Please check your DB connection is OK as I have done in my code.
<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "staff_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$queryNames = "SELECT txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
//Generating the Dropdown
echo "<select name=\"personname\">";
while ($rowNames = $resultNames->fetch_assoc()) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
echo "</select>";
?>
I dont know what your error but for better understanding i change your code like this
$queryNames = "SELECT userID,txtName FROM tblStaffName";
$resultNames = $conn->query($queryNames);
?>
<select name="personname">
<?php
while ($rowNames = $resultNames->fetch_assoc()) {
?>
<option value="<?php echo $rowNames['userID']; ?>">
<?php echo $rowNames['txtName']; ?>
</option>
<?php
}
?>
</select>
Following code snippets works for me.
<?php
$servername = "localhost";
$username = "user";
$password = "password";
//DB Server Connection
$conn = mysql_connect($servername, $username, $password) or die("Connection establishment failed");
//DB Selection
$selected = mysql_select_db("staff_db", $conn) or die("Could not select DB");
//Query String
$queryNames = "SELECT txtName FROM tblStaffName";
//Query the DB
$resultNames = mysql_query($queryNames);
//Generating the Dropdown
echo "<select name=\"personname\">";
while ($rowNames = mysql_fetch_array($resultNames)) {
echo "<option value=\"{$rowNames['txtName']}\">";
echo $rowNames['txtName'];
echo "</option>";
}
echo "</select>";
?>

How to retrieve images from database in php

I have a database which has a table called 'propImages' and there are two columns.- 'pid' and 'location'.
And i have data in the database where multiple images can contained by single pid.
image contains database data
now i want to retrieve images from database according to given pid. there can be more than one image.
All i know it there should be an iteration to retrieve images.
I want to display images in HTML .
can you please show me the way to do it in php?
Thanks in advance guys
This may help you
<?php
include 'inc/database.php';
$conn = new mysqli($servername, $username, $password, $database);
$propid = $_GET['propid'];
$sql = "SELECT * FROM propImages WHERE propid='" . $propid . "';";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img src=" . $row['image'] . ">";
}
}
else {
echo "No results";
}
?>
in the inc/database.php :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "database";
?>
To see how it works try visiting : file.php?propid=22
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "databasename";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
//create sql
$sql = "SELECT * FROM `propImages` where pid='$YOUR_PID'";
$result = mysqli_query($con, $sql);
$row = mysqli_num_rows($result);
//retrive data print here
if($row > 0){
while($col = mysqli_fetch_assoc($result))
{
echo $col['location'];
}
} else {
echo 'no result found.';
}
?>
wish it helps

Filter SQL results with drop down menu in PHP

I ve been stuck on this problem for about 3 days now. I would appreciate any help i can with this problem how ever please can you explain where and how i have gone wrong as i would like to learn and understand!
Basically what i am trying to achieve is the following.
A drop down menu, that will display the results of the selected column in my database.
My database has 3 columns "project_name","stage" and "project_details"
If the user selects "stage" it must only display / echo the results form the "stage" column on the screen.
The code below is what i have so far! I know i am subject to an SQL injection but i am trying to get the filter to work first then i will sort that out.
At the moment i keep getting an error on line 42 and 56, i would appreciate any help or input for anyone!
<form action='filter2.php' method='post' name='value' >
<select name="value">
<option value="project_name">project_name</option>
<option value="stage">stage</option>
<option value="project_details">project_details</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
<?php
// Authentication Variables
$servername = "localhost";
$username = "basic";
$password = "redrobinX123";
$dbname = "basic_forms1";
// Make connection
$con = mysql_connect($servername, $username, $password, $dbname);
// If error connecting
if (!$con) {
die(mysql_error("could not connect"));
}
// If post value isset
if(isset($_POST['value'])) {
$column = $_POST['value']; // Set Column
$query = "SELECT". $column . "FROM photo"; // Create Query
$sql = mysql_query($query); // Run Query
// Loop through results
while ($row = mysql_fetch_array ($sql)){
echo "<br>". $row[$column] . "<br>";
}
// Close connection
mysql_close($con);
}
?>
Set it up like this....
// Authentication Variables
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Make connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// If error connecting
if (!$con) {
die(mysqli_error("could not connect"));
}
// If post value isset
if(isset($_POST['value'])) {
$column = $_POST['value']; // Set Column
$query = "SELECT $column FROM photo"; // Create Query
$sql = mysqli_query($con, $query); // Run Query
// Loop through results
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
echo "<br>". $row[$column] . "<br>";
}
// Close connection
mysqli_close($con);
}
I think you mixing mysql and mysqli functionalities. Try this
<form action='filter2.php' method='post' name='value' >
<select name="value">
<option value="project_name">project_name</option>
<option value="stage">stage</option>
<option value="project_details">project_details</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
<?php
// Authentication Variables
$servername = "localhost";
$username = "basic";
$password = "redrobinX123";
$dbname = "basic_forms1";
// Make connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// If error connecting
if (!$con) {
die(mysqli_error("could not connect"));
}
// If post value isset
if(isset($_POST['value'])) {
$column = $_POST['value']; // Set Column
$query = "SELECT ". $column . " FROM photo"; // Create Query
$sql = mysqli_query($con,$query); // Run Query
// Loop through results
while ($row = mysqli_fetch_array ($sql)){
echo "<br>". $row[$column] . "<br>";
}
// Close connection
mysqli_close($con);
}
?>

Displaying table and data in database doesnt show

i have this problem that my table doesnt show and all info in my database doesnt show also can you help me? the output only shows "Student No. First Name Middle Name Last Name Subject Code Units Final
..." . my info in database doesnt show and the table I created didnt show also . Any answers how to fix this code? thankss
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tsukishiro";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM student_grades WHERE Student_ID='c2011-02529' ";
$result = $conn->query($sql);
echo "<table>";
echo "<tr><th>Student No.</th><th>First Name</th><th>Middle Name</th><th>Last Name</th><th>Subject Code</th><th>Units</th><th>Final Grade</th><th>Remarks</th><th>Professor</th></tr>";
while($row = mysqli_fetch_array($result)) {
$Student_ID = $row['Student_ID'];
$First_Name = $row['First_Name'];
$Middle_Name = $row['Middle_Name'];
$Last_Name = $row['Last_Name'];
$Subject_Code = $row['Subject_Code'];
$Units = $row['Units'];
$Final_Grade = $row['Final_Grade'];
$Remarks = $row['Remarks'];
$Professor = $row['Professor'];
echo "<tr><td>".$Student_ID."</td><td>".$First_Name."</td><td>".$Middle_Name."</td><td>".$Last_Name."</td><td>".$Subject_Code."</td><td>".$Units."</td><td>".$Final_Grade."</td><td>".$Remarks."</td><td>".$Professor."</td></tr>";
}
echo "</table>";
$conn->close();
?>
this is<table border='1' cellpadding='2' cellspacing ='2'><tr><td>Student No.</td><td>First Name</td><td>Middle Name</td><td>Last Name</td><td>Subject Code</td><td>Units</td><td>Final</td></tr></table>... </div>
thats the only code in that page only

Categories