Webmaster Key - Discussion Forums


Welcome, Guest. Please login or register.
Did you miss your activation email?
February 09, 2012, 06:24:36 PM

Login with username, password and session length
Welceome to Forums!

Important information for guests and new members:

In order to understand the full benefits of becoming an active member of this forum, please review the following information on guest and new member restrictions. These forum changes have been prompted by an overwhelming and unreasonable amount of bot postings and incoherent guest spam messages. We wish to prevent these events from happening in the future and make our community a more comfortable place for all of our members.

For guests:

Guests are not allowed to open new topics, polls, or posts attachments.
If you wish to open up new discussions on this forum, we encourage you to register.

For new members:

New members with less than five posts are not allowed to modify additional profile information such as avatars, contact information, biographies, and signatures. However, new members are encouraged to post their own topics or reply to topics initiated by other members. Become active on the forums and 5 posts should be an easy task!

We are a diverse community with members from all over the world. We encourage new ideas and interesting conversation. Do not be afraid to post webmaster/computer-related questions or problems, as our active members are always willing to help when they are able. Interested? Join us.

+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ Coding Talk
| | | |-+ create tables with php
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print
Author Topic: create tables with php  (Read 4028 times)
stu
Key Keeper
Member
**
Posts: 83



WWW
« on: December 19, 2008, 03:23:26 PM »

Hi all


I'm having a little problem creating a php script to create database tables. Heres what i have got

   $qry = "CREATE TABLE $userid (id int not null primary key auto_increment,
testField varchar (75))";


this does create a databse table fine. it creates a table based on the users id but what i want is two tables based on the users id somthing like

$userid_contacts   and   $userid_info

is this possible what i want and how do i do it

thanks

stuart
Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #1 on: December 19, 2008, 03:32:28 PM »

Hi

I've got it working now fine with this code


   $qry = "CREATE TABLE diary_$login (id int not null primary key auto_increment,
testField varchar (75))";


but when i go to add a second qry to create a second table it will still only create one table and its the bottom qry

   $qry = "CREATE TABLE diary_$login (id int not null primary key auto_increment,
testField varchar (75))";

   $qry = "CREATE TABLE contacts_$login (id int not null primary key auto_increment,
testField varchar (75))";

any ideas?

thanks

stuart
« Last Edit: December 19, 2008, 03:54:06 PM by stu » Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #2 on: December 21, 2008, 01:26:06 PM »

Hi Stu,

I normally create the database tables in PHPMyAdmin in my Cpanel and this gives you the code that works. But it may contain additional stuff that is not needed that you can trim out, but you will have to test it.

But, I suspect that you are not executing the MySQL command to actually generate the table between the $qry string assignments. Surf over php.net to look for examples.

OK, I am feeling generous since it is near Christmas  Grin, here is a snippet from my code of how to generate a MySQL database table in an install script:

Code:
$q = "CREATE TABLE `" . TABLE_PREFIX . "links` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `count` int(10) unsigned NOT NULL default '0',
  `frame` int(11) NOT NULL,
  `cloak` int(11) NOT NULL,
  `url` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  `goal` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `name` (`name`)
) ENGINE = MYISAM";

connect_to_db();
$r = mysql_query($q);
if (!$r) {
echo "Query: $q ";
die('Invalid query: ' . mysql_error());
}

The freaky left-slanted tick marks are used in MySQL, so to get more exam points, make sure you use them. Your teachers maybe don't know about this. They are used for security reasons so the code breaks in the event of SQL injections rather than contorting off hackers.

TABLE_PREFIX  is a constant value that is included via the config file like with WordPress where they have the wp_ prefix as the default value. In your code you have a $ sign that may be a bug. so you may be wanting to insert the $login string variable into the table name, so you need to concatenate the elements of the string using dots to join them up.
« Last Edit: December 21, 2008, 01:55:22 PM by Andy » Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #3 on: December 21, 2008, 09:06:30 PM »

Hi

Thank you. I have managed to get it working how i want today. I to normaly create my tables in phpmyadmin. But I started php a few months ago and thought as I wasnt busy over xmas i would give myself a challange. What I am creating is a website called http://www.mylifelink.co.uk it intends to be a website rarther like a diary. I want it to have the ability for users to have there own account and in their a diary, address book, search the internet, search for friends that are signed up to the site.  I know its all been done before but I am trying to do this as it uses pretty much all php coding and I thought it would be a good way to learn.

The reason i needed the php code to create the database tables was so that when some one registers the register script will insert there registration details into the table users and the create the tables need for the diary and contacts. Each user will need to have a database table that is unique to them so hence the tables called contacts_$userid as only one username can be registered under that name.

I have just about finished today the login functions of the website. Its not uploaded yet. but its tested and working.

Login uses md5 encryption
register uses's form takes down all there details stores it in the database creates all the tables needed under each users userid name.
database stores users last login data and registration data
login uses session and displates the users id on screen <?php echo $_SESSION['SESS_userid'];?>  i then uses this to call there details from the database
logout closes the user session.

all this works fine. ok its probably not up to proffessinal standards. I've just taken it down now so that i can create the style and graphics and make it W3C compliant then its just a case of the contacts script, diary script, search users script, invite friends script extra.

I could do with something els, i was thinking something like a few simple games or applications as i know they can be done in php carnt they. any ideas welcome.

The script i used to get the registraton form to create the tables was, ok I've not created big tables yet but not sure what feilds are required so its just basic tables to test function.

   //Create insert query
   $qry = "INSERT INTO members(firstname, lastname, login, passwd, passwdr) VALUES('$fname','$lname','$login','".md5($_POST['password'])."','$cpassword')";
   $result = @mysql_query($qry);
   
   //Create Table for diary query
       $sql = "CREATE TABLE diary_$login (id int not null primary key auto_increment,
testField varchar (75))";

mysql_query( $sql );

//Create Table for contacts query
   $sql = "CREATE TABLE contacts_$login (id int not null primary key auto_increment,
testField varchar (75))";

mysql_query( $sql );

//Create insert for user activity query
$qry = "INSERT INTO activity(user_id, date) VALUES('$login','$date')";
   $result = @mysql_query($qry);
   
   //Check whether the query was successful or not
   if($result) {
      header("location: register-pass.php");
      exit();
   }else {
      die("Query 2 failed ");
   }

thanks

stuart
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #4 on: December 22, 2008, 11:48:37 AM »

Stu,

you are doing the opposite of what is standard practise. You should have common tables that all the users share. The database engine is optimized for speed to search and manipulate data in a table. You need to read up on database design (design of the structure of your database).

So your diary table should be for all the users with an index (Key) column of user ID as well as a primary index that is like a row count. Then the database engine can quickly find data relevant to a particular user.
Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #5 on: December 22, 2008, 12:54:31 PM »

hi

thanks, i think i know what u mean. I'll do a bit of reading up and see what i can cum up with.

thanks

stu
Report to moderator   Logged

stu
Key Keeper
Member
**
Posts: 83



WWW
« Reply #6 on: December 23, 2008, 08:35:18 AM »

Hi

I've been having a look at how to do these tables could you tell me if i'm on the right lines Create one single table for all users contacts somthing like

Create TABLE contacts (
id auto incrument,
user_id
firstname,
lastname,
phone,
fax,

Extra, I know thats not in detail but its just to show an example. All users would use this one table then. As the userid is unique to each user the search would need to be somthing like

on the find contact  it would be post values of firstname aqnd lastname


$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$userid = $_SESSION['SESS_userid']

qry = SELECT FROM TABLE contacts WHERE userid = $userid and WHERE firstname = $fname and WHERE lastname = $lname;

How would i write a mulitple where statment like this and is it possible?

thanks

stuart
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #7 on: December 23, 2008, 12:01:03 PM »

Yes, your table is along the right lines.

Here is a tutorial: http://dev.mysql.com/doc/refman/5.1/en/tutorial.html
Report to moderator   Logged

Pages: [1] Go Up Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print 
+ Webmaster Key Forums
|-+ Webmaster Corner
| |-+ Site Design and Web Authoring
| | |-+ Coding Talk
| | | |-+ create tables with php

Jump to:  
« previous next »


Our Partners
RelmaxTOP Ranking System Web Hosting RelmaxTOP Ranking System
Staff Sites
12Noon[12Noon Gallery] Andy[Urgentclick]
Tamuril[Tamuril's Digital Art Exhibit] Sensovision
Powered by MySQL Powered by PHP We are hosted by Relmax Inc. |Our Privacy Policy | Sitemap
Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC
Forum design by Tamuril © 2005.
Valid XHTML 1.0! Valid CSS!