Ok,
I create 3 files and 1 database with 1 table.
select3.php this is the form
select4.php this is the function for sending to the database
display.php this is to display result from database
MySQL Database structure:
Table structure for table `multiple`
CREATE TABLE `multiple` (
`id1` int(4) NOT NULL auto_increment,
`id` varchar(4) default '1',
`test` varchar(255) default '2',
PRIMARY KEY (`id1`)
) TYPE=MyISAM AUTO_INCREMENT=45 ;
select3.php
<html>
<head>
<title>Select Multiple Box</title>
</head>
<body>
<table width="300" cellpadding="5" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="left" colspan="1" rowspan="1" bgcolor="#FFFFFF">
<h3>Insert Record <a href="display.php">Display result </a></h3>
<form method="POST" action="select4.php">
<!--The hidden fields are provided to maintain state. They are used to pass the username and password from script to script.-->
<input type="hidden" name="username" value="<? print $_POST [ 'id' ] ?> ">
<?php
print "Enter name: <input type=text name=id size=20><br>\n" ;
print "Select Items: <select name=test[] size=5 multiple >
<option value=item1 >item1</option>
<option value=item2 >item2</option>
<option value=item3 >item3</option>
<option value=item4 >item4</option>
<option value=item5 >item5</option>
<option value=item6 >item6</option>
<option value=item7 >item7</option>
</select><br>\n" ;
print "<br>\n" ;
print "(Shif+Right Mouse) for multiple selection<br>\n" ;
print "<br>\n" ;
print "<input type=submit value=Submit><input type=reset>\n" ;
?>
</form></td></tr></table>
</body>
</html>
select4.php
<html>
<head>
<title>Birthdays Insert Record</title>
</head>
<body>
<?php
$id = $_POST [ 'id' ];
$test = $_POST [ 'test' ];
$db = "select" ;
$link = mysql_connect ( "localhost" , "root" , "" );
//$link = mysql_connect("localhost",$_POST['username'],$_POST['password']);
if (! $link )
die( "Couldn't connect to MySQL" );
mysql_select_db ( $db , $link ) or die( "Select Error: " . mysql_error ());
//for($i=0;$i<sizeof($_POST["test"]);$i++)
//{
//$sql = "insert into tbl_name values ($_POST["test"][$i])"; }
//sql = "INSERT INTO table_name VALUES ('" . join(",",$_POST["test"]) . "')";
$result = mysql_query ( "INSERT INTO multiple (id, test) VALUES ('$id','" . join ( "," , $_POST [ "test" ]) . "')" )or die( "Insert Error: " . mysql_error ());
mysql_close ( $link );
print "Record added\n" ;
?>
<a href="display.php">Display Records</a>
<form method="POST" action="select3.php">
<input type="submit" value="Insert Another Record">
</form>
</body>
</html>
display.php
<html>
<head>
<title>(Title Here)</title>
</head>
<body>
<?php
$db = "select" ;
$link = mysql_connect ( "localhost" , "root" , "" );
if (! $link )
die( "Couldn't connect to MySQL" );
mysql_select_db ( $db , $link )
or die( "Couldn't open $db: " . mysql_error ());
$result = mysql_query ( "SELECT * FROM multiple" )
or die( "SELECT Error: " . mysql_error ());
$num_rows = mysql_num_rows ( $result );
print "There are $num_rows records.<P>" ;
print "<table width=200 border=1>\n" ;
while ( $get_info = mysql_fetch_row ( $result )){
print "<tr>\n" ;
foreach ( $get_info as $field )
print "\t<td><font face=arial size=1/>$field</font></td>\n" ;
print "</tr>\n" ;
}
print "</table>\n" ;
mysql_close ( $link );
?>
<a href="select3.php">Back</a>
</body>
</html>
Newbie Notes #1 - Making a form return to itself Categories : PHP , Beginner Guides , HTML and PHP mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP , Beginner Guides , MySQL , HTML and PHP , Databases Form Submission Using Array's Categories : PHP , HTML and PHP , Beginner Guides , Arrays Nested repeat region Categories : PHP , HTML and PHP , Beginner Guides Simple PHP Form Field Generator Categories : PHP , Beginner Guides , Form Processing , HTML and PHP How to Create a Shoutbox Using PHP & MySQL Categories : PHP , MySQL , Web Applications , Beginner Guides , HTML and PHP Newbie Notes #9 - Hyperlinking a post Categories : PHP , Java Script , HTML and PHP , Beginner Guides Kewl Date Example Categories : PHP , HTML and PHP , Date Time , CSS , Beginner Guides PHP based HTML rabbing Tools Categories : PHP , HTML and PHP , Tag Extractors , Regexps , Beginner Guides Using PHP im HTML image tags Categories : PHP , HTML and PHP , Graphics , Beginner Guides How to preset a text string in a textarea input field Categories : HTML , HTML and PHP , PHP , Beginner Guides Real simple example of removing HTML tags from text then changing \n (new line) to <br>. Could be used in a forum for instance. Categories : HTML , PHP , HTML and PHP , Beginner Guides PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use! Categories : HTML and PHP , Email , PHP , Perl , HTML and PHP Message of the Day - Random Message (Needs MySQL!) Categories : Databases , HTML and PHP , PHP , MySQL a function that builds an HTML select list from any mysql table. Categories : PHP , MySQL , HTML and PHP
Aadi Dugar wrote : 1650
Thanks guys, I was missing the brackets \[\] in my multiple option select field. Cheers for that.
buggeroff none wrote : 1797
what kind of a code is this? I ve copied it and pasted it
and it doesn't work! what is it? you dont even bother to
check if it works before launching it publicly?
this is the thanks I got for wasting my time here:
Parse error: syntax error, unexpected '"', expecting
T_STRING or T_VARIABLE or T_NUM_STRING in
C:\xampp\htdocs\select4.php on line 17