Create table plusieurs d'un coup

ViPHP
ViPHP | 5462 Messages

15 déc. 2010, 21:28

Non je ne connais pas le SQL. Wordpress met la clé primaire à la fin donc je ne voyais pas le problème. Et ça ne fonctionne pas. J'ai viré la clé primaire temporairement mais ce n'Est pas ça encore.
je t'ai dis des bêtises c'est bien a la fin, mais quel est ton message d'erreur toujours le même ? dans le fond c'est quoi le problème pourquoi tu fait tout ca si tu connais pas le langage ? qu'est ce que t'essayes de faire ?

Eléphanteau du PHP | 16 Messages

15 déc. 2010, 21:39

L'objectif premier: apprendre en décortiquant une installation de wordpress
Dans le cas présent, j'ai un formulaire à remplir et à partir de là je veux lancer l'installation de la base de données. Or créer une table marche nickel, en créer 2 ça échoue.

Je mets l'ensemble du code si jamais tu voies quelque chose de bizarre.
$server = $_POST["dbserver"]; 
$username = $_POST["dbusername"]; 
$password = $_POST["dbpassword"]; 
$databasename = $_POST["dbname"]; 
$serverstring = file_get_contents('../config-sample.php');
$connect = mysql_connect($server, $username, $password);

// Connection to the db server
if(mysql_connect($server, $username, $password)) { 
  echo '<p class="success">Connected to the server</p>';

// Select the db
if(mysql_select_db($databasename, $connect)) {
  echo '<p class="success">Database selected</p>';

// Save config file
//$configfilesample = '../config-sample.php';
//$contents = file_get_contents($configfilesample); 
//$chercher = array("wordpress", "root", "rootpassword"); 
//$remplacer = array($databasename, $username, $password); 
 
//$contents = str_replace("wordpress", "test", $contents); 

//if(file_put_contents($configfilesample, $contents)) {
 //if(str_replace("wordpress", "test", $contents)) {
 //echo '<p class="success">Config file saved</p>';


// Create table
//FOREIGN KEY (UserType) REFERENCES Settings(ID)
$tablecreation = "CREATE TABLE users
(
ID int NOT NULL auto_increment PRIMARY KEY(ID),
Name varchar(30),
Username varchar(20) UNIQUE,
Password varchar(32),
UserType int,
Modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
) CHARACTER SET utf8;

CREATE TABLE settings
(
ID int NOT NULL auto_increment PRIMARY KEY(ID),
Name varchar(30),
Username1 varchar(20) UNIQUE,
Password varchar(32),
UserType int,
Modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) CHARACTER SET utf8;" ;

if (mysql_query($tablecreation,$connect)) {
  echo '<p class="success">Table created</p>';

// Create Admin account
$adminaccountcreation="INSERT INTO users (Name, Username, Password, UserType)
VALUES
('$_POST[adminname]','$_POST[adminusername]',MD5('$_POST[adminpassword]'),1)";
if (mysql_query($adminaccountcreation,$connect)) {
  echo '<p class="success">' ."Admin account created". '</p>' , '<p class="success">Sucessfull installation</p>' , '<a href="#" class="button">Log in</a>' ;
}

else { // error crating admin account
  die('<p class="error">Error creating Administrator account: ' . mysql_error()) . '</p>' ;
}
}
else { // error creating table
  die('<p class="error">Error creating table: ' . mysql_error()) . '</p>' ;
}
}
//else { // error saving config file 
  //die('<p class="error">Could not save config file </p>'); 
//} 
//}
else { // error database selection
  echo '<p class="error">' ."Error selecting database. Make sure it exists: " . mysql_error() . '</p>' ;
}
}
else { // error connection to the db server 
echo '<p class="error">Could not connect to the server: ' . mysql_error() . '</p>' ; 
} 

mysql_close($connect);


