...

View Full Version : Create sql row



Cliffo
03-28-2010, 02:30 AM
I made the following code in a text file to import through phpmyadmin and create a row;


ALTER TABLE ws_780_settings
ADD ( activetheme varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT 'default' );

What i want to do is made it into a php so users can just browse to yoursite.com/install.php, but i cant get the files to work. Can someone rewrite that small peice to put in a .php file, here is what i tried but it doesnt add the row.



<?php
include ("_mysql.php");
mysql_connect($host, $user, $pwd) or die ('FEHLER: Keine Verbindung zu MySQL');
mysql_select_db($db) or die ('FEHLER: Konnte nicht zur Datenbank "'.$db.'" verbinden');

mysql_query("INSERT INTO ".PREFIX."settings (activetheme)
VALUES (default)");

echo'<center><h2>Theme Selector has be installed.</h2><h2>Make sure to delete install.php from your server.</h2><h2>Visit <a href="http://www.cgshost.com/">CGShost.com</a> for support and more!</h2></center>';
?>


here is an example of another one i have (for a diff mod) that works perfect;


<?php
include ("_mysql.php");
mysql_connect($host, $user, $pwd) or die ('FEHLER: Keine Verbindung zu MySQL');
mysql_select_db($db) or die ('FEHLER: Konnte nicht zur Datenbank "'.$db.'" verbinden');

mysql_query("DROP TABLE IF EXISTS ".PREFIX."features");
mysql_query("CREATE TABLE ".PREFIX."features (
featureID int(11) NOT NULL auto_increment,
url varchar(255) NOT NULL default '',
featurestext varchar(255) NOT NULL default '',
banner varchar(255) NOT NULL default '',
sort int(11) NOT NULL default '0',
PRIMARY KEY (featureID)
) TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=2 ");

echo'<center><h2>Features installed sucesfully</h2><br><h2><br><br><br>Please delete install.php</center>';
?>

Thanks for any help!

tomws
03-28-2010, 03:24 AM
How about instead of someone doing your work for you, you post the broken code and ask for help fixing that?

Cliffo
03-28-2010, 03:32 AM
Sure sorry about that,


<?php
include ("_mysql.php");
mysql_connect($host, $user, $pwd) or die ('FEHLER: Keine Verbindung zu MySQL');
mysql_select_db($db) or die ('FEHLER: Konnte nicht zur Datenbank "'.$db.'" verbinden');

mysql_query("ALTER TABLE ".PREFIX."settings ( ADD
activetheme varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT 'default')
TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=2 ");

echo'<center><h2>Theme Selector has installed sucesfully</h2><br><h2><br><br><br>Please delete install.php</center>';
?>

tomws
03-28-2010, 03:35 AM
You mentioned a syntax error in the original post. What is it? And is it from php or mysql?

EDIT: By the way, there's a misspelling. Should be "successfully".

Cliffo
03-28-2010, 04:17 AM
When i run it through the web browser there are no errors at all but the row never gets added to the table, syntax errors were from uploading directly but i want to prevent having to do that.

tomws
03-28-2010, 04:24 AM
It's a column (or field) you're adding, not a row.

The ALTER query in the first post is different than the query in the broken code. Why?

Cliffo
03-28-2010, 04:26 AM
I added the prefix command from the _mysql and what not, plus i really only know html still trying to learn sql and php.

tomws
03-28-2010, 04:28 AM
So, you've defined PREFIX somewhere? What about this stuff?

TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=2

Cliffo
03-28-2010, 04:35 AM
Not sure, i seen it in the other install.php that does the same thing im trying to do, i have tried dozens of different ways while reading w3schools, here is my latest;


<?php
include ("_mysql.php");
mysql_connect($host, $user, $pwd) or die ('FEHLER: Keine Verbindung zu MySQL');
mysql_select_db($db) or die ('FEHLER: Konnte nicht zur Datenbank "'.$db.'" verbinden');

mysql_query("INSERT INTO ".PREFIX."settings (activetheme)
VALUES (default)");

echo'<center><h2>Theme Selector has be installed.</h2><h2>Make sure to delete install.php from your server.</h2><h2>Visit <a href="http://www.cgshost.com/">CGShost.com</a> for support and more!</h2></center>';
?>

MattF
03-28-2010, 04:41 AM
mysql_query("ALTER TABLE ".PREFIX."settings ADD COLUMN activetheme varchar(255) NOT NULL DEFAULT 'default'") or exit(mysql_error());

tomws
03-28-2010, 04:43 AM
Why are you attempting an INSERT? Didn't you say that this works through phpmyadmin?

ALTER TABLE ws_780_settings
ADD ( activetheme varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT 'default' );
If so, make that your query. If it works, then you can get more fancy and figure out how to use a prefix.

The other one does not do what you're trying to do - it creates an entirely new table. You can tell because it says CREATE TABLE.

Cliffo
03-28-2010, 04:50 AM
If so, make that your query. If it works, then you can get more fancy and figure out how to use a prefix.

Obviously i tried that and it doesnt work, syntax errors.

Matt i tried yours and for some reason it gave a duplicate column error but it defiantly is not there, i even tried on a fresh db and same error.

Cliffo
03-28-2010, 05:10 AM
Never mind MattF's works, thanks!

tomws
03-28-2010, 05:20 AM
Obviously i tried that and it doesnt work, syntax errors.

You were asked for the errors way back in post #4 and then said there really weren't any. :rolleyes:

Cliffo
03-28-2010, 05:22 AM
Here is the final code, thanks again!


<?php
include ("_mysql.php");
mysql_connect($host, $user, $pwd) or die ('FEHLER: Keine Verbindung zu MySQL');
mysql_select_db($db) or die ('FEHLER: Konnte nicht zur Datenbank "'.$db.'" verbinden');

mysql_query("ALTER TABLE ".PREFIX."settings DROP COLUMN activetheme");

mysql_query("ALTER TABLE ".PREFIX."settings ADD COLUMN activetheme varchar(255) NOT NULL DEFAULT 'default'") or exit(mysql_error());

echo'<center><h2>Theme Selector has be installed.</h2><h2>Make sure to delete install.php from your server.</h2><h2>Visit <a href="http://www.cgshost.com/">CGShost.com</a> for support and more!</h2></center>';
?>

Please set to resolved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum