Jump to content

Creating A Table - Entry


AliasXNeo

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.