Non je ne connais pas le SQL. Wordpress met la clé primaire à la fin donc je ne voyais pas le problème. Et ça ne fonctionne pas. J'ai viré la clé primaire temporairement mais ce n'Est pas ça encore.
je t'ai dis des bêtises c'est bien a la fin, mais quel est ton message d'erreur toujours le même ? dans le fond c'est quoi le problème pourquoi tu fait tout ca si tu connais pas le langage ? qu'est ce que t'essayes de faire ?

ViPHP
ViPHP | 5462 Messages

15 déc. 2010, 22:20

avec l'extension mysql tu peux pas faire de requete multiple

Eléphanteau du PHP | 16 Messages

15 déc. 2010, 22:40

Ce qui veut dire?
Et comment mettre tout le script de création de la BDD dans un fichier séparé que j'appellerai dans ma page actuelle?
avec l'extension mysql tu peux pas faire de requete multiple

ViPHP
ViPHP | 5462 Messages

15 déc. 2010, 23:01

Ce qui veut dire?
que ce que t'essaye de faire c'est pas possible,
Et comment mettre tout le script de création de la BDD dans un fichier séparé que j'appellerai dans ma page actuelle?
séparer tes requêtes et les exécuter une par une

Eléphanteau du PHP | 16 Messages

15 déc. 2010, 23:07

Si je regarde l'exemple de wordpress ils font pareil et ça marche, non?
/** Create WordPress database tables SQL */
$wp_queries = "CREATE TABLE $wpdb->terms (
 term_id bigint(20) unsigned NOT NULL auto_increment,
 name varchar(200) NOT NULL default '',
 slug varchar(200) NOT NULL default '',
 term_group bigint(10) NOT NULL default 0,
 PRIMARY KEY  (term_id),
 UNIQUE KEY slug (slug),
 KEY name (name)
) $charset_collate;
CREATE TABLE $wpdb->term_taxonomy (
 term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment,
 term_id bigint(20) unsigned NOT NULL default 0,
 taxonomy varchar(32) NOT NULL default '',
 description longtext NOT NULL,
 parent bigint(20) unsigned NOT NULL default 0,
 count bigint(20) NOT NULL default 0,
 PRIMARY KEY  (term_taxonomy_id),
 UNIQUE KEY term_id_taxonomy (term_id,taxonomy),
 KEY taxonomy (taxonomy)
) $charset_collate;
CREATE TABLE $wpdb->term_relationships (
 object_id bigint(20) unsigned NOT NULL default 0,
 term_taxonomy_id bigint(20) unsigned NOT NULL default 0,
 term_order int(11) NOT NULL default 0,
 PRIMARY KEY  (object_id,term_taxonomy_id),
 KEY term_taxonomy_id (term_taxonomy_id)
) $charset_collate;
CREATE TABLE $wpdb->commentmeta (
  meta_id bigint(20) unsigned NOT NULL auto_increment,
  comment_id bigint(20) unsigned NOT NULL default '0',
  meta_key varchar(255) default NULL,
  meta_value longtext,
  PRIMARY KEY  (meta_id),
  KEY comment_id (comment_id),
  KEY meta_key (meta_key)
) $charset_collate;
CREATE TABLE $wpdb->comments (
  comment_ID bigint(20) unsigned NOT NULL auto_increment,
  comment_post_ID bigint(20) unsigned NOT NULL default '0',
  comment_author tinytext NOT NULL,
  comment_author_email varchar(100) NOT NULL default '',
  comment_author_url varchar(200) NOT NULL default '',
  comment_author_IP varchar(100) NOT NULL default '',
  comment_date datetime NOT NULL default '0000-00-00 00:00:00',
  comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  comment_content text NOT NULL,
  comment_karma int(11) NOT NULL default '0',
  comment_approved varchar(20) NOT NULL default '1',
  comment_agent varchar(255) NOT NULL default '',
  comment_type varchar(20) NOT NULL default '',
  comment_parent bigint(20) unsigned NOT NULL default '0',
  user_id bigint(20) unsigned NOT NULL default '0',
  PRIMARY KEY  (comment_ID),
  KEY comment_approved (comment_approved),
  KEY comment_post_ID (comment_post_ID),
  KEY comment_approved_date_gmt (comment_approved,comment_date_gmt),
  KEY comment_date_gmt (comment_date_gmt),
  KEY comment_parent (comment_parent)
) $charset_collate;
Ce qui veut dire?
que ce que t'essaye de faire c'est pas possible,
Et comment mettre tout le script de création de la BDD dans un fichier séparé que j'appellerai dans ma page actuelle?
séparer tes requêtes et les exécuter une par une

