Submit dropdown list values to mysql

Links for php scripts

Moderators: macek, egami, gesf

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:00 pm

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

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:02 pm

on that line, update rowCount to be rowCount()

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:06 pm

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
Last edited by kevz on Fri Mar 15, 2013 12:10 pm, edited 1 time in total.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:10 pm

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.

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:20 pm

i included the 2 tables
Attachments
tables.gif
the 2 tables: country and city
tables.gif (7.1 KiB) Viewed 1455 times

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:21 pm

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

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:28 pm

my code is working on my side after creating those tables.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:30 pm

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') . '" />';

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:34 pm

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?
Last edited by kevz on Fri Mar 15, 2013 12:37 pm, edited 1 time in total.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:36 pm

Is it still giving an error?

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:38 pm

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"

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:42 pm

I've posted a working copy of the same thing here on my domain... http://seandisanti.com/trystuff.php

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:43 pm

this is the webbrower bit where it shows the 2nd drop down list dont show any thing
Attachments
webbrowser.gif
webbrowser.gif (2.39 KiB) Viewed 1452 times

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:44 pm

right... click 'get cities', and it should populate the second drop down

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:45 pm

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.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:49 pm

lol, sorry, i didn't mean 'right click', i meant 'that looks right, now just click get cities'

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:51 pm

it works fine on yours buts not on mine still. Still saying "cant get cities from country 1/2"

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 12:52 pm

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>';
   

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:54 pm

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>'
;
      

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 12:55 pm

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

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Fri Mar 15, 2013 1:09 pm

the file!

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Fri Mar 15, 2013 1:49 pm

have to actually include the file or it's contents... :D

kevz
New php-forum User
New php-forum User
Posts: 29
Joined: Fri Mar 15, 2013 6:50 am

Re: Submit dropdown list values to mysql

Postby kevz » Mon Mar 18, 2013 12:39 pm

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

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Submit dropdown list values to mysql

Postby seandisanti » Mon Mar 18, 2013 2:21 pm

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


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest

cron