Forum Moderators: coopster

Message Too Old, No Replies

Another drop-down box problem

Have text inputs working, but need to get dropdown as well

         

stuartindigo

4:26 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Ok, I'm working on a plugin for Wordpress based on a previous one.

A text-box input works ok - sample
<fieldset class="small"><legend><?php _e('Title'); ?></legend>
<input type="text" name="biblio_title" class="input" size=45 value="<?php if ( !empty($data) ) echo htmlspecialchars($data->title); ?>" />
</fieldset>


What do I need to do to make a drop-down box work in a similar way?

eg I have:
<fieldset class="small"><legend><?php _e('Language'); ?></legend>

<select name="boek_boeklang">
<option value="English">English</option>
<option selected value="Dutch">Dutch</option>
<option value="German">German</option>
<option value="French">French</option>
<option value="Latin">Latin</option>
<option value="Greek">Greek</option>
<option value="Spanish">Spanish</option>
<option value="Italian">Italian</option>

</select>

</fieldset>


I know I have to do something which is equivalent to the
<?php if ( !empty($data) ) echo htmlspecialchars($data->title); ?>

in the text example, but I am totally confused

Matthew1980

5:57 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there stuartindigo,

And welcome to the forum :) [webmasterworld.com ]

Does the contents of: <?php _e('Language'); ?> hold more than one language, because if you are going to try to do a drop down of all the languages, you will need a foreach loop (if the data is in an array) or just a plain for loop to go through the data that is potentially stored in: <?php _e('Language'); ?> and then build the drop down around the data being looped through.

As for the 'working' version:<?php if (!empty($data)){echo htmlspecialchars($data->title); }?> I always prefer to use opening and closing braces just so that the code is easier to read and IMO makes for better coding practices.

If you could tell us what format the data is stored in the variable, we can show you how to proceed..

For example:-

$places = array('england','ireland','scotland','wales');

$dropDown = "<select name=\"places\">";
foreach($places AS $place){
$dropDown .= "<option value=\"".$place."\">".$place."</option>";
}
$dropDown .= "</select>";

echo $dropDown;

Hope that helps :)

Cheers,
MRb

stuartindigo

6:28 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



The
<legend><?php _e('Language'); ?></legend>
is just the title for the management input - so a bit of a red herring.

In general the name of a field is "prefix_name" with the data from the input being assigned to "name" I've tried both
<select name="prefix_name">

and
<select name=name">


But neither seems to pass the data to the field

stuartindigo

7:03 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Update: the variables are being passed (used an error message to check they exist), problem appears to be to do with my insertion into the database.

Matthew1980

7:35 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there stuartindigo,

When you say insertion to the DB, are you generating (dynamically) a statement or is this a static one? If it's not a huge code dump, post the offending query & we can have a look :)

Cheers,
MRb

stuartindigo

11:29 am on Aug 5, 2010 (gmt 0)

10+ Year Member



I've just tried a total reinstall of my testbed server (using turnkey linux wordpress appliance), as I could not get access to the mysql server to look at it either through webmin or phpmyadmin - I'll go through my coding again and reinstall the plugin, and see if I still get the same problem before posting what could be a largeish code segment.

stuartindigo

6:08 pm on Aug 5, 2010 (gmt 0)

10+ Year Member



Okay - still not working - nothing being written to database - here's the whole grisly function.