ViPHP
ViPHP | 5462 Messages

15 déc. 2010, 23:10

Si je regarde l'exemple de wordpress ils font pareil et ça marche, non?
/** Create WordPress database tables SQL */
$wp_queries = "CREATE TABLE $wpdb->terms (
 term_id bigint(20) unsigned NOT NULL auto_increment,
 name varchar(200) NOT NULL default '',
 slug varchar(200) NOT NULL default '',
 term_group bigint(10) NOT NULL default 0,
 PRIMARY KEY  (term_id),
 UNIQUE KEY slug (slug),
 KEY name (name)
) $charset_collate;
CREATE TABLE $wpdb->term_taxonomy (
 term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment,
 term_id bigint(20) unsigned NOT NULL default 0,
 taxonomy varchar(32) NOT NULL default '',
 description longtext NOT NULL,
 parent bigint(20) unsigned NOT NULL default 0,
 count bigint(20) NOT NULL default 0,
 PRIMARY KEY  (term_taxonomy_id),
 UNIQUE KEY term_id_taxonomy (term_id,taxonomy),
 KEY taxonomy (taxonomy)
) $charset_collate;
CREATE TABLE $wpdb->term_relationships (
 object_id bigint(20) unsigned NOT NULL default 0,
 term_taxonomy_id bigint(20) unsigned NOT NULL default 0,
 term_order int(11) NOT NULL default 0,
 PRIMARY KEY  (object_id,term_taxonomy_id),
 KEY term_taxonomy_id (term_taxonomy_id)
) $charset_collate;
CREATE TABLE $wpdb->commentmeta (
  meta_id bigint(20) unsigned NOT NULL auto_increment,
  comment_id bigint(20) unsigned NOT NULL default '0',
  meta_key varchar(255) default NULL,
  meta_value longtext,
  PRIMARY KEY  (meta_id),
  KEY comment_id (comment_id),
  KEY meta_key (meta_key)
) $charset_collate;
CREATE TABLE $wpdb->comments (
  comment_ID bigint(20) unsigned NOT NULL auto_increment,
  comment_post_ID bigint(20) unsigned NOT NULL default '0',
  comment_author tinytext NOT NULL,
  comment_author_email varchar(100) NOT NULL default '',
  comment_author_url varchar(200) NOT NULL default '',
  comment_author_IP varchar(100) NOT NULL default '',
  comment_date datetime NOT NULL default '0000-00-00 00:00:00',
  comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  comment_content text NOT NULL,
  comment_karma int(11) NOT NULL default '0',
  comment_approved varchar(20) NOT NULL default '1',
  comment_agent varchar(255) NOT NULL default '',
  comment_type varchar(20) NOT NULL default '',
  comment_parent bigint(20) unsigned NOT NULL default '0',
  user_id bigint(20) unsigned NOT NULL default '0',
  PRIMARY KEY  (comment_ID),
  KEY comment_approved (comment_approved),
  KEY comment_post_ID (comment_post_ID),
  KEY comment_approved_date_gmt (comment_approved,comment_date_gmt),
  KEY comment_date_gmt (comment_date_gmt),
  KEY comment_parent (comment_parent)
) $charset_collate;
qui te dit qu'il font un mysql_query juste après ? d'ailleurs il utilise peu être une autre extension (mysqli, PDO)

Eléphanteau du PHP | 16 Messages

