Jump to content

Using user input to select a table from a database


stow19

Recommended Posts

Hi all,

 

I've been trying to write a piece of code that allows some data to be added to a table that is specified by the user but nothing seems to happen. So, for example if the user type in the value "chair" in the furniture box in the form below the rest of the data will be added to the table chair... Here's the code, it's a bit basic...

 

The Form:

<form method="post" action="process_add.php">
Furniture: <input type="text" name="furniture" /><br />
Material: <input type="text" name="material" /><br />
Price: <input type="text" name="price" /><br />
Quantity: <input type="text" name="quantity" /><br />
Color: <input type="text" name="color" /><br />
Width: <input type="text" name="width" /><br />
Height: £<input type="text" name="height" /><br />
<input type="submit" name="submit" value="Submit" />
<input type="reset" value="Cancel" />
</form>

 

and the php file

<?php
if($_POST['submit'])
{
$furniture = $_POST['furniture'];
$material = $_POST['material'];
$price = $_POST['price'];
$color = $_POST['quantity'];
$width = $_POST['width'];
$height = $_POST['height'];
$connection = mysql_connect("localhost", "user", "pass");
$db = "products";

if(!$furniture || !$material || !$price || $color || $width || $height)
       {
    	echo "Please fill out all the details";
        }
else
{
mysql_query("INSERT INTO '".$furniture."' (material, price, color, width, height) VALUES ('".$material."','".$price."','".$color."','".$width."','".$height."'");
	echo "The data you have filled in has been added to the table";
}
}
?>

 

Any help would be appreciated.  Thanks.

@AMcHarg Thanks for that, can't believe I forgot to put it in but... I forgot another piece of code  :-\... ok here we go... I'm trying to do a check to see if the furniture has the same type of material so I used this piece of code:

$mCheck = "SELECT material FROM ".$furniture." WHERE material = '".$material."'";
$mGet = mysql_query($nCheck) or die(mysql_error());

if($material == $mGet)
{
	echo "Please enter a different material";
}

 

 

From this peice of code I don't get any reply when I tested it. Sorry about the poor mistake before

You made a typo: mysql_query($nCheck) should be mysql_query($mCheck), see below:

 

$mCheck = "SELECT material FROM ".$furniture." WHERE material = '".$material."'";

$mGet = mysql_query($mCheck) or die(mysql_error());
if($material == $mGet) {
echo "Please enter a different material";
}

Archived

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

×
×
  • Create New...

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.