Hi,
I am trying to develope a PHP script that will count the fields of a table specified from a form, and then it will insert values into the same table. Again, the values are the info a user submitted in a form. What I have done so far is the following code:
PHP Code:
First of all the user selects a category from a drop down menu, which is actually a MySQL table. This is done like this:
<html>
<body>
<form action = "manageCategories.php" method = "get">
<?php
$server = "localhost";
$username = "username";
$password = "password";
$database = "database";
$table = "Categories";
$connection = mysql_connect($server, $username, $password);
$db = mysql_select_db($database, $connection);
$query = "select * from $table";
$result = mysql_query($query);
$number_cols = mysql_num_fields($result);
echo "<table border = 1>";
echo "<tr align = center>";
echo "<td><font size = +1 color = #bg3333><b>Choose Category</b></font></td>";
echo "<td>";
echo "<select name = Categories>";
while ($row = mysql_fetch_row($result))
{
for ($i = 0; $i < $number_cols; $i++)
{
echo "<option value = \"$row[$i]\">$row[$i]";
}
}
echo "</td>\n";
echo "</select>\n";
echo "</table>";
?>
<tr>
<td>
<input type = "submit" name = "submit" value = "Submit">
</td>
</tr>
</form>
</body>
</html>
manageCategories.php
This is the next step. After submit, the script will display the fields of the selected table and textboxes to capture the user info. This is done like this:
<html>
<body>
<form action = addProduct.php method = get>
<?php
$server = "localhost";
$username = "username";
$password = "password";
$database = "database";
if ($submit)
{
$connection = mysql_connect ($server, $username, $password);
$db = mysql_select_db($database, $connection);
$query = "select * from $Categories"; /*Categories is the name of the previous list box*/
$result = mysql_query($query);
$number_cols = mysql_num_fields($result);
echo "<b>Add a Product</b>";
echo "<table border = 0>";
echo "<tr>";
echo "<td>";
echo "Category:<font color = red>*</font>";
echo "</td>";
echo "<td>";
echo "$Categories";
echo "</td>";
echo "<td>";
echo "<input name = Cat type = hidden value = $Categories>";
echo "</td>";
for ($i=0; $i<$number_cols; $i++)
{
echo "<tr>";
echo "<td nowrap>";
echo mysql_field_name($result, $i);
echo "</td>";
echo "<td nowrap>";
echo "<input name = chr($i + 97) type = text>";
/*echo chr($i + 97); a unique name for its textbox. Maybe this is the problem*/
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td>";
echo "<input name = add type = submit value = Add>";
echo "</td>";
echo "<td>";
echo "";
echo "</td>";
echo "</tr>";
echo "</table>";
}
?>
</form>
</body>
</html>
addProduct.php
This is the last page. This script doesn't work. This is where I have problems. I must specify the values the insert query asks for, but this is not easy (for me). That depends from the table. Its table has different fields.
<?php
$server = "localhost";
$username = "username";
$password = "password";
$database = "database";
if ($add)
{
$connection = mysql_connect($server,$username, $password);
$db = mysql_select_db($database, $connection);
$helpQuery = "select * from $Cat";
$helpResult = mysql_query($helpQuery);
$number_cols = mysql_num_fields($helpResult);
for ($i=0; $i<$number_cols; $i++)
{
//$value[$i] = "'"."$".chr($i + 97)."'";
}
$query = "insert into $Cat values ($a)";
//$result = mysql_query($query);
echo "$query";
}
?>
Please help me!!!