How to retrieve data based on dropdown list in php? - php

Ive been having problem with my codes.. Im a still noob with php so i really appreciate if anyone could help me :'( .. Im trying to display data from database based on the selected dropdown list value. There are three data currently in my database table .. However, only one show up on my output .. i dont know whats wrong with my codes. can anyone help me ..
this is my code departmentlist.php
Choose Department :
<form action="" method="POST">
<select name="department">
<option value="" selected="selected">-- Choose department --</option>
<option value="Information System Department">Information System Department</option>
<option value="Finance">Finance</option>
<option value="HR and Administration">HR and Administration</option>
<option value="Security">Security</option>
</select>
<input type="submit" value="Submit" />
</form>
</br>
<table border="1" id="table">
<tr><th bgcolor="#00CCCC">Department</th>
<th bgcolor="#00CCCC">Locaion</th>
<th bgcolor="#00CCCC">Serial Number</th>
<th bgcolor="#00CCCC">Description of asset</th>
</tr>
<?php
mysql_connect('localhost','root',"") or die('Error1 '.mysql_error());
mysql_select_db("ams") or die('error2'.mysql_error());
if($_SERVER['REQUEST_METHOD'] =='POST')
{ $dep=$_POST['department'];
$query="SELECT * FROM asset WHERE department= '" . $dep . "'";
$run=mysql_query($query);
$numrow = mysql_num_rows($run);
$row=mysql_fetch_array($run, MYSQLI_ASSOC);
echo "<tr><td bgcolor='#00FFCC'>".$row['department']."</td><td bgcolor='#00FFCC'>".$row['location']."</td><td bgcolor='#00FFCC'>".$row['serialno']."</td><td bgcolor='#00FFCC'>".$row['desc']."</td></tr>";
}
}
?>
</table>

Manual: http://www.php.net/mysql_fetch_array
Put mysql_fetch_array in a while loop so:
while ($row = mysql_fetch_array($run, MYSQL_ASSOC)) {
echo "<tr><td bgcolor='#00FFCC'>".$row['department']."</td><td bgcolor='#00FFCC'>".$row['location']."</td><td bgcolor='#00FFCC'>".$row['serialno']."</td><td bgcolor='#00FFCC'>".$row['desc']."</td></tr>";
}
I also suspect that you should MYSQL_ASSOC not MYSQLI_ASSOC since you're using mysql not mysqli.
Take note that mysql will be deprecated so use mysqli.

you have to loop through the results.
while($row = mysql_fetch_array($run, MYSQLI_ASSOC)){
echo "<tr><td bgcolor='#00FFCC'>".$row['department']
."</td><td bgcolor='#00FFCC'>".$row['location']
."</td><td bgcolor='#00FFCC'>".$row['serialno']
."</td><td bgcolor='#00FFCC'>".$row['desc']
."</td></tr>";
}

Try using a while loop.
while($row=mysql_fetch_array($run)){
echo "<tr>
<td bgcolor='#00FFCC'>".$row['department']."</td>
<td bgcolor='#00FFCC'>".$row['location']."</td>
<td bgcolor='#00FFCC'>".$row['serialno']."</td>
<td bgcolor='#00FFCC'>".$row['desc']."</td>
</tr>";
}

Related

How do I edit a specific row in SQL using PHP

I am fairly new to PHP and SQL.
I want to be able to assign a task to a certain user and the update that row in the database with the user's name assigned to that task.
Here is my code:
<table border="0" width="1100px" style= "font-size: 12px" >
<thead>
<tr valign="top" align="left">
<th height="20"></th>
<th height="20">Customer</th>
<th>Vehicle</th>
<th>Appt Time</th>
<th>Notes</th>
<th>Assign</th>
<th>Action</th>
<tr><td valign="top" colspan="6"><hr><br></td></tr>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr valign='center'>
<td width='50'><b>{$row['id']}</td>
<td width='220' height='70'><b>{$row['firstname']}
</td>
<td width='240'><b>{$row['car']}</b> <br>{$row['reg']}</td>
<td width='170'>Monday<br>24 September<br>17:00</td>
<td width='240'>{$row['notes']}<br><b>Status:</td>
<td width='240'>
<form action='bookings.php' method='post'>
<select style='width:90px' class='reg' name='assign'required>
<option value=''></option>
<option value='User1'>User1</option>
<option value='User2'>User2</option>
<option value='User3'>User3</option>
<option value='User4'>User4</option>
<option value='User5'>User5</option>
</select><input type='submit' value='>' class='assignButton'/></form>
</td><td>
<button class='myButton'>Edit</button>
</td>
<tr><td colspan='6'><hr class='hrTitle'></td></tr>
</tr>\n";
}
?>
</tbody>
</table>
As you can see, I have a number of users that can be selected, I want to be able to assign that task to a user from the select list.
Any help is much appreciated.
Try the following steps.
Give a name to your submit button.
<input type='submit' value='>' name='submit' class='assignButton'>
Add a hidden input field to your form. Give it the value of the id of the current row. Give it a name. I chose id.
This is very important so you can know which customer to edit. Please double check the value that you will set. From your code, I see it is $row['id'].
<input type="hidden" name="id" value='$row["id"]''>
In your PHP file bookings.php (assuming you have a connection to your database), process the submitted form.
if(isset($_POST["submit"])){ //checks if the form was submitted
$id = $_POST["id"]; //id of the customer
$assign = $_POST["assign"]; //your selected value
$query = "UPDATE table SET columnToModify = '$assign' WHERE id = '$id'";
$result = $connection->query($query); //run the query
}
Hope it helps.

