Page 1 of 1

Create table and insert into on same page

Posted: Fri Jun 29, 2007 8:26 am
by Ken_in_NH
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Is it possible to create a table and insert data into the table on the same page?

Here is what I am using:

THE HTML FORM:

The input form (input.html)
[syntax="html"]<form action="insert.php" method="POST" enctype="multipart/form-data">

Table Name (Which would be the address)<input type="text" name="table_name" SIZE="60"><P>

Would be the field names starting with address again<input type="text" name="fulladdress" SIZE="60"><P>

<input type="submit" value="Submit Questionaire" name="func" >&nbsp;&nbsp;<INPUT NAME="reset" type="reset" value="Start Over" ALIGN=absbottom>

</FORM>
THE PHP PAGE[/syntax]

Code: Select all

<HTML>
<HEAD>

<?

$username="";
$password="";
$database="";
$server="";

$con = mysql_connect($server,$username,$password);


if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$newtable = $_POST["$table_name"];

// Create table in my_db database
mysql_select_db("buysellnh_com_showings", $con);
$sql = "CREATE TABLE $newtable IF NOT EXISTS
(
id INT,
address VARCHAR(100),
cur_timestamp TIMESTAMP(8)
)";
mysql_query($sql,$con);
$sql="insert into $newtable (id,address,cur_timestamp) values ('',".$_POST['fulladdress'].",NOW())";
mysql_query($sql,$con);
mysql_close($con);

?>
Nothing happens to the DB when this is executed (There is a simple form that populates the variables)

when I remove the insert into line, I get the new table in the DB but when I add back the INSERT INTO I get no table and no rows...

What am I doing wrong?

The customer (A realtor) wants to be able to add an address and then add showings to that address. The address would be the table and the showings would be the rows....

Greatly appreciate anybody's help...

Ken


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jun 29, 2007 8:36 am
by idevlin
I don't see why you would want to create a new table every time anyway?

New table

Posted: Fri Jun 29, 2007 8:41 am
by Ken_in_NH
The process I am creating is for a realtor to show listings on a property. They will input a street address (Name of the table) and then once the table is created, they will insert the dates and showing information for each address. Is there a better way to do this.

Essentially, each table consistes of all the showings for a given property. The table is only created once and then populated as showings are scheduled. Does that make sense?

Ken

Posted: Fri Jun 29, 2007 8:47 am
by volka
Why can't you put all addresses in one table?

All addresses

Posted: Fri Jun 29, 2007 8:53 am
by Ken_in_NH
How would I identify which listings go with which addresses?

Just have an address drop down and allow the user to either select an existing address or add a new one?

and if they select an address that exists, how do I make it not add that field again seeing as how it is already in the dbase?

Re: All addresses

Posted: Fri Jun 29, 2007 9:01 am
by volka
Ken_in_NH wrote:How would I identify which listings go with which addresses?
How do you do that with multiple tables?

Multiple tables

Posted: Fri Jun 29, 2007 9:07 am
by Ken_in_NH
Couldn't each table be the address and all the data within the table be all the showings. Therefore when they select a table tabe it would show all the rows?

Posted: Fri Jun 29, 2007 9:18 am
by idevlin
Have one table with listings and another with addresses. Give each address a unique id, which is then also stored in the listings table when a listing is made for that address.

Then when the person selects that address, you have its id, then you select everything from the listings table where the id is the id of the address you want.