Actually i am performing a search in my database, there are two tables, rent and sale. When user selects parameter for rent with the help of radio button and other parameters, it should search the data in rent table and return the result if it all matches. and same for second table.
But after executing my query i am getting error. Please help me in this.
I am posting all my codes here.
User selection page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>real estate</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function()
{
$("input#tblsection").val('sale_table');
$('input:radio[name=rbtn]').attr('checked',true);
$("#rent").click(function() {
$("#rentselect").attr('style', 'display:block;margin-top:20px;');
$("#saleselect").attr('style', 'display:none;');
$("input#tblsection").val('rent_table');
});
$("#sale").click(function() {
$("#saleselect").attr('style', 'display:block;margin-top:20px;');
$("#rentselect").attr('style', 'display:none;');
$("input#tblsection").val('sale_table');
});
$("#srajax").click(function(){
var rentvalue = $('select#rentselect').val();
var salevalue = $('select#saleselect').val();
var location = $('select#location').val();
var area = $('select#Area').val();
var bedroom = $('select#bedroom').val();
var table = $('input#tblsection').val();
$.post(" testajax.php",
{
rentvalue: rentvalue,
salevalue: salevalue,
location: location,
area: area,
bedroom: bedroom,
table: table
},
function(data) {
alert("Data Loaded: "+data);
});
});
});
</script>
</head>
<body>
<form name="srajax" method="post">
<input type="radio" name="rbtn" value="rent" id="rent" /> <b>Rent</b>
<input type="radio" name="rbtn" value="sale" id="sale" /> <b>Sale</b>
<input type="hidden" name="tablenmae" id="tblsection" value="tablesale">
<br>
<select id="rentselect" style='display:none;'>
<option value="">----Select Budget For Rent----</option>
<option value="5000">5000</option>
<option value="5000_10000">5000 to 10000</option>
<option value="11000_20000">11000 to 20000</option>
<option value="above_20000">Above 20000</option>
</select>
<select id="saleselect" style='margin-top:20px;margin-bottom:20px;'>
<option value="">----Select Budget For Sale----</option>
<option value="5000">100000</option>
<option value="5000_10000">500000</option>
<option value="11000_20000">1000000</option>
<option value="above_20000">2500000</option>
</select>
<br>
<select id="location" name="Location">
<option>----Select Location---</option> <option value="All_Location">All Location</option>
<option value="Central_Mumbai">Central Mumbai</option>
<option value="Mumbai_Harbour">Mumbai Harbour</option>
<option value="Mumbai_Navi">Mumbai Navi</option>
<option value="Mumbai_South">Mumbai South</option>
<option value="Mumbai_Thane">Mumbai Thane</option> </select>
<br>
<br>
<select id="Area" name="Area">
<option value="">-----Select Area---</option>
<option value="All Area">All Area</option>
<option value="Bhandup">Bhandup</option>
<option value="Chembur">Chembur</option>
<option value="Kurla">Kurla</option>
<option value="Mulund">Mulund</option>
<option value="All Area">All Area</option>
<option value="Byculla">Byculla</option>
<option value="Chembur">Chembur</option>
<option value="Govandi">Govandi</option>
<option value="Sewri">Sewri</option>
<option value="Wadala">Wadala</option>
<option value="All Area">All Area</option>
<option value="Airoli">Airoli</option>
<option value="Belapur">Belapur</option>
<option value="Ghansoli">Ghansoli</option>
<option value="Mahape">Mahape</option>
<option value="Nerul">Nerula</option>
<option value="All Area">All Area</option>
<option value="Churchgate">Churchgate</option>
<option value="CST">CST</option>
<option value="Dadar">Dadar</option>
<option value="Fort">Fort</option>
<option value="All Area">All Area</option>
<option value="Brindavan">Brindavan</option>
<option value="Kalothe">Kalothe</option>
<option value="Kapur">Kapur</option>
<option value="Kalwa">Kalwa</option>
<option value="Kopat">Kopat</option> </select><br /><br>
<select id="bedroom" name="bedroom">
<option>---Select Bedroom---</option>
<option value="1BHK">1 BHK</option>
<option value="2BHK">2 BHK</option>
<option value="3BHK">3 BHK</option>
<option value="4BHK">3 BHK</option>
</select>
<br><br><br>
<input type="button" name="search" id="srajax" value="Search"/>
</form>
</body>
</html>
My search page
<?php
$host="localhost"; // Host name
$db_name="netelmbn_realestate"; // Database name
$tbl1_name="rent_table"; // Table1 name
$tbl2_name="sale_table"; //Table2 name
mysql_connect("localhost","netelmbn","password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
echo "<pre>";
print_r($_POST);
if($_POST['table'] == 'rent_table')
{
$result=mysql_query("select * from rent_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND $budget='budget' ");
echo "<table cellpadding='20'>";
echo '<tr>';
while ($row = mysql_fetch_assoc($result)) {
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
}
if($_POST['table'] == 'sale_table')
{
$result=mysql_query("select * from sale_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND $budget='budget' ");
echo "<table cellpadding='20'>";
echo '<tr>';
while ($row = mysql_fetch_assoc($result)) {
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
}
while ($row = mysql_fetch_assoc($result)) {
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
exit;
and i have rent_table
CREATE TABLE `rent_table` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`location` varchar(40) NOT NULL,
`area` varchar(40) NOT NULL,
`bedroom` varchar(20) NOT NULL,
`budget` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
samething for sale_table
please help me where i am going wrong. Here is the error image i am getting this error:
error http://netelity.com/realestate/error.png
After any and all database queries, check if the query was actually successful and use the appropriate debugging functions to find why it wasn't. Don't just assume that it worked.
$result = mysql_query(...);
if (!$result) {
echo mysql_error();
die;
}
ok here you go:
1) add this before if($_POST['table'] == 'rent_table') :
$location = $_POST['location'];
$area = $_POST['area'];
$bedroom = $_POST['bedroom'];
$budget = $_POST['budget'];
2) you have a mistake in your query : AND $budget='budget' should be AND budget='$budget'
That error indicates an error in the queries and therefore the MySQL resultset is invalid. The error seems to be in the check "$budget = 'budget'" that $budget should be enclosed in quotes. Try these versions:
Query1: $result=mysql_query("select * from rent_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND '$budget'='budget' ");
Query2: $result=mysql_query("select * from sale_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND '$budget'='budget' ");
Hope it helps!
Related
Html Form:
<form>
<select name="country[]" id="country" multiple>
<option value="any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
</form>
PHP Code
<?php
$country = $_REQUEST['country'];
if($country=="")
$countrysql = "";
else
{
if($country == "Any") $countrysql = "";
else
{
$country = str_replace(",","','",$country);
$countrysql = " and Country in ('$country')";
}
}
$queryString = "SELECT * FROM register where $countrysql";
?>
I have created a form in PHP and I want to search multiple options. I already created table Register and a column Country. I am getting the result If I give single value. If I give multiple I am not getting the result. Please help.
You evaluate in if($country == "Any") the word Any is not equal to any in option <option value="any">any</option>
But I suggest this php code:
<?php
$country="";
$countryError="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["country"])){
$countryError = "Country is required";
}else{
$country = $_POST["country"];
}
if($country == "Any") {
$queryString = "SELECT * FROM register";
}else{
$queryString = "SELECT * FROM register where Country in ('$country')";
}
// Print the SQL string:
echo $queryString;
}
?>
The html tags:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="country" id="country" multiple>
<option value="Any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<span class="error"><?php echo $countryError;?></span>
i want to change tag option based on values coming from database.if value is "1" i want to show different options and different for other values.
<<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
Because
No closing ; on here <option value='4'>Maths</option></select>"
and here too <option value='8'>Maths</option></select>"
First if() condition missing end }.
So final code is
<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number = $_POST['number'];
//echo $number;
if ( $number == "1" )
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
You are missing the ; off of the end of both echos:
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
Note: Sorry about poor layout, quick posting of answer!
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"; //missing ; here
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"; //missing ; here
}
} // missing } here
I have a Tour Search application where user can search for available tours based on three different parameters- Region, Country and Duration. Currently the code which I am using is showing the Output Result in the same page. I want the output result to show in a different page.
Below is my PHP Code:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
//define the index for the All option
$optionAllValue = 0; //add here the option index value used for the 'All' option
//define the where clause for the query
//in order to avoid many conditions verifications, we start it as 1=1
$whereClause = "1=1";
//now we check if the option selected for each field is not the value defined for the option 'All'
//this is just an example, and the best would be to create a function to avoid the replication of code
if($region != $optionAllValue)
{
$whereClause = $whereClause." and region='$region'";
}
if($country != $optionAllValue)
{
$whereClause = $whereClause." and country='$country'";
}
if($duration != $optionAllValue)
{
$whereClause = $whereClause." and duration='$duration'";
}
$query = "select * from byp_tour where ".$whereClause;
//original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
$tour = mysql_query($query);
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name"; // HERE IS THE OUTPUT RESULT
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>BYP Test</title>
</head>
<body>
<form action="searchtest.php" method="post">
<div>
<label>Region</label>
<select id="region" name="region">
<option value="0">All</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
</div>
<div>
<label>Country</label>
<select id="country" name="country">
<option value="0">All</option>
<option value="1">Cambodia</option>
<option value="2">Thailand</option>
<option value="3">Vietnam</option>
<option value="4">Myanmar</option>
<option value="5">Laos</option>
<option value="6">Ethiopia</option>
<option value="7">France</option>
<option value="8">New York City</option>
<option value="9">Melbourne</option>
</select>
</div>
<div>
<label>Duration</label>
<select id="duration" name="duration">
<option value="0">All</option>
<option value="1">5 Days</option>
<option value="2">10 Days</option>
</select>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
You need to add target="_blank" to <form>:
<form action="searchtest.php" method="post" target="_blank">
I am developing a Tour and Travel website and I have an option to allow my users to search Tours based on Region, Country and Duration.
Currently, the search option which I have developed allows to search tours and show results if all the search parameters match. For example, if a Tour is listed under Region- Africa, Country- Ethiopia and Duration- 5 Days, I have to select all the parameters to show up the Tour. But I want to modify the search option so that:
There would be a Select All option for Region, Country and Duration.
If someone searches using Select All option for all the three
parameters it would show all Tours available in the database.
If someone selects a Region only with the other two as Select All, it
would show only those tours which are listed under that Region. Same
with Country and Duration.
If someone selects only two parameters Like Country and Duration with
the third option as Select All, it would show only those tours which
are listed under the selected Country and Duration. Same with Region
and Duration.
Here is my Database Structure
The PHP code which I am using now is:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
$tour = mysql_query("select * from byp_tour where region='$region' and country='$country' and duration='$duration'");
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name";
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>BYP Test</title>
</head>
<body>
<form action="byptest.php" method="post">
<div>
<label>Region</label>
<select id="region" name="region">
<option value="0">Select</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
</div>
<div>
<label>Country</label>
<select id="country" name="country">
<option value="0">Select</option>
<option value="1">Cambodia</option>
<option value="2">Thailand</option>
<option value="3">Vietnam</option>
<option value="4">Myanmar</option>
<option value="5">Laos</option>
<option value="6">Ethiopia</option>
<option value="7">France</option>
<option value="8">New York City</option>
<option value="9">Melbourne</option>
</select>
</div>
<div>
<label>Duration</label>
<select id="duration" name="duration">
<option value="0">Select</option>
<option value="1">5 Days</option>
</select>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
There are some possibilities to follow on your code.
You can let the 0 index option on the selects to be "All", instead of being "Select".
E.g.
The regions select
<select id="region" name="region">
<option value="0">Select</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
will be transformed into
<select id="region" name="region">
<option value="0">All</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
Another possibility is to create a new option with a specific index, e.g., 9999;
This way you can modify your query in order to select all the elements depending on your selection.
The code should be modified to:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
//define the index for the All option
$optionAllValue = 0; //add here the option index value used for the 'All' option
//define the where clause for the query
//in order to avoid many conditions verifications, we start it as 1=1
$whereClause = "1=1";
//now we check if the option selected for each field is not the value defined for the option 'All'
//this is just an example, and the best would be to create a function to avoid the replication of code
if($region != $optionAllValue)
{
$whereClause = $whereClause." and region='$region'";
}
if($country != $optionAllValue)
{
$whereClause = $whereClause." and country='$country'";
}
if($duration != $optionAllValue)
{
$whereClause = $whereClause." and duration='$duration'";
}
$query = "select * from byp_tour where ".$whereClause;
//original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
$tour = mysql_query($query);
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name";
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
The entire modified example can be found below:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
//define the index for the All option
$optionAllValue = 0; //add here the option index value used for the 'All' option
//define the where clause for the query
//in order to avoid many conditions verifications, we start it as 1=1
$whereClause = "1=1";
//now we check if the option selected for each field is not the value defined for the option 'All'
//this is just an example, and the best would be to create a function to avoid the replication of code
if($region != $optionAllValue)
{
$whereClause = $whereClause." and region='$region'";
}
if($country != $optionAllValue)
{
$whereClause = $whereClause." and country='$country'";
}
if($duration != $optionAllValue)
{
$whereClause = $whereClause." and duration='$duration'";
}
$query = "select * from byp_tour where ".$whereClause;
//original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
$tour = mysql_query($query);
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name";
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>BYP Test</title>
</head>
<body>
<form action="byptest.php" method="post">
<div>
<label>Region</label>
<select id="region" name="region">
<option value="0">All</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
</div>
<div>
<label>Country</label>
<select id="country" name="country">
<option value="0">All</option>
<option value="1">Cambodia</option>
<option value="2">Thailand</option>
<option value="3">Vietnam</option>
<option value="4">Myanmar</option>
<option value="5">Laos</option>
<option value="6">Ethiopia</option>
<option value="7">France</option>
<option value="8">New York City</option>
<option value="9">Melbourne</option>
</select>
</div>
<div>
<label>Duration</label>
<select id="duration" name="duration">
<option value="0">All</option>
<option value="1">5 Days</option>
</select>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
I have noticed your Selects are interdependents. It means you need to update the current options in cascade after on of the Selects are modified. To be able to do execute the cascading you can reference other questions on StackOverflow: cascading dropdowns from mysql with javascript and php
You have to build up your query depending on what variables are set. You could do something like:
$where = '';
if (!empty(($duration)) {
$where = " AND duration='$duration'";
}
if (!empty($country)) {
$where = " AND duration='$country'";
}
if (!empty($duration)) {
$where = " AND duration='$duration'";
}
$where = substr($where, 5);
$sql = "select * from byp_tour" . ($where ? " WHERE $where" : '');
$tour = mysql_query($sql);
Im no PHP whiz but I would first in your HTML form you would want a "Select All" (unless that's what you already mean by "Select")
If you did "Select All" Value = "0" Then after you check for form submission with your:
if(isset($_POST['submit'])){
YOu would then need if statements to check for any field that has a Value of "0". If a field is Set to 0 you would then want to remove that part of your database query.
For Ex. If Region = Select All then in your query remove region='$region'
That way you are not returning 0 results as your Region field should not have any region of 0.
Hope that kind of makes sense.
i'm working on a php script wherein i must add certain score value at each row. I was able to display all the rows but i'm not sure on how would I able to store each of the given score in a variable and what query should I make to add all of them.
Here's my code
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
$query = mysql_query('insert query here');
}
?>
</table>
</body>
</html>
any suggestions are appreciated. Thanks in advance.:D
I think you need the id of each changed row (maybe as a hidden field for each row. Then just do a loop through all received rows and UPDATE each one.
You might also want to change all of your form field names to use the array format. This way it's easier to make your PHP loop throught them.
Sample row:
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score['.$i["id"].']" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
Now just loop through the $_POST["score"] array and use the appropriate ID for your update.
foreach($_POST["score"] as $id => $value{
// ESCAPE your db values!!!!!
// query stuff with $value and $id
}
Also keep in Mind
mysql is deprecated! Use mysqli
Escape anything from outside sources like $_POST before use in SQL
You just needs to make an array of your drop down box like below,
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score[".$i['com_id']."]" id="score" size="1">
<option valyue="5">5</option>
<option valyue="5">10</option>
<option valyue="5">15</option>
<option valyue="5">20</option>
<option valyue="5">25</option>
<option valyue="5">30</option>
<option valyue="5">35</option>
<option valyue="5">40</option>
<option valyue="5">45</option>
<option valyue="5">50</option>
</select></td></tr>';
echo'<br>';
}
and you can access it for all of your comments
<option valyue="5">50</option>
should be
<option value="5">50</option>
To send the value of a comment to database you need to add a ID of the comment
you should loop something like this.
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
echo '<input type="hidden" name="id" value="'.$i['com_id'].'">';
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
echo'<br>';
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
}
I guess the easiest way for you is the following (a mix of the other solutions and comments):
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$x = 0;
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
$x++;
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score_'.$x.'" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
for($y = 0;$y <= $x; $y++)
{
//This query is no sql injection save. Please add filters for productive uses!!
$query = mysql_query('UPDATE table_name SET score = '.$_POST["score_".$y].' WHERE id='.$id);
}
?>
</table>
</body>
</html>
Code is no tested!