15 déc. 2010, 23:23

OK je vois ce que tu veux dire alors est-ce que ce genre de raisonnement pourrait marcher?

Je garde mon fichier actuel appelons le A. Je mets mes instructions dans fichier B. J'appelle B à partir de A mais aucune idée sur la commande à réaliser.

if (fichier B s'exécute correctement) {
echo "Tout bon"
}
else {
echo "Erreur"
}

B contient
$table creation = " CREATE TABLE users ... "
Si je regarde l'exemple de wordpress ils font pareil et ça marche, non?
/** Create WordPress database tables SQL */
$wp_queries = "CREATE TABLE $wpdb->terms (
 term_id bigint(20) unsigned NOT NULL auto_increment,
 name varchar(200) NOT NULL default '',
 slug varchar(200) NOT NULL default '',
 term_group bigint(10) NOT NULL default 0,
 PRIMARY KEY  (term_id),
 UNIQUE KEY slug (slug),
 KEY name (name)
) $charset_collate;
CREATE TABLE $wpdb->term_taxonomy (
 term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment,
 term_id bigint(20) unsigned NOT NULL default 0,
 taxonomy varchar(32) NOT NULL default '',
 description longtext NOT NULL,
 parent bigint(20) unsigned NOT NULL default 0,
 count bigint(20) NOT NULL default 0,
 PRIMARY KEY  (term_taxonomy_id),
 UNIQUE KEY term_id_taxonomy (term_id,taxonomy),
 KEY taxonomy (taxonomy)
) $charset_collate;
CREATE TABLE $wpdb->term_relationships (
 object_id bigint(20) unsigned NOT NULL default 0,
 term_taxonomy_id bigint(20) unsigned NOT NULL default 0,
 term_order int(11) NOT NULL default 0,
 PRIMARY KEY  (object_id,term_taxonomy_id),
 KEY term_taxonomy_id (term_taxonomy_id)
) $charset_collate;
CREATE TABLE $wpdb->commentmeta (
  meta_id bigint(20) unsigned NOT NULL auto_increment,
  comment_id bigint(20) unsigned NOT NULL default '0',
  meta_key varchar(255) default NULL,
  meta_value longtext,
  PRIMARY KEY  (meta_id),
  KEY comment_id (comment_id),
  KEY meta_key (meta_key)
) $charset_collate;
CREATE TABLE $wpdb->comments (
  comment_ID bigint(20) unsigned NOT NULL auto_increment,
  comment_post_ID bigint(20) unsigned NOT NULL default '0',
  comment_author tinytext NOT NULL,
  comment_author_email varchar(100) NOT NULL default '',
  comment_author_url varchar(200) NOT NULL default '',
  comment_author_IP varchar(100) NOT NULL default '',
  comment_date datetime NOT NULL default '0000-00-00 00:00:00',
  comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  comment_content text NOT NULL,
  comment_karma int(11) NOT NULL default '0',
  comment_approved varchar(20) NOT NULL default '1',
  comment_agent varchar(255) NOT NULL default '',
  comment_type varchar(20) NOT NULL default '',
  comment_parent bigint(20) unsigned NOT NULL default '0',
  user_id bigint(20) unsigned NOT NULL default '0',
  PRIMARY KEY  (comment_ID),
  KEY comment_approved (comment_approved),
  KEY comment_post_ID (comment_post_ID),
  KEY comment_approved_date_gmt (comment_approved,comment_date_gmt),
  KEY comment_date_gmt (comment_date_gmt),
  KEY comment_parent (comment_parent)
) $charset_collate;
qui te dit qu'il font un mysql_query juste après ? d'ailleurs il utilise peu être une autre extension (mysqli, PDO)

ViPHP
ViPHP | 5462 Messages

15 déc. 2010, 23:26

utilise les fonctions de Wordpress, celle qui t'intéresse c'est dbDelta qui se trouve dans le fichier /wp-admin/includes/upgrade.php