Jump to content


Photo

Creating A Table - Entry


  • Please log in to reply
2 replies to this topic

#1 AliasXNeo

AliasXNeo
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 03 January 2006 - 03:57 AM

Okay, here I have put together two functions. One creates a table, the other creates an entry, except neither are doing the original intention. I get no errors, and it 'seems' to work fine. The two functions are held in a PHP file, I will display all my code in the best manner I can:

sqlfunctions.php > This is where the functions are held:

<?php
function create_table($tablename, $values){
  $VString = join(', ', $values);
  $query = 'CREATE TABLE ' . $tablename . '(' . $VString . ')';
  $result = mysql_query($query);
  print 'Created table $tablename';
}

function insert($tablename, $parameters, $values){
$PString = join(', ', $parameters);
$VString = join(', ', $values);
$query = 'INSERT INTO $tablename ($PString) VALUES ($VString)';
mysql_query($query);
}
?>

Install_Login.php > This script was supposed to create a table for login use using the function above:

<?php
include 'Php/sqlfunctions.php';
include 'Php/connect.php';

$name = "newlogin";
$Values[0] = "id int(6) NOT NULL auto_increment";
$Values[1] = "user varchar(15) NOT NULL";
$Values[2] = "password varchar(15) NOT NULL";
$Values[3] = "PRIMARY KEY (id)";
$Values[4] = "UNIQUE id (id)";
$Values[5] = "KEY id_2 (id)";
create_table($name, $Values[]);

mysql_close($conn);
?>

Create_User.php > This script was supposed to create a user in the table using the function in the function script above:

<?php
include 'Php/connect.php';
include 'Php/sqlfunctions.php';

$pars[0] = "id";
$pars[1] = "user";
$pars[2] = "password";

$values[0] = "";
$values[1] = "Josh";
$values[2] = "josh7234";

$name = "login";

insert($name, $pars[], $values[]);

mysql_close($conn);
?>

So nothing is working. Would someone mind helping me get all this fixed, as with no errors I am clueless to what is wrong.

#2 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 03 January 2006 - 05:10 AM

Hi.

With your code you should have received some errors. At least about the brackets in function calls.

The table creation was OK but my myPhpAdmin has a warning that "you cant have both PRIMARY and INDEX keys for columns id".
Also inserting went OK, but I had to include single quotes to the value ($values[1] = "'Josh'";)

I don't know if the warning has some effects on the table later. But wouldn't it be enough if you just declared the primary key?

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 January 2006 - 09:25 AM

Not to mention that you don't need to explicitly specify the array index. And you might as well drop the two extra indices, they are not necessary for the PRIMARY KEY, and just waste space and affect performance. Also, as LazyJones said, you need to quote your VALUES() -- at least the ones that are strings -- so you'd need to join then with "','" and add a leading & trailing "'" to the the $VString variable.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users