Page 2 of 2

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:00 pm
by kevz
SCREAM: Error suppression ignored for
Notice: Undefined property: PDOStatement::$rowCount in C:\wamp\www\test\index.php on line 10

could not pull city names for country 2

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:02 pm
by seandisanti
on that line, update rowCount to be rowCount()

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:06 pm
by kevz
could not pull city names for country 2

it just says that now in the webbrowser. I don't have a clue whats going on to be honest!

***EDIT****
I have checked to make sure that my table names and column names match whats on the script as well.

EDIT:

Ok now i know whats causing the above issue. The 2nd drop down list is not picking up any values from the database

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:10 pm
by seandisanti
the issue is most likely with the $sql_city query. I made some assumptions there, specifically that you were pulling from table city that contains a field 'country_id' that has the numeric country code.

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:20 pm
by kevz
i included the 2 tables

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:21 pm
by seandisanti
what's the name of the second table? i assumed city, but if it's cities, or something else, it's just a change to line 6

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:28 pm
by seandisanti
my code is working on my side after creating those tables.

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:30 pm
by seandisanti
Sorry, one thing to change to make it work exactly as intended; line 37 (for the submit button) needs a ! added to the condition, to reverse the logic. it should read:

Code: Select all

<input type="submit" value="' . (!(isset($_POST['country']))?'Get cities':'Submit form') . '" />';

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:34 pm
by kevz
the name of table 2 is city

****EDIT*****
Could it be something to do with my internet explorer settings? Maybe somethings is disabled in internet options?

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:36 pm
by seandisanti
Is it still giving an error?

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:38 pm
by kevz
i dont get any errors until i click the submit button. But this is because the 2nd drop down list dont show any values

EDIT: When i click the submit with the 2nd empty drop down list then i get this message "could not pull city names for country 1"

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:42 pm
by seandisanti
I've posted a working copy of the same thing here on my domain... http://seandisanti.com/trystuff.php

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:43 pm
by kevz
this is the webbrower bit where it shows the 2nd drop down list dont show any thing

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:44 pm
by seandisanti
right... click 'get cities', and it should populate the second drop down

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:45 pm
by kevz
hmmmm......when i tried it from your domain, i get the same problem as mine in my browser


*******EDIT********
Sorry man, even right clicking get cities dont do anything on my web browser but it works on your domain.

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:49 pm
by seandisanti
lol, sorry, i didn't mean 'right click', i meant 'that looks right, now just click get cities'

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:51 pm
by kevz
it works fine on yours buts not on mine still. Still saying "cant get cities from country 1/2"

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:52 pm
by kevz
This is the code i got on my notepad ++ at moment.

Code: Select all

 <?php

    $db = new PDO("mysql:host=localhost;dbname=test", "root", "");
    if ($_POST) {//are we responding to a form? 
        $country = $_POST['country'];
        $sql_city = "SELECT * FROM city WHERE country_id =" . $country;
        $cityResult = $db->query($sql_city);
        if ((!$cityResult) || $cityResult->rowCount()) {
            die('could not pull city names for country ' . $country);
        }
    }
    $sql_country = "SELECT * FROM country";
    $countryResult = $db->query($sql_country);
    if ((!$countryResult) || $countryResult->rowCount() == 0) {
        die('no countries pulled from database');
    }
    echo '
    <!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>Country List</title>
        </head>
        <body>';
    echo '<form id="frm" action="' . ((isset($_POST['city'])?'fullform.php':$_SERVER['PHP_SELF'])) . '" method="post">';
    echo '<select name="country">';
    while ($row = $countryResult->fetch(PDO::FETCH_ASSOC)) {
        echo '<option value="' . $row['id'] . '"' . ((isset($country) && $country == $row['id']) ? ' selected ' : '') . '>' . $row['country'] . '</option>';
    }
    echo "</select>";
    echo '<div id="cityLayer"><select name="city" id="city">';
    if (!empty($cityResult)) {
        while ($cRow = $cityResult->fetch(PDO::FETCH_ASSOC)) {
            echo '<option value="' . $cRow['id'] . '">' . $cRow['city'] . '</option>';
        }
    }
    echo '</select></div>
        <input type="submit" value="' . (!(isset($_POST['country']))?'Get cities':'Submit form') . '" />';
    echo '</body>
        </html>';
	

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:54 pm
by seandisanti
here's what I've got for everything below the credentials. I removed just that line because now it's got mine instead of yours...

Code: Select all

    if ($_POST) {//are we responding to a form? 
        $country = $_POST['country'];
        $sql_city = "SELECT * FROM city WHERE country_id =" . $country;
        $cityResult = $db->query($sql_city);
        if ((!$cityResult) || $cityResult->rowCount() == 0) { 
           die('could not pull city names for country ' . $country);
        }
    }
    $sql_country = "SELECT * FROM country";
    $countryResult = $db->query($sql_country);
    if ((!$countryResult) || $countryResult->rowCount() == 0) {
        die('no countries pulled from database');
    }
    echo '
    <!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>Country List</title>
        </head>
        <body>';
    echo '<form id="frm" action="' . ((isset($_POST['city'])?'fullform.php':$_SERVER['PHP_SELF'])) . '" method="post">';
    echo '<select name="country">';
    while ($row = $countryResult->fetch(PDO::FETCH_ASSOC)) {
        echo '<option value="' . $row['id'] . '"' . ((isset($country) && $country == $row['id']) ? ' selected ' : '') . '>' . $row['country'] . '</option>';
    }
    echo "</select>";
    echo '<div id="cityLayer"><select name="city" id="city">';
    if (!empty($cityResult)) {
        while ($cRow = $cityResult->fetch(PDO::FETCH_ASSOC)) {
            echo '<option value="' . $cRow['id'] . '">' . $cRow['city'] . '</option>';
        }
    }
    echo '</select></div>
        <input type="submit" value="' . (!(isset($_POST['country']))?'Get cities':'Submit form') . '" />';
    echo '</body>
        </html>';
      

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 12:55 pm
by seandisanti
if you want; go to http://localhost/phpMyAdmin then select the 'test' database, then click export and have it save to a .sql file. if you message that to me i can find the difference in the schema, because i think that's where the issue is

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 1:09 pm
by kevz
the file!

Re: Submit dropdown list values to mysql

Posted: Fri Mar 15, 2013 1:49 pm
by seandisanti
have to actually include the file or it's contents... :D

Re: Submit dropdown list values to mysql

Posted: Mon Mar 18, 2013 12:39 pm
by kevz
The problem has been fixed, thanks alot. i also found a good tutorial about drop down lists using javascript http://www.youtube.com/watch?v=FTJszYP6O6s

If you are a newbie like me and really desperate to create dynamic drop down list, just visit the site above and you will be all good. Also ask the guys on here for further help, they are really good esp. seandisanti

Re: Submit dropdown list values to mysql

Posted: Mon Mar 18, 2013 2:21 pm
by seandisanti
lol, thanks, I'm glad you got it sorted, and there are a lot of people on here that are happy to help, and I'd say several offer better help than me, but the consideration is appreciated and I'm glad i could help