Coincidently, I have been working on the same thing as you, my code is below. This code is good for adding a record, you just need to set $dname, $tname, localhost root and password to your settings. The file should be names addrecord.php
Now the problem I am facing is that adding is easy, but when you have to delete or update (edit) a record you need a unique identifier and for some reason when I create my database with the auto increment unique field it doesn't create the field. So I would have a hard time setting specify what in my table I want to delete or update. if you have any suggestions or any questions just holla.
<?php
if (isset($_POST['action']) && $_POST['action'] == 'submitted')
{
//executed second
$dname = $_POST[dname];
$tname = $_POST[tname];
//connect to mysql
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//select database and table
mysql_select_db($dname, $con);
$result = mysql_query("select * from $tname");
//this is where the magic happens, the code below goes to the specified database and table and extract the field names and make a form that has the field names and textfields and a submit button for the user.
$i = 0;
echo "<form action=\"addrecord.php\" method=\"post\">";
while ($i < mysql_num_fields($result))
{
$meta = mysql_fetch_field($result, $i);
echo "$meta->name:<input type=\"text\" name=\"fname[]\" /><br>";
$i++;
}
mysql_close($con);
echo "<input type=\"hidden\" name=\"action\" value=\"newsubmit\" />";
echo "<input type=\"hidden\" name=\"field_dname\" value=\"$dname\" />";
echo "<input type=\"hidden\" name=\"field_tname\" value=\"$tname\" />";
echo "<input type=\"submit\" name=\"submit\" value=\"Show Field(s)\" />
</form>";
}
else if (isset($_POST['action']) && $_POST['action'] == 'newsubmit')
{
//execute third
$wp = $_POST['field_dname'];
$motto = $_POST['field_tname'];
$fname2=$_POST['fname'];
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($wp, $con);
//this part of the code make the insert into table with the user input, you should echo $result to see what happens
foreach($fname2 as $ndata){$br .= "'$ndata', ";};
$nval = $br;
$nval = substr($nval, 0, strlen($nval) - 2);
$result = "INSERT into $motto VALUES($nval)";
mysql_query($result);
mysql_close($con);
echo "record added \n";
}
else
{
//execute first
echo <<< HERE
<form action="addrecord.php" method="post">
Database Name: <input type="text" name="dname" />
Table Name: <input type="text" name="tname" /><br>
<input type="hidden" name="action" value="submitted" />
<input type="submit" name="submit" value="Create Table" />
</form>
HERE;
}
?>