Ok so I have a drop down menu in a form…
[code=php]<td class=”required”>
<select name=”est”>
<option value=”select”>- Select One -</option>
<option value=”bars”>Bar</option>
<option value=”clubs”>Club</option>
<option value=”coffee_shops”>Coffee Shop</option>
<option value=”resturants”>Resturant</option>
</select>
</td>
…and I have a db set up with tables called “bars”, “clubs”, “coffee_shops”, and “resturants”. How do I set up an sql insert statement to insert the data in the form into the corrisponding table.
For example the form gets filled out and the person selects the “Bar” option from the drop down and all the information in the form will be inserted into the “bars” table of the db.
Any ideas?
Thanks alot for the help
[code=php]
$query = "INSERT INTO ".$_GET['est']." VALUES('".$_GET['field1']."', '".$_GET['field2']."');";
[/code]
[code=php]$query = "INSERT INTO ".$_GET['est']." (input1, input2, input3, input4, input5, input6)
VALUES('".$_GET['input1']."', '".$_GET['input2']."', '".$_GET['input3']."', '".$_GET['input4']."',
'".$_GET['input5']."', '".$_GET['input6']."')";[/code]
[code=php]<div class = "nround">
<div class = "ntop"><h2>Become a hotspot</h2></div>
<div class = "nmiddle" align="left">
<form name="step1" method="post" action="basic2.php">
<?php include ('db_connect.php');
$query = "INSERT INTO ".$_GET['est']." (comp_name, comp_address, comp_phone, comp_email, comp_web, comp_desc)
VALUES('".$_GET['comp_name']."', '".$_GET['comp_address']."', '".$_GET['comp_phone']."', '".$_GET['comp_email']."',
'".$_GET['comp_web']."', '".$_GET['comp_desc']."')";
//if ($_REQUEST['bars'])
//sql = "INSERT INTO bar (comp_name, comp_address, comp_phone, comp_email, comp_web, comp_desc)
//VALUES ('$_POST[comp_name]','$_POST[comp_address]''$_POST[comp_phone]','$_POST[comp_email]','$_POST[comp_web]',
//'$_POST[comp_desc]')";
//if (!mysql_query($sql,$con))
// {
// die('Error: ' . mysql_error());
// }
// echo "Thank you for your submission.";
?>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tbody><tr>
<td colspan="2"><b>Please enter a valid e-mail address. You will need to confirm your e-mail address to activate your account.</b>
</td>
</tr>
<tr>
<td class="required">Company Name</td>
<td class="required"><input type="text" name="comp_name" size="20" maxlength="15"
value="<?php if (isset($_POST['comp_name'])) echo $_POST['comp_name']; ?>" /></td>
</tr>
<tr>
<td class="required">Establishment Type</td>
<td class="required">
<select name="est">
<option value="select">- Select One -</option>
<option value="bars">Bar</option>
<option value="clubs">Club</option>
<option value="coffee_shops">Coffee Shop</option>
<option value="resturants">Resturant</option>
</select>
</td>
</tr>
<tr>
<td class="required">Address</td>
<td class="required"><input type="text" name="comp_address" size="30" maxlength="30"
value="<?php if (isset($_POST['comp_address'])) echo $_POST['comp_address']; ?>" /></td>
</tr>
<tr>
<td class="required">Phone Number</td>
<td><input type="text" name="comp_phone" size="20" maxlength="40"
value="<?php if (isset($_POST['comp_phone'])) echo $_POST['comp_phone']; ?>" /></td>
<?php
if (!preg_match('/^[0-9 ]+$/' , $_POST['comp_phone'])){
$error_msg['comp_phone'] = '<div class=message>Invalid character in phone number.';
}
?>
</tr>
<tr>
<td class="required">Email Address</td>
<td><input type="text" name="comp_email" size="30" maxlength="20"
value="<?php if (isset($_POST['comp_email'])) echo $_POST['comp_email']; ?>" /></td>
</tr>
<tr>
<td class="required">Website Address</td>
<td><input type="text" name="comp_web" size="30" maxlength="20"
value="<?php if (isset($_POST['comp_web'])) echo $_POST['comp_web']; ?>" /></td>
</tr>
<tr>
<td class="required">Description</td>
<td><textarea name="comp_desc" cols="30" rows="12"
value="<?php if (isset($_POST['comp_desc'])) echo $_POST['comp_desc']; ?>" ></textarea></td>
</tr>
<tr>
<td class="verdana11" align="right" width="125"><p></td>
<td><input onClick="basic2.php" type="submit" name="submit" value="Go to step 2" /></td>
</tr>
</tbody>
</table>
</form>
</div>
<div class = "nbottom"></div>
</div>[/code]
;";[/QUOTE]I thought it was a typo just because I have only seen "; after sql statements. Ill add it back in since its not a typo, my bad ?
$query = "INSERT INTO ".$_GET['est']."[/QUOTE]is working correctly or not. When i hit submit the page changes to load the "step2" form but nothing that I entered into the "step1" form gets inserted into the db.
<i>
</i>$query = "INSERT INTO comp (comp_name, comp_address, comp_phone, comp_email, comp_web, comp_desc, comp_type)
VALUES('".$_POST['comp_name']."', '".$_POST['comp_address']."', '".$_POST['comp_phone']."', '".$_POST['comp_email']."',
'".$_POST['comp_web']."', '".$_POST['comp_desc']."', '".$_POST['comp_type']."')";
[code=php]<?php
//This sets the database access information as constants
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_NAME','hotspot');
define ('DB_HOST', 'localhost');
//Establish connection to the databse and some error handeling
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)
or die ('Could not connect to MySQL: ' .mysql_error());
mysql_select_db(DB_NAME) OR die('Could not select the database: ' .mysql_error() );
$query = "INSERT INTO establishment (comp_name, comp_address, comp_phone, comp_email, comp_web, comp_desc, comp_type)
VALUES('".$_POST['comp_name']."', '".$_POST['comp_address']."', '".$_POST['comp_phone']."', '".$_POST['comp_email']."',
'".$_POST['comp_web']."', '".$_POST['comp_desc']."', '".$_POST['comp_type']."')";
?>[/code]
[code=php]<div class = "nround">
<div class = "ntop"><h2>Become a hotspot</h2></div>
<div class = "nmiddle" align="left">
<form name="step1" method="post" action="basic2.php">
<?php
//This sets the database access information as constants
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_NAME','hotspot');
define ('DB_HOST', 'localhost');
//Establish connection to the databse and some error handeling
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)
or die ('Could not connect to MySQL: ' .mysql_error());
mysql_select_db(DB_NAME) OR die('Could not select the database: ' .mysql_error() );
$query = "INSERT INTO establishment (comp_name, comp_address, comp_phone, comp_email, comp_web, comp_desc, comp_type)
VALUES('".$_POST['comp_name']."', '".$_POST['comp_address']."', '".$_POST['comp_phone']."', '".$_POST['comp_email']."',
'".$_POST['comp_web']."', '".$_POST['comp_desc']."', '".$_POST['comp_type']."')";
?>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tbody><tr>
<td colspan="2"><b>Please enter a valid e-mail address. You will need to confirm your e-mail address to activate your account.</b>
</td>
</tr>
<tr>
<td class="required">Company Name</td>
<td class="required"><input type="text" name="comp_name" size="20" maxlength="15"
value="<?php if (isset($_POST['comp_name'])) echo $_POST['comp_name']; ?>" /></td>
</tr>
<tr>
<td class="required">Establishment Type</td>
<td class="required">
<select name="est">
<option value="select">- Select One -</option>
<option value="bars">Bar</option>
<option value="clubs">Club</option>
<option value="coffee_shops">Coffee Shop</option>
<option value="resturants">Resturant</option>
</select>
</td>
</tr>
<tr>
<td class="required">Address</td>
<td class="required"><input type="text" name="comp_address" size="30" maxlength="30"
value="<?php if (isset($_POST['comp_address'])) echo $_POST['comp_address']; ?>" /></td>
</tr>
<tr>
<td class="required">Phone Number</td>
<td><input type="text" name="comp_phone" size="20" maxlength="40"
value="<?php if (isset($_POST['comp_phone'])) echo $_POST['comp_phone']; ?>" /></td>
<?php
if (!preg_match('/^[0-9 ]+$/' , $_POST['comp_phone'])){
$error_msg['comp_phone'] = '<div class=message>Invalid character in phone number.';
}
?>
</tr>
<tr>
<td class="required">Email Address</td>
<td><input type="text" name="comp_email" size="30" maxlength="20"
value="<?php if (isset($_POST['comp_email'])) echo $_POST['comp_email']; ?>" /></td>
</tr>
<tr>
<td class="required">Website Address</td>
<td><input type="text" name="comp_web" size="30" maxlength="20"
value="<?php if (isset($_POST['comp_web'])) echo $_POST['comp_web']; ?>" /></td>
</tr>
<tr>
<td class="required">Description</td>
<td><textarea name="comp_desc" cols="30" rows="12"
value="<?php if (isset($_POST['comp_desc'])) echo $_POST['comp_desc']; ?>" ></textarea></td>
</tr>
<tr>
<td class="verdana11" align="right" width="125"><p></td>
<td><input onClick="basic2.php" type="submit" name="submit" value="Go to step 2" /></td>
</tr>
</tbody>
</table>
</form>
</div>
<div class = "nbottom"></div>
</div>[/code]
[code=php]
$result = mysql_query($query);
[/code]
[code=php]<?php
$con = mysql_connect("localhost","root",""); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hotspot", $con); //Replace with your MySQL DB Name
$comp_name=mysql_real_escape_string($_POST['comp_name']);
$comp_address=mysql_real_escape_string($_POST["comp_address"]);
$comp_phone=mysql_real_escape_string($_POST["comp_phone"]);
$comp_email=mysql_real_escape_string($_POST['comp_email']);
$comp_web=mysql_real_escape_string($_POST["comp_web"]);
$comp_desc=mysql_real_escape_string($_POST["comp_desc"]);
$sql="INSERT INTO establishment (comp_name, comp_address, comp_phone, comp_email, comp_web, comp_desc, comp_type) VALUES ('$comp_name','$comp_address','$comp_phone','$comp_email','$comp_web','$comp_desc')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>[/code]
Error: Column count doesn't match value count at row 1[/QUOTE]
[code=php]<?php
$username="root";
$password="";
$database="hotspot";
$con = mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$sql="INSERT INTO establishment (type, comp_name, comp_address, comp_phone, comp_desc, comp_email, comp_web)
VALUES
('$_POST[type]','$_POST[comp_name]','$_POST[comp_address]','$_POST[comp_phone]','$_POST[comp_desc]',
'$_POST[comp_email]','$_POST[comp_web]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close();
?>[/code]
[code=php]<select name="comp_type">
<option selected>- Select One -</option>
<option value="bars">Bar</option>
<option value="clubs">Club</option>
<option value="coffee_shops">Coffee Shop</option>
<option value="resturants">Resturant</option>
</select>[/code]
[code=php]
if($_POST['comp_type'] == NULL || $_POST['comp_type'] == "- Select One -" || $_POST['comp_type'] == '') {
header('location:myform.php');
}
[/code]
[code=php]if($_POST['comp_type'] == NULL || $_POST['comp_type'] == "bar" || $_POST['comp_type'] == '') {
header('location:myform.php');[/code]
[code=php]if($_POST['comp_type'] == NULL || $_POST['comp_type'] == "club" || $_POST['comp_type'] == '') {
header('location:myform.php');[/code]
[code=php]if($_POST['comp_type'] == NULL || $_POST['comp_type'] == "resturant" || $_POST['comp_type'] == '') {
header('location:myform.php');[/code]
0.1.9 — BETA 5.18