Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-23-2004, 10:20 PM   PM User | #1
dogugotw
New to the CF scene

 
Join Date: Oct 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
dogugotw is an unknown quantity at this point
Sequential MySQL with PHP/HTML fails when spaces are invloved

Noob alert.
What I'm trying to do
User sees a form, one field, select drop down box populated via a query against a MySQL db. This works (whoo whoo!).,
User selects a value, a second query is run using the selected value from the first query as the WHERE clause in the 2nd query.
If the selected value from Query1 is a single word, Query2 works like a charm.
If the selected value from Query1 has more than one word (and spaces), $site when echoed back to the screen is just the first word and Query2 results are null.

I know I'm missing something stupid but can't find it in any of the books, articles, forums, or googled searches I've tried. Any pointers or tips are greatly appreciated.

Doug

<html>

<body>
Select the site name from the list below<br>
Note - if you start typing the name, you don't have to scroll to the name.<br>
</body>
<br>
<form>

<?php


// Define variables

$server = 'localhost';

$username = 'web';

$password = 'user';

$database = 'HomeData';


//$query = "Select site, username, password from sitelogins where site = '$site'";
$query = "Select site from sitelogins order by site";



// connect to mysql

$db = mysql_connect($server, $username, $password);


// connect to db

mysql_select_db($database, $db);



// run query and print results. This bit works great, list is what I expect to see.

$result = mysql_query($query, $db);

echo "<select name=\"site\">";
if(!$result) die ("query failed");

while($row = mysql_fetch_row($result)) {
echo "<OPTION VALUE=".$row[0].">".$row[0]."</OPTION>";
}
echo "</select>";

// if $site is one word, the rest of this works also.
// if $site is two words or more, I get the first word only and the query returns null.
echo "<br><br>The requested site is $site <br><br>";
echo "<table border=1>\n";
echo "<tr><td>The username is:</td><td>The password is:</td>";


$query2 = "Select * from sitelogins where site = '$site'";
$result2 = mysql_query($query2, $db);

if(!$result2) die ("query failed");

while($row = mysql_fetch_row($result2)) {
echo "<tr><td>$row[1]</td><td>$row[2]</td></tr>";

}

echo "</table>";



// close connnection

mysql_close($db);

?>

<br>

<input type="submit" value = "Get Password">

</form>
</html>
dogugotw is offline   Reply With Quote
Old 10-24-2004, 01:33 AM   PM User | #2
boeing747fp
Regular Coder

 
Join Date: Oct 2003
Posts: 599
Thanks: 1
Thanked 1 Time in 1 Post
boeing747fp is an unknown quantity at this point
try sending it with + as a space and on the receiving page, convert it back to space from +
boeing747fp is offline   Reply With Quote
Old 10-24-2004, 02:38 AM   PM User | #3
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
I have had a similar problem to this. I found the solution is to set the dropdown value to an unique id, and then setting the shown value as text. It could take some work for you to convert your table to this, but it is the best solution.
Kurashu is offline   Reply With Quote
Old 10-24-2004, 02:26 PM   PM User | #4
dogugotw
New to the CF scene

 
Join Date: Oct 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
dogugotw is an unknown quantity at this point
Sorry, when I said noob...

I only have one page (one php file). Where and how would I convert what to what and back to what again?

Sorry for being a bit dense here, but I'm in the learning phase of LAMP.

Thanks for the reply.

Doug

Quote:
Originally Posted by boeing747fp
try sending it with + as a space and on the receiving page, convert it back to space from +
dogugotw is offline   Reply With Quote
Old 10-24-2004, 02:29 PM   PM User | #5
dogugotw
New to the CF scene

 
Join Date: Oct 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
dogugotw is an unknown quantity at this point
Still a noob...

Do you mean re-define my MySQL table with the site as a key and as text (already is text actually)?
I'm also not sure what you mean by 'set the dropdown to unique id'. Is this something in the html/php code? If so, can you give me a hint as to what I need to set to something?

Inch by inch... Thank you for your patience and help.

Doug

Quote:
Originally Posted by Kurashu
I have had a similar problem to this. I found the solution is to set the dropdown value to an unique id, and then setting the shown value as text. It could take some work for you to convert your table to this, but it is the best solution.
dogugotw is offline   Reply With Quote
Old 10-24-2004, 02:43 PM   PM User | #6
dogugotw
New to the CF scene

 
Join Date: Oct 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
dogugotw is an unknown quantity at this point
Fixed the problem one way but still want to know how to work with spaces

OK, when fine motor skills don't do the trick, hit it with a hammer.
Did the following in MySQL
Update sitelogins
Set site = replace(site, " ", "_")
Execute
Bang! No spaces, happy happy joy joy.

Now, I still do want to know what's up with the space issue. I know I'll run into this again and don't want to not allow spaces in MySQL data.

TIA for tips and pointers (and feel free to point me to resources rather than solving the problem for me - I like to know how to fish, just give me enough of a pointer to find the answer).

Doug
dogugotw is offline   Reply With Quote
Old 10-24-2004, 08:49 PM   PM User | #7
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
There is something about SQL in general that doesn't like spaces, or at least having spaces submitted to it. What I meant when I said convert is changing this:

Code:
table_1

text
-----
blah
-----
blah
-----
blah
to

Code:
table_1

text | id
-----------
blah | 1
-----------
blah | 2
-----------
blah | 3
Kurashu is offline   Reply With Quote
Old 10-26-2004, 12:26 AM   PM User | #8
dogugotw
New to the CF scene

 
Join Date: Oct 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
dogugotw is an unknown quantity at this point
Another bit of solution from a news group

Someone just suggested I change the line that builds the option list
from
echo "<OPTION VALUE=".$row[0].">".$row[0]."</OPTION>";
to
echo "<option value='$row[0]'>$row[0]</option>";

Works great now! Looks like the version I had, since it didn't quote the 'Value' bit resulted in just the first word being seen. With the 'to' verison, the string is fully recognized and works great.

I'm pretty sure the form is still not completely right, no get or post method, missing bits of the form, and probably some funky logic on my part for the way I'm thinking the code is working vs the way it's actually working...
More fun.

Thanks again for the help.

Doug
dogugotw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:26 AM.


Advertisement
Log in to turn off these ads.