Problem in drop Down List Box while write it to the data base table.

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 3273 Views - Last Post: 17 November 2011 - 04:20 AM Rate Topic: -----

#1 adwani_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 16-November 11

Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 01:21 AM

<?php
 $server = 'localhost';
 $user = 'xxxxx';
 $pass = 'xxxxx';
 $db = 'xxxxxxx';
 // Connect to Database
 $connection = mysql_connect($server, $user, $pass)
 or die ("Could not connect to server ... \n" . mysql_error ());
 mysql_select_db($db)
 or die ("Could not connect to database ... \n" . mysql_error ());
 $result = @mysql_query("select id,guide from xxxxxx.guide");
print "<p>Select Guide :\n";
print "<select name=\"id\">\n";
while ($row = mysql_fetch_assoc($result))
{
 $id = $row['id'];
 $guide = $row['guide'];
print "<option value=$id>$guide\n";
}
print "</select>\n";
print "</p>\n";
mysql_query("insert into guideresult SET guide='$guide'")
or die(mysql_error());
?>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="dropdown.php">
<input type="Submit" name="submit" value="Submit">
</form>
</body>
</html>



File name
dropdown.php

Above code is used to display a drop down list box from the database.
For which am using two tables.
Table Name
1.guide
2.guideresult

Table Description
First table is having the guide name's which are going to display in the drop down list box.
Second table is used to store the guide name which is selected from the drop down list box.
Result
There is no problem to show the guide name's as a drop down list....Its clearly showing the guide name in the drop down list format.

Problem
While insert the selected guide name to the GUIDERESULT table its does not inserting the selected guide name.
What ever the guide name i select and submitted means also its not insert the selected guide name inot the database but its inserting the last name which is presnt in the GUIDE table to the GUIDERESULT table.

