puja Posted April 24, 2006 Share Posted April 24, 2006 heyim trying to add selection from a php form into a mysql database but it i am not sure how to.ive got drop down lists to make the selection from and ive given them values and i know i need to use the POST method but i just cant seem to get it to work.my options look like this:Type: <select name="select"> <option value='Villa'>Villa</option> <option value='Caravan'>Caravan</option> <option value='Lodge'>Lodge</option> </select> Board: <select name="select"> <option value='Full Board'>Full Board</option> <option value='Half Board'>Half Board</option> <option value='B & B'>B & B</option> <option value='Self-Catering'>Self-Catering</option> </select> hope someone can help Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/ Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 your select names must be different.<SELECT NAME="accom_type"><option value="caravan"><option value="villa"></select><SELECT NAME="accom_board"><option value="half board"><option value="full board"></select>hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30055 Share on other sites More sharing options...
dedart Posted April 24, 2006 Share Posted April 24, 2006 The post method also should require a <form tag!<form name="form1" method="post" action="submitform.php">select stuff</form> Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30057 Share on other sites More sharing options...
puja Posted April 24, 2006 Author Share Posted April 24, 2006 thanks guysmy form tag looks like: <form name="add_accomm.php" action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST"> <font face="arial" size ="1">is there anything wrong with that? Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30060 Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 Type:<select name="select_accom"><option value='Villa'>Villa</option><option value='Caravan'>Caravan</option><option value='Lodge'>Lodge</option></select> Board:<select name="select_board"><option value='Full Board'>Full Board</option><option value='Half Board'>Half Board</option><option value='B & B'>B & B</option><option value='Self-Catering'>Self-Catering</option></select> using this will ensure your options are passed properly... otherwise the board would have over-written the type. If you want your code fixed properly, you'll have to give us the whole code. Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30062 Share on other sites More sharing options...
puja Posted April 24, 2006 Author Share Posted April 24, 2006 ok i'll put my whole code but how do i do that? if i just put it straight on it'll b quite long Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30102 Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 just put it straight on... copy/paste.It might be long, but at least we'll see everything relating to what you have a problem with. Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30103 Share on other sites More sharing options...
puja Posted April 24, 2006 Author Share Posted April 24, 2006 ok so at the moment i just have a normal add implemented as if a user was entering the values themselves.i need it to insert them according to the values chosenthanks 4 the help<html><head><title><?php echo "Accommodation" ?></title><link rel="stylesheet" type="text/css" href="template2.css"><script language="JavaScript" src="gen_validatorv2.js" type="text/javascript"></script></head><body><?phprequire_once("config.php"); $connection = @mysql_connect($db_host, $db_user, $db_password) or die("oops! error connecting"); mysql_select_db($db_name, $connection); $self = $_SERVER['PHP_SELF']; $accommID = $_POST ["accommID"]; $len = strlen($accommID); if ($len > 0) { $accommID = $_POST["accommID"]; $type = $_POST["type"]; $sleeps = $_POST["sleeps"]; $board = $_POST["board"]; $description = $_POST["description"]; $pets_allowed = $_POST["pets_allowed"]; $query = "INSERT INTO accommodation (accommID, type, sleeps, board, description, pets_allowed) VALUES ('$accommID', '$type', '$sleeps', '$board', '$description', '$pets_allowed')"; mysql_query($query, $connection) or die (mysql_error()); }?><table border="1" width="100%" height="226"> <tr> <td width="22%" height="91" bgcolor="#0099FF"> <img alt ="[Company Logo]" src="file:///C:/Documents%20and%20Settings/Puja/My%20Documents/My%20Pictures/puj2.gif" width="100" height="100"> <p> </td> <td width="78%" height="91" bgcolor="#66CCFF" ALIGN=CENTER> <H1> Accommodation </H1> </td> </tr> <td width="22%" height="123" bgcolor="#6699FF"> <div class="buttonscontainer"> <div class="buttons"> <a href="manage_accomm.php">Manage Accommodation</a> <a href="ownerhomepage.php">Owner Homepage</a> <a href="logout.php">Logout</a> </div> </div> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </td> <td width="78%" height="123" bgcolor="#99CCFF" ALIGN=CENTER> <center> <form name="add_accomm.php" action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST"> <font face="arial" size ="1"> Accommodation ID: <input type="text" name ="accommID"> Type: <select name="select_type"> <option value='Villa'>Villa</option> <option value='Caravan'>Caravan</option> <option value='Lodge'>Lodge</option> </select> Sleeps: <select name="select_sleeps"> <option value='2'>Up to 2</option> <option value='4'>Up to 4</option> <option value='6'>Up to 6</option> <option value='8'>Up to 8</option> <option value='10'>Up to 10</option> </select> <br> <br> Board: <select name="select_board"> <option value='Full Board'>Full Board</option> <option value='Half Board'>Half Board</option> <option value='B & B'>B & B</option> <option value='Self-Catering'>Self-Catering</option> </select> Pets Allowed: <select name="select_pets"> <option value='Y'>Yes</option> <option value='N'>No</option> </select> <br> <br> Description:<br> <textarea style ="width: 75%" rows="10" name="description" cols="20"></textarea> <br> <br> <center> <input type="submit" value="Add"></center> </font> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </form> </center></td> <script language="JavaScript" type="text/javascript"> var frmvalidator = new Validator("add_accomm.php"); frmvalidator.addValidation("description","req","Please enter a Description"); </script></table> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30109 Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 what currently happens when you submit the form? does it update the database? Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30111 Share on other sites More sharing options...
puja Posted April 24, 2006 Author Share Posted April 24, 2006 it adds in the accomm id and description because they are just normal values neway, then the type, board and pets allowed just stay blank because it doesnt realise that the user has selected and option and the sleeps inserts 0 because that is the default Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30113 Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 TRY THIS:<html><head><title><?php echo "Accommodation" ?></title><link rel="stylesheet" type="text/css" href="template2.css"><script language="JavaScript" src="gen_validatorv2.js" type="text/javascript"></script></head><body><?phprequire_once("config.php");$connection = @mysql_connect($db_host, $db_user, $db_password) or die("oops! error connecting");mysql_select_db($db_name, $connection);$self = $_SERVER['PHP_SELF'];$accommID = $_POST ["accommID"];$len = strlen($accommID);if ($len > 0){$accommID = $_POST["accommID"];$type = $_POST["select_type"];$sleeps = $_POST["select_sleeps"];$board = $_POST["select_board"];$description = $_POST["description"];$pets_allowed = $_POST["pets_allowed"];$query = "INSERT INTO accommodation (accommID, type, sleeps, board, description, pets_allowed) VALUES ('$accommID', '$type', '$sleeps', '$board', '$description', '$pets_allowed')";mysql_query($query, $connection) or die (mysql_error());}?><table border="1" width="100%" height="226"><tr><td width="22%" height="91" bgcolor="#0099FF"> <img alt ="[Company Logo]"src="file:///C:/Documents%20and%20Settings/Puja/My%20Documents/My%20Pictures/puj2.gif" width="100" height="100"><p></td><td width="78%" height="91" bgcolor="#66CCFF" ALIGN=CENTER> <H1> Accommodation </H1></td></tr><td width="22%" height="123" bgcolor="#6699FF"> <div class="buttonscontainer"><div class="buttons"><a href="manage_accomm.php">Manage Accommodation</a><a href="ownerhomepage.php">Owner Homepage</a><a href="logout.php">Logout</a></div></div><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p></td><td width="78%" height="123" bgcolor="#99CCFF" ALIGN=CENTER> <center><form name="add_accomm.php" action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST"><font face="arial" size ="1">Accommodation ID: <input type="text" name ="accommID"> Type:<select name="select_type"><option value='Villa'>Villa</option><option value='Caravan'>Caravan</option><option value='Lodge'>Lodge</option></select> Sleeps:<select name="select_sleeps"><option value='2'>Up to 2</option><option value='4'>Up to 4</option><option value='6'>Up to 6</option><option value='8'>Up to 8</option><option value='10'>Up to 10</option></select> <br> <br>Board:<select name="select_board"><option value='Full Board'>Full Board</option><option value='Half Board'>Half Board</option><option value='B & B'>B & B</option><option value='Self-Catering'>Self-Catering</option></select> Pets Allowed: <select name="select_pets"><option value='Y'>Yes</option><option value='N'>No</option></select> <br> <br>Description:<br><textarea style ="width: 75%" rows="10" name="description" cols="20"></textarea> <br> <br> <center> <input type="submit" value="Add"></center></font><p> </p><p> </p><p> </p><p> </p><p> </p></form></center></td><script language="JavaScript" type="text/javascript">var frmvalidator = new Validator("add_accomm.php");frmvalidator.addValidation("description","req","Please enter a Description");</script></table></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30116 Share on other sites More sharing options...
puja Posted April 24, 2006 Author Share Posted April 24, 2006 hey!thank you! im not sure wot u changed but it works for all the fields except for pets_allowed.not sure y that one dont work Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30131 Share on other sites More sharing options...
wisewood Posted April 24, 2006 Share Posted April 24, 2006 Your $_POST[variable_name] variables should have the same variable name as the name of the form field they're being sent from... so if you had;<SELECT NAME="cheesey_cheesey_cheddar"><OPTION VALUE="Cheese"><OPTION VALUE="Cheddar"></OPTION>Your $_POST variable would be $_POST[cheesey_cheesey_cheddar][code]$accommID = $_POST["accommID"];$type = $_POST["select_type"];$sleeps = $_POST["select_sleeps"];$board = $_POST["select_board"];$description = $_POST["description"];$pets_allowed = $_POST["select_pets"];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30137 Share on other sites More sharing options...
puja Posted April 24, 2006 Author Share Posted April 24, 2006 lol thanks!i liked ur example!its working nowthanks againpuja Quote Link to comment https://forums.phpfreaks.com/topic/8247-adding-problem/#findComment-30159 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.