SQL query werkt niet in php wel in phpmyadmin
Dit is de code:
Quote:
//Creates the table for pages and messages
$sql = "
CREATE TABLE IF NOT EXISTS `pages` (
`id` int NOT NULL AUTO_INCREMENT primary key,
`title` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
";
$result = mysql_query($sql) or die("creating the table pages failed..");
$sql = "
CREATE TABLE IF NOT EXISTS `messages` (
`id` int AUTO_INCREMENT primary key NOT NULL,
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`page` varchar(255) NOT NULL,
`posting_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
";
$result = mysql_query($sql) or die("creating the table messages failed..");
/********************************************
De query van messages maken werkt niet?
********************************************/
//This creates 2 pages and makes a message on the homepage and on the firstpage page.
$time = time();
$sql = "
INSERT INTO pages (title) VALUES ('Home');
INSERT INTO pages (title) VALUES ('firstpage');
INSERT INTO messages (title, description, page, posting_date) VALUES ('<h1>This is your first Message!</h1>', '<p>You can change this message if you like in the admin panel.</p>', 'home', '$time');
INSERT INTO messages (title, description, page, posting_date) VALUES ('<h1>Welcome to an example page!</h1>', '<p>This is just another test message and you can choose if you want to delete or modify this message in the admin panel.</p>', 'firstpage', '$time');
";
$result = mysql_query($sql) or die("SQL ERROR IN POST MAKING");
$sql = "
CREATE TABLE IF NOT EXISTS `pages` (
`id` int NOT NULL AUTO_INCREMENT primary key,
`title` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
";
$result = mysql_query($sql) or die("creating the table pages failed..");
$sql = "
CREATE TABLE IF NOT EXISTS `messages` (
`id` int AUTO_INCREMENT primary key NOT NULL,
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`page` varchar(255) NOT NULL,
`posting_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
";
$result = mysql_query($sql) or die("creating the table messages failed..");
/********************************************
De query van messages maken werkt niet?
********************************************/
//This creates 2 pages and makes a message on the homepage and on the firstpage page.
$time = time();
$sql = "
INSERT INTO pages (title) VALUES ('Home');
INSERT INTO pages (title) VALUES ('firstpage');
INSERT INTO messages (title, description, page, posting_date) VALUES ('<h1>This is your first Message!</h1>', '<p>You can change this message if you like in the admin panel.</p>', 'home', '$time');
INSERT INTO messages (title, description, page, posting_date) VALUES ('<h1>Welcome to an example page!</h1>', '<p>This is just another test message and you can choose if you want to delete or modify this message in the admin panel.</p>', 'firstpage', '$time');
";
$result = mysql_query($sql) or die("SQL ERROR IN POST MAKING");
Als ik deze code uitvoer krijg ik altijd SQL ERROR IN POST MAKING. Dus in de laatste query zit een fout. Maar ik kan hem maar niet vinden. Als ik dit in PHPmyadmin uitvoer werkt het wel.?
eens vervangt door die ( mysql_error() );
dan zie je tenminste een echte foutmelding.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO pages (title) VALUES ('firstpage'); INSERT INTO messages (title, d' at line 2
maar ik zie nog de fout niet :(
Wat je zou kunnen proberen is $time = time() te vervangen door $time = date( 'Y-m-d H:i:s') en in de sql de quotes om $time weglaten?
INSERT INTO pages (title) VALUES ('Home');
INSERT INTO pages (title) VALUES ('firstpage');
gelaten. Dan krijg ik nog steeds dezelfde error.
als ik die 2 insert into niet doe en juist die met time krijg ik ook weer die error maar dan met de nieuwe tekst <.<
En als ik de quotes bij $time weg haal maakt het niet uit. nog steeds zelfde error.
Normaal gesproken werkt dit gewoon. Wat zie ik over het hoofd... Om gek van te worden ;-)
Hmz, over kippevel en kromme tenen gesproken dit kan niet waar zijn.
Ik heb nu alle insert into's opgedeeld in verschillende variabels.
Wat natuurlijk zeer geheugen verspillend is.
Maar deze code werkt wel:
Quote:
$time = time();
$sql = "INSERT INTO pages (title) VALUES ('Home');";
$sql1 = "INSERT INTO pages (title) VALUES ('firstpage');";
$sql2 = "INSERT INTO messages (title, description, page, posting_date) VALUES ('<h1>This is your first Message!</h1>', '<p>You can change this message if you like in the admin panel.</p>', 'home', '$time');";
$sql3 = "INSERT INTO messages (title, description, page, posting_date) VALUES ('<h1>Welcome to an example page!</h1>', '<p>This is just another test message and you can choose if you want to delete or modify this message in the admin panel.</p>', 'firstpage', '$time');";
$result = mysql_query($sql) or die (mysql_error());
$result1 = mysql_query($sql1) or die (mysql_error());
$result2 = mysql_query($sql2) or die (mysql_error());
$result3 = mysql_query($sql3) or die (mysql_error());
$sql = "INSERT INTO pages (title) VALUES ('Home');";
$sql1 = "INSERT INTO pages (title) VALUES ('firstpage');";
$sql2 = "INSERT INTO messages (title, description, page, posting_date) VALUES ('<h1>This is your first Message!</h1>', '<p>You can change this message if you like in the admin panel.</p>', 'home', '$time');";
$sql3 = "INSERT INTO messages (title, description, page, posting_date) VALUES ('<h1>Welcome to an example page!</h1>', '<p>This is just another test message and you can choose if you want to delete or modify this message in the admin panel.</p>', 'firstpage', '$time');";
$result = mysql_query($sql) or die (mysql_error());
$result1 = mysql_query($sql1) or die (mysql_error());
$result2 = mysql_query($sql2) or die (mysql_error());
$result3 = mysql_query($sql3) or die (mysql_error());
dus het gaat fout in de sql query omdat er meerdere insert into's zijn. Is het dan zo dat je niet meerdere insert intos in php kan doen maar wel in phpmyadmin? (wat volgens mij in php is gemaakt...)
Wat betekent dat je geen meervoudige sql statements kunt uitvoeren
bij php.net staat:
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
Bedankt! probleem opgelost. (niet echt) in iedergeval ik kan mysqli uitzoeken. Ik weet eindelijk waar het aan ligt.