How to query multiple fields from database table and input into select box?

So, I'm having some issues with calling two fields from a table and inputting them as one entry in a select box that will keep adding each entry in a while statement. In simpler terms I have one field called firstname and the next lastname in a table called professors. I need these fields combined and put together and each one inputted into a select box. Below is the code I have and I receive errors in dreamweaver at the bottom part when I start the while statement, so I know what I have isn't working but I'm having trouble figuring out why. I can't upload a picture with syntax highlighting because I just made my account and don't have enough reputation. The parts of code that I'm only concerned with in this question are the beginning parts up to where I start the table, and down at the bottom where I declare the select box for professor and the loop to grab the first and last names that are in the table. I've been reading many support solutions for similar problems but was unsuccessful. If anyone has any tips or advice I'd be greatful.
$fprofessor = $_GET['p_firstname'];
$lprofessor = $_GET['p_lastname'];
$selected_professor=$fprofessor." ".$lprofessor;
$qp = "SELECT p_id, p_firstname, p_lastname FROM professors WHERE p_firstname='$fprofessor' AND p_lastname='$lprofessor'";
$rp = #mysqli_query($dbc, $qp);
// Table header:
echo '<table align="center" cellspacing="0" cellpadding="5" width="auto">
<tr bgcolor="' . $bg . '">
<td align="left">Section ID</td>
<td align="left">Course Major</td>
<td align="left">Course ID</td>
<td align="left">Course Name</td>
</tr>
<tr bgcolor="' . $bg . '">
<form action="add_courses.php" method="post">
<td align="left"><input type="text" name="section_id" size="7" maxlength="7"/></td>
<td align="left"><select name="course_major">
<option value="TBA">-Select Major-</option>
<option value="CIT">CIT</option>
<option value="CSE">CSE</option>
<option value="CIT/CSE">CIT/CSE</option></td>
<td align="left"><input type="text" name="course_id" size="3" maxlength="3"/></td>
<td align="left"><input type="text" name="course_name" size="15" maxlength="25"/></td>
</tr>
<tr bgcolor="' . $bg . '">
<td align="left">Professor</td>
<td align="left">Available</td>
<td align="left">Semester</td>
<td align="left">Year</td>
</tr>
<tr bgcolor="' . $bg . '">
<td align="left"><select name="course_professor">
<option value="TBA">-Select Professor-</option>'
while ($row = mysqli_fetch_array ($rp, MYSQLI_NUM)) {
echo'<option value="'.$row[. $selected_professor .].'">'.$row[. $selected_professor .].'</option>'
};
echo'</td>
So, I've revised the code to something that seems more fitting based on the suggestions I got. Below is what I have now but I'm still receiving an error on the $qp variable and the while statement in this code
<td align="left"><select name="course_professor">'
$qp = "SELECT p_id, p_firstname, p_lastname, CONCAT(p_firstname, p_lastname) AS p_fullname FROM professors";
$rp = #mysqli_query($dbc, $qp);
echo '<option value="TBA">-Select Professor-</option>'
while ($row = mysqli_fetch_array ($rp)) {
echo '<option value="'.$row['p_fullname'].'">'.$row['p_fullname'].'</option>'
};
echo'</td>
I think you might want to do something like:
$qp = "SELECT p_id, p_firstname, p_lastname, CONCAT(p_firstname, p_lastname) AS p_fullname FROM professors WHERE p_firstname='$fprofessor' AND p_lastname='$lprofessor'";
Then reference the concatenated field in the query at the bottom.
If you need a mysql concat reference, you can read: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
God I feel stupid. I fixed the issues I was having, I was forgetting a few ";" tags to end echo's -_- Below is the code that works, but I'd like to thank James for helping me revise my code for my query.
<td align="left"><select name="course_professor">';
$qp = "SELECT p_id, p_firstname, p_lastname, CONCAT(p_firstname, p_lastname) AS p_fullname FROM professors";
$rp = #mysqli_query($dbc, $qp);
echo '<option value="TBA">-Select Professor-</option>';
while ($row = mysqli_fetch_array ($rp)) {
echo '<option value="'.$row['p_fullname'].'">'.$row['p_fullname'].'</option>';
}
echo'</td>

