Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 3rd, 2003, 10:22 AM
Authorized User
 
Join Date: Jul 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cannot store multiple form selections to a mysql d

I have a form document (below) and would like to store that data in a mysql database.
Form.php:
<html>
<head>
<title>Form.php</title>
</head>
<body>
<?
echo ”
<form method=\"post\" action=\"handleform.php \">
<table border=\"1\">
<tr><td>Doctor: </td><td>
<select name = \"doctornames\">
<option value=\"blank\" selected=\"selected\"></option>
<option value=\"McEnroe\">Dr John McEnroe</option>
<option value=\"Becker\">Dr Boris Becker</option>
<option value=\"Borg\">Dr Bjorn Borg</option>
<option value=\"Navratilova\">Dr Martina Navratilova</option>
<option value=\"Williams\">Dr Serena Williams</option>
</select></tr>
</table>
";
echo "
<p><input type=\"submit\" name=\"mybutton\" value=\"Submit\" />
<input type=\"reset\" value=\"Reset Form\" />
</p>
</form>
";
?>
</body>
</html>


I am using PHP 4.0 that’s why I am using $HTTP_POST_VARS instead of _POST
handleform.php :

<body> <?php
global $HTTP_POST_VARS; global $doctor;
for ($x=0; $x < count($HTTP_POST_VARS['doctornames']); $x++) {
$doctor = $HTTP_POST_VARS['doctornames[$x]'];

if (isset($HTTP_POST_VARS['mybutton'])) {
echo "1.You clicked the button\n";
echo "2.You typed ". $doctor . " in the textbox.<br>\n";
$Host = "***";
$DBName = "***";
$TableName = "***";
$Link = mysql_connect ($Host, '****', '****'); if ($Link==false) { echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_select_db ($DBName, $Link);
$Query = "INSERT INTO $TableName VALUES ('{$doctor}')";

if (mysql_query ($Query, $Link)) {
print "The query was successfully executed!<BR>\n";
}
else {
print "The query could not be executed!<BR>\n";
echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_close($Link);
}
else {
echo "you must have come here from somewhere else.\n";
}
?>
</body>
 I also used switch()
switch ($HTTP_POST_VARS['doctornames']) {
case "blank":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "McEnroe":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "Becker":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "Borg":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "Navratilova":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "Williams":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
}

instead of lines
for ($x=0; $x < count($HTTP_POST_VARS['doctornames']); $x++) {
$doctor = $HTTP_POST_VARS['doctornames[$x]'];
but it is not storing the data. What is the problem? Could u help me?



 
Old August 3rd, 2003, 12:45 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Well,
Unless you are needing multiple selections from the select field, it is uneccessary to loop it. Use the loop if you are using this attribute:

<select name = \"doctornames\" multiple=\"multiple\" size=\"4\">

Doing it this way would allow you to toggle multiple selections, thereby making it neccessary to have a loop, which you also had some errors in:


Code:
foreach ($HTTP_POST_VARS['doctornames'] as $key => $value) {

    $doctor = $HTTP_POST_VARS['doctornames'][$key];

} // snip snip
The counter variable goes in its own array, and use use the $key, $value to keep in line with the selections made by the user.

Code:
<?php
global $HTTP_POST_VARS; global $doctor;


Host = "***";
$DBName = "***";
$TableName = "***";
$Link = mysql_connect ($Host, '****', '****'); 

/*
Making a database connection is resource intensive, you should do this only once at very beginning of a script, and definitely not within a loop
*/

if ($Link==false) { 

    echo mysql_errno().": ".mysql_error()."<BR>\n";

}

mysql_select_db ($DBName, $Link);

    foreach ($HTTP_POST_VARS['doctornames'] as $key => $value) {

        # I don't understand the point of this:
        # $doctor = $HTTP_POST_VARS['doctornames[$x]'];

        if (isset($HTTP_POST_VARS['mybutton'])) {

            echo "1.You clicked the button\n";
            echo "2.You selected ". $HTTP_POST_VARS['doctornames'][$key] . " from the select field.<br>\n";

            $Query = "INSERT INTO $TableName VALUES ('{$HTTP_POST_VARS['doctornames'][$key]}')";

            if (mysql_query ($Query, $Link)) {

                print "The query was successfully executed!<BR>\n";

            } else {

                print "The query could not be executed!<BR>\n";
                echo mysql_errno().": ".mysql_error()."<BR>\n";

            }

                mysql_close($Link);
        } else {

            echo "you must have come here from somewhere else.\n";

        }

?>
If you did in fact want multiple <select> choices that's how it would be done. You were trying to assign the $HTTP_POST_VARS value to a regular global, which is fine, but you didn't treat the $doctors variable as an array. And not to mention were trying to insert the counter in the string.

For a regular <select> field, without the multiple="multiple" attribute it would be exactly the same as accessing data from a text field. Just a single name, value pair - no sub array.

hth,
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 3rd, 2003, 04:16 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I noticed some more errors which I did not notice before:

The call to:
mysql_close($Link);
Should be done after the loop is closed.

Otherwise you're closing the database connection after the first selection is inserted. Which will cause some errors.

I also left out the closing '}' on the foreach loop.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
how many items can mysql store? yulin11 MySQL 1 December 6th, 2007 11:58 AM
Dropdown list with multiple selections tombert ASP.NET 1.0 and 1.1 Basics 15 March 7th, 2007 07:38 PM
C# ListBox Working With Multiple Selections pro-logic C# 1 October 9th, 2005 02:15 AM
multiple listbox selections written to a text fiel pascoag Access 0 October 14th, 2004 10:01 PM
Select Menu Multiple Selections phungleon HTML Code Clinic 2 July 20th, 2004 12:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.