Jump to content


Photo

adding problem


  • Please log in to reply
13 replies to this topic

#1 puja

puja
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 April 2006 - 11:49 AM

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


#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 11:55 AM

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

wisewood: proven fact, I am both wise, and wooden.

#3 dedart

dedart
  • Members
  • PipPip
  • Member
  • 10 posts
  • LocationNewcastle Australia

Posted 24 April 2006 - 11:59 AM

The post method also should require a <form tag!

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

#4 puja

puja
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 April 2006 - 12:08 PM

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?


#5 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 12:11 PM

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.
wisewood: proven fact, I am both wise, and wooden.

#6 puja

puja
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 April 2006 - 02:17 PM

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

#7 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 02:19 PM

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.
wisewood: proven fact, I am both wise, and wooden.

#8 puja

puja
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 April 2006 - 02:25 PM

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>

#9 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 02:31 PM

what currently happens when you submit the form? does it update the database?
wisewood: proven fact, I am both wise, and wooden.

#10 puja

puja
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 April 2006 - 02:37 PM

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

#11 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 02:42 PM

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>
wisewood: proven fact, I am both wise, and wooden.

#12 puja

puja
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 April 2006 - 03:10 PM

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

#13 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 03:17 PM

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]

$accommID = $_POST["accommID"];
$type = $_POST["select_type"];
$sleeps = $_POST["select_sleeps"];
$board = $_POST["select_board"];
$description = $_POST["description"];
$pets_allowed = $_POST["select_pets"];

wisewood: proven fact, I am both wise, and wooden.

#14 puja

puja
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 April 2006 - 04:21 PM

lol thanks!
i liked ur example!
its working now
thanks again

puja




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users