Jump to content

Archived

This topic is now archived and is closed to further replies.

puja

adding problem

Recommended Posts

hey
im 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>
&nbsp;
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> &nbsp;

hope someone can help

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
The post method also should require a <form tag!

<form name="form1" method="post" action="submitform.php">
select stuff
</form>

Share this post


Link to post
Share on other sites
thanks guys

my 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?

Share this post


Link to post
Share on other sites
Type:
<select name="select_accom">
<option value='Villa'>Villa</option>
<option value='Caravan'>Caravan</option>
<option value='Lodge'>Lodge</option>
</select>
&nbsp;
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> &nbsp;



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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 chosen
thanks 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>

<?php

require_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">&nbsp;
<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>&nbsp;

<H1> Accommodation </H1>

</td>
</tr>

<td width="22%" height="123" bgcolor="#6699FF">&nbsp;
<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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</td>
<td width="78%" height="123" bgcolor="#99CCFF" ALIGN=CENTER>
&nbsp;
&nbsp;
&nbsp;
<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"> &nbsp;
Type:
<select name="select_type">
<option value='Villa'>Villa</option>
<option value='Caravan'>Caravan</option>
<option value='Lodge'>Lodge</option>
</select>
&nbsp;
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> &nbsp; <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> &nbsp;
Pets Allowed:
<select name="select_pets">
<option value='Y'>Yes</option>
<option value='N'>No</option>
</select> &nbsp;
<br> <br>
Description:<br>
<textarea style ="width: 75%" rows="10" name="description" cols="20"></textarea> <br> <br>&nbsp;
<center> <input type="submit" value="Add"></center>
</font>


<p>&nbsp;</p>
<p>&nbsp;</p>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</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>

Share this post


Link to post
Share on other sites
what currently happens when you submit the form? does it update the database?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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>

<?php

require_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">&nbsp;
<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>&nbsp;

<H1> Accommodation </H1>

</td>
</tr>

<td width="22%" height="123" bgcolor="#6699FF">&nbsp;
<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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</td>
<td width="78%" height="123" bgcolor="#99CCFF" ALIGN=CENTER>
&nbsp;
&nbsp;
&nbsp;
<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"> &nbsp;
Type:
<select name="select_type">
<option value='Villa'>Villa</option>
<option value='Caravan'>Caravan</option>
<option value='Lodge'>Lodge</option>
</select>
&nbsp;
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> &nbsp; <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> &nbsp;
Pets Allowed:
<select name="select_pets">
<option value='Y'>Yes</option>
<option value='N'>No</option>
</select> &nbsp;
<br> <br>
Description:<br>
<textarea style ="width: 75%" rows="10" name="description" cols="20"></textarea> <br> <br>&nbsp;
<center> <input type="submit" value="Add"></center>
</font>


<p>&nbsp;</p>
<p>&nbsp;</p>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</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>

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
lol thanks!
i liked ur example!
its working now
thanks again

puja

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.