Create table and insert into on same page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ken_in_NH
Forum Newbie
Posts: 4
Joined: Fri Jun 29, 2007 8:23 am

Create table and insert into on same page

Post 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]
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Post by idevlin »

I don't see why you would want to create a new table every time anyway?
Ken_in_NH
Forum Newbie
Posts: 4
Joined: Fri Jun 29, 2007 8:23 am

New table

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Why can't you put all addresses in one table?
Ken_in_NH
Forum Newbie
Posts: 4
Joined: Fri Jun 29, 2007 8:23 am

All addresses

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: All addresses

Post by volka »

Ken_in_NH wrote:How would I identify which listings go with which addresses?
How do you do that with multiple tables?
Ken_in_NH
Forum Newbie
Posts: 4
Joined: Fri Jun 29, 2007 8:23 am

Multiple tables

Post 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?
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Post 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.
Post Reply