elseif ( $updateaction == 'add_boek' )
{
$title = !empty($_REQUEST['boek_title']) ? $_REQUEST['boek_title'] : '';
$subtitle = !empty($_REQUEST['boek_subtitle']) ? $_REQUEST['boek_subtitle'] : '';
$originaltitle = !empty($_REQUEST['boek_originaltitle']) ? $_REQUEST['boek_originaltitle'] : '';
$reeks = !empty($_REQUEST['boek_reeks']) ? $_REQUEST['boek_reeks'] : '';
$publisher = !empty($_REQUEST['boek_publisher']) ? $_REQUEST['boek_publisher'] : '';
$volgnumber = !empty($_REQUEST['boek_volgnumber']) ? $_REQUEST['boek_volgnumber'] : '';
$yearfirstedition = !empty($_REQUEST['boek_yearfirstedition']) ? $_REQUEST['boek_yearfirstedition'] : '';
$yearthisedition = !empty($_REQUEST['boek_yearthisedition']) ? $_REQUEST['boek_yearthisedition'] : '';
$purchased = !empty($_REQUEST['boek_purchased']) ? $_REQUEST['boek_purchased'] : '';
$AuthorSurname = !empty($_REQUEST['boek_AuthorSurname']) ? $_REQUEST['boek_AuthorSurname'] : '';
$AuthorForename = !empty($_REQUEST['boek_AuthorForename']) ? $_REQUEST['boek_AuthorForename'] : '';
$Authorfunction = !empty($_REQUEST['boek_Authorfunction']) ? $_REQUEST['boek_Authorfunction'] : '';
$CoAuthorSurname = !empty($_REQUEST['boek_CoAuthorSurname']) ? $_REQUEST['boek_CoAuthorSurname'] : '';
$CoAuthorForename = !empty($_REQUEST['boek_CoAuthorForename']) ? $_REQUEST['boek_CoAuthorForename'] : '';
$CoAuthorfunction = !empty($_REQUEST['boek_CoAuthorfunction']) ? $_REQUEST['boek_CoAuthorfunction'] : '';
$translatorSurname = !empty($_REQUEST['boek_translatorSurname']) ? $_REQUEST['boek_translatorSurname'] : '';
$translatorForename = !empty($_REQUEST['boek_translatorForename']) ? $_REQUEST['boek_translatorForename'] : '';
$translatorfunction = !empty($_REQUEST['boek_translatorfunction']) ? $_REQUEST['boek_translatorfunction'] : '';
$boeklang = !empty($_REQUEST['boek_boeklang']) ? $_REQUEST['boek_boeklang'] : '';
$firstedition =!empty($_REQUEST['boek_firstedition']) ? $_REQUEST['boek_firstedition'] : '';
$boekcat = !empty($_REQUEST['boek_boekcat']) ? $_REQUEST['boek_boekcat'] : '';
$boeksubcat = !empty($_REQUEST['boek_boeksubcat']) ? $_REQUEST['boek_boeksubcat'] : '';
$classification = !empty($_REQUEST['boek_classification']) ? $_REQUEST['boek_classification'] : '';
$specialtopic = !empty($_REQUEST['boek_specialtopic']) ? $_REQUEST['boek_specialtopic'] : '';
$synopsis = !empty($_REQUEST['boek_synopsis']) ? $_REQUEST['boek_synopsis'] : '';
$pictureURL = !empty($_REQUEST['boek_pictureURL']) ? $_REQUEST['boek_pictureURL'] : '';

$sql = "INSERT INTO " . $wpdb->prefix . "boekfly SET title = '" . $title . "' , subtitle = '" . $subtitle . "' , originaltitle = '" . $originaltitle . "' , reeks = '" . $reeks . "' , publisher = '" . $publisher . "' , volgnummer = '" . $volgnummer . "' , yearfirstedition = '" . $yearfirstedition . "' , yearthisedition = '" . $yearthisedition . "' , purchased = '" . $purchased . "' , AuthorSurname = '" . $AuthorSurname . "' , AuthorForename = '" . $AuthorForename . "' , Authorfunction = '" . $Authorfunction . "' , CoAuthorSurname = '" . $CoAuthorSurname . "' , CoAuthorForename = '" . $CoAuthorForename . "' , CoAuthorfunction = '" . $CoAuthorfunction . "' , translatorSurname = '" . $translatorSurname . "' , translatorForename = '" . $translatorForename . "' , translatorfunction = '" . $translatorfunction . "' , boeklang = '" . $boeklang . "' , firstedition = '" . $firstedition . "' , boekcat = '" . $boekcat . "' , boeksubcart= '" . $boeksubcat . "' , classification = '" . $classification . "' , synopsis = '" . $synopsis . "' , specialtopic = '" . $specialtopic . "' , pictureURL = '" . $pictureURL . "'";
$wpdb->get_results($sql);
$sql = "SELECT boekid FROM " . $wpdb->prefix . "boekfly WHERE title = '" . $title . "' and subtitle = '" . $subtitle . "' and originaltitle = '" . $originaltitle . "' and reeks = '" . $reeks . "' and publisher = '" . $publisher . "' and volgnummer = '" . $volgnummer . "' and yearfirstedition = '" . $yearfirstedition . "' and yearthisedition = '" . $yearthisedition . "' and purchased = '" . $purchased . "' and AuthorSurname = '" . $AuthorSurname . "' and AuthorForename = '" . $AuthorForename . "' and Authorfunction = '" . $Authorfunction . "' and CoAuthorSurname = '" . $CoAuthorSurname . "' and CoAuthorForename = '" . $CoAuthorForename . "' and CoAuthorfunction = '" . $CoAuthorfunction . "' and translatorSurname = '" . $translatorSurname . "' and translatorForename = '" . $translatorForename . "' and translatorfunction = '" . $translatorfunction . "' and boeklang = '" . $boeklang . "' and firstedition = '" . $firstedition . "' and boekcat = '" . $boekcat . "' and boeksubcart= '" . $boeksubcat . "' and classification = '" . $classification . "' and synopsis = '" . $synopsis . "' and specialtopic = '" . $specialtopic . "' and pictureURL = '" . $pictureURL . "'";
$check = $wpdb->get_results($sql);
if ( empty($check) || empty($check[0]->boekid) )
{
?><div class="error"><?php echo $boekid, $title," " ,$subtitle, " ",$originaltitle, " ",$boeklang;?><p><strong>Failure:</strong> Holy crap you destroyed the internet! That, or something else went wrong when I tried to insert the entry. Try again? </p></div><?php
}


According to both the error catch and phpmyadmin nothing is being written to the database. boekid is supposed to be an autonumber index.

Any ideas - title, subtitle, boeklang etc are indicating the correct values being passed from the form.

Matthew1980

6:41 pm on Aug 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Stuartindigo,

$sql = "INSERT INTO " . $wpdb->prefix . "boekfly SET title = '" .

Ok, I think this may have something to do with it, Insert's *I think* (I hope ;)) are structured as below:- ([dev.mysql.com ])

INSERT INTO `tablename` (`column1`, `column2`, `colum3`) VALUES ('value1', 'value2', 'value3');

So your query is actually invalid syntax I think, have you actually tried it from within phpmyadmin to see if it works statically?

Once you have the syntax correct, try echoing it to screen to see if it's fully populated, then go from there :)

Oh, and please if these are $_POST values your inserting, dont use $_REQUEST, well at least not for release scripts, because of their inherent security vulnerabilities, this has been well documented. Also, using these 'user generated' values - sanitise them first, this will make your database more secure against malicious code injections (strip_tags() & mysql_real_escape_string())

Hope that helps..

Cheers,
MRb

stuartindigo

4:41 pm on Aug 10, 2010 (gmt 0)

10+ Year Member



I redid the code to the recommended syntax, this cleared the insert problem.

I then had exactly the same problem with the update function - added in an extra mysql error catch (after banging my head against the desk for hours) and found that most annoying of little errors - a misspelt variable...

here : boeksubcart= '" . $boeksubcat . "

so easy to do, so difficult to find!

Matthew1980

5:55 pm on Aug 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Sturtindigo,

Does this mean your all working now :) Great if you are!

Yep, miss spellings are the most common thing when your using php in conjunction with MySql, but at least you have found it - this is why I always echo the string to screen, then copy that and pop it into something like Mysql query browser or PhpMyadmin (whichever is your preference) and see if that returns the data I ask for, if not I debug from there, this has proven time and time again to be the most effective method for debugging sql syntax imho.

>>added in an extra mysql error catch
If that is: or die(mysql_error()); on the end of a query function, remember to remove it when you have worked all of the kinks in your software so that you don't release sensitive information about your database structure because to a well versed hacker, this is all they require to do damage to your valuable info.

>>so easy to do, so difficult to find!
Welcome to programming :) I'm learning VisualBasic at present, and know exactly how you feel ;-p

Anyway, have fun with the rest of your project.

Cheers,
MRb