Update several mysql entries at once from PHP table

I have a table with some data from a mysql database. I need to update that information from within the table itself so when I press the button submit (table is inside a form) I need that all that data gets updated on the database. I can get the information from the database with no problem but I'm unable to update it! Here is the code:
<table border=1 cellpadding=4 cellspacing=0 width=960px style='font-size:10px'>
<form id='form1' name='form1' method='post' action='itself.php'>
<thead>
<tr>
<th colspan=3> People on the list </th>
<th><input type='submit' name='filtrar' id='filtrar' value='Filtrar'/></th>
<th><input type='submit' name='modificar' id='modificar' value='Modificar'/></th>
</tr>
<tr>
<th><label for='id'>ID</label></th>
<th><label for='id'>Friends</label></th>
<th><label for='id'>On the list?</label></th>
</tr>
<?
while($row = mysqli_fetch_array($result_listas, MYSQLI_BOTH))
$id= $row['id'];
{
?>
<tr>
<td><? echo "$row[id]" ?>
</td>
<td><select name='friends[<? echo "$id" ?>]' size='1' id='friends[<? echo "$id" ?>]'>
<option selected='selected'><? echo "$row[friends]" ?></option>
<option>less than 10</option>
<option>more than 10</option>
</select>
</td>
<td><select name='onlist[<? echo "$id" ?>]' size='1' id='onlist[<? echo "$id" ?>]'>
<option selected='selected'><? echo "$row[onlist]" ?></option>
<option>SI</option>
<option>NO</option>
</select>
</td>
<?
$ssql_min="select min(id) as id from listas_old";
$result_min= mysqli_query($link, $ssql_min);
$resultado_min = mysqli_fetch_array($result_min, MYSQLI_BOTH);
$ssql_max="select max(id) as id from listas_old";
$result_max= mysqli_query($link, $ssql_max);
$resultado_max = mysqli_fetch_array($result_max, MYSQLI_BOTH);
if(isset($_POST[modificar]))
{
for($i=$resultado_min['id']; $i<$resultado_max['id']; $i++)
{
$sql1="UPDATE listas_old SET friends='$friends[$i]', onlist='$onlist[$i]' WHERE id='$i'";
$result1=mysqli_query($link, $sql1);
}
}
?>
The OP wrote in a comment:
Got the solution for myself! This is what I did...
Change the for sentence like this:
for($i=$resultado_min['id'];
$i<=$resultado_max['id'];
$i++) {
$friendsfor= $_POST['friends('.$i.')'];
$onlistfor= $_POST['onlist('.$i.')'];
$sql1="UPDATE listas_old SET friends='$friendsfor', anotado= '$onlistfor' WHERE id='$i'";
}
It seems like you have your quotes all wrong in the update. You are trying to concatenate a string (your update sql) with php variables. But you need to make sure that php recognizes them as a variable instead of just as a string.
Should be like this
$sql1= "UPDATE listas_old SET friends='" . $friends[$i] . "', onlist='" . $onlist[$i] . "' WHERE id='" . $i . "'";
You only need to add in the single quotes if you are adding a varchar or date or something. If it is an int, you don't need the single quotes. Also I'm not sure what onlist would do, or if that is valid.

filter data to show using dropdown option in php

I try to show some data by dropdown option from mySQL
when the user choose option United states and click submit, the page will go to the next page and show the data only for United states
here is my code for test.html
<body>
<table border="0">
<tr>
<th>test
</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select>
<option value="US">United States</option>
<option value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<button type="submit">submit</button>
</td>
</table>
</body>
here is my second page showDB.php
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");
//connect to database
mysql_select_db("asdasd");
//query the database
//$query = mysql_query("SELECT * FROM auip_wipo_sample");
if($_POST['value'] == 'US') {
// query to get all US records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='US'");
}
elseif($_POST['value'] == 'AUD') {
// query to get all AUD records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='AUD'");
} else {
// query to get all records
$query = mysql_query("SELECT * FROM auip_wipo_sample");
}
//fetch the result
Print "<table border cellpadding=3>";
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$row['invention_title'] . "</td> ";
Print "<th>Pet:</th> <td>".$row['invention-title'] . " </td></tr>";
}
Print "</table>";
?>
I try the above code, but I got 2 error which are
Undefined index: value in C:\xampp\htdocs\folder\showDB.php on line 10
Undefined index: value in C:\xampp\htdocs\folder\showDB.php on line 14
line 10 is
if($_POST['value'] == 'US') {
and line 14 is
elseif($_POST['value'] == 'AUD') {
anyone can give solution
thanks
You need to add a name parameter to test.html - either set it to name="value" to make showDB.php to work without any alterations, or set lines 10 and 14 to match the name parameter you set. Example below:
edit:
#adeneo is right, you also need to add a form and a valid submit button
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="value">
<option name="country" value="US">United States</option>
<option name="country" value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<input type="submit" name="btn_submit" />
</td>
</table>
</form>
</body>
and the php:
if($_POST['country'] == 'US') {
// query to get all US records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='US'");
}
elseif($_POST['country'] == 'AUD') {
You have no element with the name value in your form, so you don't have a $_POST['value'] either! Actually, you don't even have a form to submit at all, you just placed a link to another page inside the submit button ?
<body>
<form action="showDB.php">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="value">
<option value="US">United States</option>
<option value="AUD">Australia</option>
</select>
</td>
</tr>
<td>
<button type="submit">submit</button>
</td>
</table>
</form>
</body>

Get Mysql table row data based on form submit values

I need to fetch the rows based on form submission values.
Here is my form
<form name="choose" method "post" t" action="search.php">
<table>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Prof</font1>
</td>
<td>
<select name proffession on>
<option value=""></option>
<option value="doctor"><font4>Doctor</font></option>
<option value="designer">Designer</option>
</select>
</td>
</tr>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Source</font1>
</td>
<td>
<select name source>
<option value=""></option>
<option value="x"><font4>X</font></option>
<option value="y">Y</option>
<option value="z">Z</option>
</select>
</td>
</tr>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Location</font1>
</td>
<td>
<select name location on>
<option value=""></option>
<option value="bangalore">Bangalore</option>
<option value="delhi">Delhi</option>
</select>
</td>
</tr>
<tr>
<td>
<input name=look type=submit value=submit>
</td>
</tr>
</form>
If there is any empty field submission i need to fetch the rows excluding that column.
Here is my search.php
<?php
mysql_connect("localhost","root","");//database connection
mysql_select_db("alldata");
$qry = "SELECT * FROM data WHERE location LIKE '" . mysql_escape_string($_POST['location']) . "' And proffession LIKE '" . mysql_escape_string($_POST['proffession']) . "' And source LIKE '" . mysql_escape_string($_POST['source']) . "'";
$res = mysql_query($qry);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
function create_table($dataArr) {
echo "<tr>
"; for($j = 0; $j < count($dataarr); $j++) { echo "<td>".$dataArr[$j]."
</td>
"; } echo "
</tr>
"; } $all = mysql_fetch_all($res); echo "
<table class='data_table'>
"; for($i = 0; $i < count($all); $i++) { create_table($all[$i]); } echo "</table>";
?>
But this script is not able to get me a solution.
Please help
1.Correct your function mysql_fetch_all($res). There is no query
inside the function.
2. Deprecated: mysql_escape_string(): This
function is deprecated; use mysql_real_escape_string()
3. Correct: <select name source> to: <select name="source"> and <select name location on> to <select name="location"> and <input name=look type=submit value=submit> to <input name="look" type="submit" value="submit"> and delete t" from choose form AND <form name="choose" method="post" action="search.php">
Firstly, you should not be using mysql_* functions, see the big red box here. Consider using PDO or MySQLi instead.
Second, you appear to be missing some = such as method="post" You may want to check you're actually receiving your POST values correctly with var_dump($_POST)
Thirdly to exclude non-submitted values, you could construct the query string based on these. Something like:
$qry = "SELECT * FROM data WHERE ";
if($_POST['location']) {
$qry .= 'LIKE "%'. mysql_real_escape_string($_POST['location']) .'%"';
}
// etc...
$qry = "SELECT * FROM data WHERE location LIKE '%" . mysql_escape_string($_POST['location']) . "%' And proffession LIKE '%" . mysql_escape_string($_POST['proffession']) . "%' And source LIKE '%" . mysql_escape_string($_POST['source']) . "%'";

Categories