This post has been edited by Dormilich: 16 November 2011 - 01:27 AM
Reason for edit:: please use [code] /* your code source here */ [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: Problem in drop Down List Box while write it to the data base table.

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 01:32 AM

View Postadwani_77, on 16 November 2011 - 09:21 AM, said:

What ever the guide name i select and submitted means also its not insert the selected guide name inot the database but its inserting the last name which is presnt in the GUIDE table to the GUIDERESULT table.

but that’s what you tell it to do. on line #22 you tell it to insert the value of $guide, which was last defined on line #17 with the last value of the SELECT query.

maybe you meant to insert $_POST['id'] ? (if it is set and validated, of course)
Was This Post Helpful? 0
  • +
  • -

#3 adwani_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 16-November 11

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 02:33 AM

Using $_POST['id'] also i cant able to get the selected name..Still its inserting the last name which is presented in the table...

This post has been edited by Dormilich: 16 November 2011 - 02:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 02:36 AM

that wouldn’t be possible if you changed the code accordingly. show us your code and we can tell you what it is.
Was This Post Helpful? 0
  • +
  • -

#5 adwani_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 16-November 11

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 02:45 AM

<?php
 $server = 'localhost';
 $user = 'test';
 $pass = '123456';
 $db = 'sample';
 // Connect to Database
 $connection = mysql_connect($server, $user, $pass) 
 or die ("Could not connect to server ... \n" . mysql_error ());
 mysql_select_db($db) 
 or die ("Could not connect to database ... \n" . mysql_error ());
 $result = @mysql_query("select id, guide from pradap.guide_name");
print "<p>Select Guide :\n";
print "<select name=\"id\">\n";
while ($_POST = mysql_fetch_assoc($result))
{

 $idnumber = $_POST['id'];
 $guidename = $_POST['guide'];
print "<option value=$idnumber>$guidename\n";
}
print "</select>\n";
print "</p>\n";
mysql_query("insert into guideresult SET guide='$guidename'")
or die(mysql_error()); 
?> 

<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="dropdown.php">
<input type="Submit" name="submit" value="Submit">
</form>
</body>
</html>




This file is know as dropdown.php

Actually it is my full code...i did not included that in any form or any other page.....

Simply i just need to read the data from database(table) like a dropdown list and user want to select the option from that list after that the selected data is inserted to the another table.....that's only i need....For that only am using the above code....

This post has been edited by no2pencil: 16 November 2011 - 02:52 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 02:47 AM

adwani_77 : Please use code tags when posting.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 03:05 AM

ok, why do you overwrite the system’s $_POST superglobal, where the user choice was stored? i.e. changing $row to $_POST doesn’t change anything at all (besides the variable name).

obviously we need a change of plans. first we need to analyse, what your code is supposed to do. I see 2 things:
- print the dropdown
- save the user choice

these are two entirely different things so we need at least 2 scripts for that (otherwise they interfere, as you can see in the current code)

This post has been edited by Dormilich: 16 November 2011 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 03:11 AM

Take a closer look at this part of the code:
while ($_POST = mysql_fetch_assoc($result))
{
    $idnumber = $_POST['id'];
    $guidename = $_POST['guide'];
    print "<option value=$idnumber>$guidename\n";
}
print "</select>\n";
print "</p>\n";
mysql_query("insert into guideresult SET guide='$guidename'") 
    or die(mysql_error());


There are two major issues there.

First, you are assigning the array of fields from each row in your MySQL result to the $_POST variable, effectively overwriting any user input you may have been sent. - You should be using something like $row, like you did in your first post.

Second, the INSERT statement is using the $guidename value from within the loop as it's value. This means it will always use the last value retrieved from the database, rather than the user input.

And third, this is being executed before the <html> element of the page, and long before the <form> element. If you want the select box to be sent as a part of the form submission, it must be within the <form> element. And nothing but the doctype declaration (which is missing from that page!) is supposed to come before the <html> tag.


To insert the user input, test first whether the appropriate $_POST value exists (in your case: $_POST['id']), and then use that in the INSERT statement. You can use the isset function for that. And you should use mysql_real_escape_string on the value as well.
if (isset($_POST['theFieldName'])) {
    $safeValue = mysql_real_escape_string($_POST['theFieldName']);
    $sql = "INSERT INTO mytbl(colName) VALUES('{$safeValue}')";
    // Then execute the query and test the results...
}
else {
    echo "No value received. Not performing an INSERT!";
}


Was This Post Helpful? 2
  • +
  • -

#9 adwani_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 16-November 11

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 03:50 AM

Really i would like to thak you for spending your time for me...Really your answer is good but i still i having the problem.


Now i changed my code as the two files.

1.dropdown.php
2.testtest.php

1.dropdown.php

<?php
 $server = 'localhost';
 $user = 'pradap';
 $pass = '123456';
 $db = 'pradap';
 // Connect to Database
 $connection = mysql_connect($server, $user, $pass) 
 or die ("Could not connect to server ... \n" . mysql_error ());
 mysql_select_db($db) 
 or die ("Could not connect to database ... \n" . mysql_error ());
 $result = @mysql_query("select id, guide from pradap.guide_name");
print "<p>Select Guide :\n";
print "<select name=\"id\">\n";
while ($row = mysql_fetch_assoc($result))
{
 $idnumber = $row['id'];
 $guidename = $row['guide'];
print "<option value=$idnumber>$guidename\n";
}
print "</select>\n";
print "</p>\n";
?> 
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="testtest.php">
<input type="Submit" name="submit" value="Submit">
</form>
</body>
</html>



2.testtest.php

<?php
 $server = 'localhost';
 $user = 'pradap';
 $pass = '123456';
 $db = 'pradap';
 // Connect to Database
 $connection = mysql_connect($server, $user, $pass) 
 or die ("Could not connect to server ... \n" . mysql_error ());
 mysql_select_db($db) 
 or die ("Could not connect to database ... \n" . mysql_error ());
 if (isset($_POST['guidename'])) {
 $safeValue = mysql_real_escape_string($_POST['guidename']);
 $sql = "INSERT INTO guideresult(guide) VALUES('{$safeValue}')";
 // Then execute the query and test the results...
	}
	else {
	    echo "No value received. Not performing an INSERT!";
	}
?>




I think this is only u make me to do....

Now the result is no value received.

Tell me clearly by editing my code and showing because am new to php so i can not know clearly anything in php.

edit my code and tell me....
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 04:06 AM

did you print the dropdown in the form as Atli said?

View PostAtli, on 16 November 2011 - 11:11 AM, said:

And third, this is being executed before the <html> element of the page, and long before the <form> element. If you want the select box to be sent as a part of the form submission, it must be within the <form> element. And nothing but the doctype declaration (which is missing from that page!) is supposed to come before the <html> tag.

the only thing I see within the form tags is a submit button.
Was This Post Helpful? 0
  • +
  • -

#11 adwani_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 16-November 11

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 04:18 AM

Sorry i dont know want you are telling to do me...Try to send me more description about that problem.other wise just edit my code and show me the problem....
thank you very much...
Was This Post Helpful? 0
  • +
  • -

#12 adwani_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 16-November 11

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 04:27 AM

I just need to save the user choice to database..i did not need to print....
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 05:02 AM

View Postadwani_77, on 16 November 2011 - 12:27 PM, said:

I just need to save the user choice to database..i did not need to print....

see post #10.
Was This Post Helpful? 0
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 06:08 AM

View Postadwani_77, on 16 November 2011 - 11:18 AM, said:

other wise just edit my code...

I'm sorry, but we won't do that. We won't just give you a working version of your code when it is clear you don't really understand it. I'm more than happy to explain the theory; how things work in general, so you can fix the code yourself, but we won't just fix it for you. - We aim to teach people, not do their homework/assignments for them.


To that end, this is how HTML forms work...

A HTML form starts in a <form> tag and ends in a </form> tag. Every piece of data you want sent with that form must be withing these tags. (Pay special attention to that last line!)
The action attribute on the form tag tells the form where the data is supposed to go; to which page the form data is to be sent.

The data sent as a part of the form is represented as input tags inside the form. Those are tags like <input>, <textarea> and <select>. - I'll say it again, because this is an important point: These tags must be inside the <form> tags to be considered a part of the form.

For example:
<!--
	A POST form, sent to the "process.php" page.
-->
<form action="process.php" method="post">
	<label>Name:</label> 
		<!-- This is a normal text input field -->
		<input type="text" name="first_name"><br>
	
	<label>Gender:</label>
		<!-- This is a dropdown list -->
		<select name="gender">
			<option value="f" selected>Female</option>
			<option value="m">Male</option>
		</select><br>
		
	<label>Bio:</label>
		<!-- This is a larger, multi-line text input -->
		<textarea name="bio"></textarea><br>
		
	<!-- 
		This is a "submit" button, which triggers the
		form to send the data.
	-->
	<input type="submit" value="Send">
	
	<!-- 
		Reset elements clear the form of everything that
		the user has written/selected, reverting it to
		how it was when the page loaded.
	-->
	<input type="reset" value="Reset">
</form>

<!-- 
	This drop down WILL NOT BE sent with the form.
	It is outside the <form> element, so it does not
	belong to it.
-->
<select name="language">
	<option>English</option>
	<option>French</option>
	<option>German</option>
</select>



The PHP page the <form action=""> property points to can access the values via the $_POST or $_GET arrays. Which one is used depends on the <form method=""> attribute. You can set it to either "post" or "get". (The "post" method is recommended when you can. It defaults to "get" if the method attribute is omitted.)

You access the form values by the <input name=""> attribute. So if it is <input name="user">, then you access it like $_POST["user"]. To figure out if a form value was sent, use the isset or empty functions. (See the links for the difference between them.) For example:
if (isset($_POST['user'])) {
    // Do stuff with $_POST['user']
}
else {
    // There is no "user" form field.
}



... And that's about it.

Look though your code, or rather: the HTML your code generates, and you should see why your value isn't being sent with the form.


Edit:
By the way, I should probably point out that the code I posted in my earlier post was purposefully incomplete. There is a line missing. My comment explains what that line should do. If you know how to use MySQL in PHP, it should be easy to finish. If not, I suggest you read up on that topic as well.

This post has been edited by Atli: 16 November 2011 - 06:20 AM

Was This Post Helpful? 2
  • +
  • -

#15 adwani_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 16-November 11

Re: Problem in drop Down List Box while write it to the data base table.

Posted 16 November 2011 - 09:39 PM

You are telling me to put the value which is going to save inside the form tag...then only it will take that selected value to next page like that right...

I dont know how to use the php code inside the html tag.....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2