[opgelost] wil niet INSERTEN met mysql
Xzero - 11/10/2005 22:12 (laatste wijziging 12/10/2005 00:19)
PHP beginner
heey,
ik zit hier met een probleempje... als ik bij de gastenboek iets wil toevoegen, dan wil hij niet inserten... deze fout krijg ik:
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 'by, email, type, message, date, town, country) VALUES ('', 'Xze
en dit is dan dat stukje van mijn code:
<?PHP
if (count($error) == 0) {
$title = addslashes($_POST['title']);
$name = addslashes($_POST['by']);
$email = $_POST['email'];
$town = addslashes($_POST['town']);
$country = addslashes($_POST['country']);
$message = nl2br($_POST['message']);
$message = eregi_replace("\n", "", $message);
$insert = "INSERT INTO guestbook (title, by, email, type, message, date, town, country) VALUES ('".$title."', '".$name."', '".$email."', 'guest', '".$message."', NOW(), '".$town."', '".$country."')";
mysql_query($insert) or die(mysql_error());
?>
<?PHP
if ( count ( $error ) == 0 ) { $email = $_POST [ 'email' ] ;
$message = nl2br ( $_POST [ 'message' ] ) ;
$insert = "INSERT INTO guestbook (title, by, email, type, message, date, town, country) VALUES ('" . $title . "', '" . $name . "', '" . $email . "', 'guest', '" . $message . "', NOW(), '" . $town . "', '" . $country . "')" ;
?>
19 antwoorden
Gesponsorde links
SvenP - 11/10/2005 22:17
PHP interesse
hoeft daar geen id bij?
iig
$insert = "INSERT INTO `guestbook` (`title`, `by`, `email`, `type`, `message`, `date`, `town`, `country`) VALUES ('$title', '$name', '$email', 'guest', '$message', '"". NOW() ."', '$town', '$country')";
$insert = "INSERT INTO `guestbook` (`title`, `by`, `email`, `type`, `message`, `date`, `town`, `country`) VALUES ('$title ', '$name ', '$email ', 'guest', '$message ', '" ". NOW() ." ', ' $town ', ' $country ')";
Xzero - 11/10/2005 22:26
PHP beginner
id is auto_increment en is primary key, dus die moet ie vanzelf toevoegen... maar alsnog werkt het niet...
Xzero - 11/10/2005 22:43
PHP beginner
en alsnog werkt ie niet...
ik heb nu dit:
<?PHP
if (isset($_POST['add'])) {
$error = array();
if (empty($_POST['name'])) {
$error[] = "You have to fill in your name.";
} elseif (empty($_POST['email']) || !eregi('@',$_POST['email']) || !eregi('.',$_POST['email']) || eregi(' ',$_POST['email'])) {
$error[] = "You have to fill in your correct e-mail address.";
} elseif (empty($_POST['message'])) {
$error[] = "You have to post a message.";
}
if (count($error) == 0) {
$title = addslashes($_POST['title']);
$name = addslashes($_POST['name']);
$email = $_POST['email'];
$town = addslashes($_POST['town']);
$country = addslashes($_POST['country']);
$message = nl2br($_POST['message']);
$message = eregi_replace("\n", "", $message);
$insert = "INSERT INTO guestbook (title, whom, email, typeposter, message, datewhen, town, country) VALUES ('$title', '$name', '$email', 'guest', '$message', '".NOW()."', '$town', '$country')";
mysql_query($insert) or die(mysql_error());
?>
GELUKT
<?PHP
} else {
?>
IETS NIET INGEVULD
<?PHP
}
} else {
?>
FORM
<?PHP
}
?>
<?PHP
if ( isset ( $_POST [ 'add' ] ) ) {
if ( empty ( $_POST [ 'name' ] ) ) { $error [ ] = "You have to fill in your name." ;
} elseif ( empty ( $_POST [ 'email' ] ) || ! eregi ( '@' , $_POST [ 'email' ] ) || ! eregi ( '.' , $_POST [ 'email' ] ) || eregi ( ' ' , $_POST [ 'email' ] ) ) { $error [ ] = "You have to fill in your correct e-mail address." ;
} elseif ( empty ( $_POST [ 'message' ] ) ) { $error [ ] = "You have to post a message." ;
}
if ( count ( $error ) == 0 ) { $email = $_POST [ 'email' ] ;
$message = nl2br ( $_POST [ 'message' ] ) ;
$insert = "INSERT INTO guestbook (title, whom, email, typeposter, message, datewhen, town, country) VALUES ('$title ', '$name ', '$email ', 'guest', '$message ', '" . NOW( ) . "', '$town ', '$country ')" ;
?>
GELUKT
<?PHP
} else {
?>
IETS NIET INGEVULD
<?PHP
}
} else {
?>
FORM
<?PHP
}
?>
ik zie zelf echt geen fout hierin... mijn database structuur heb ik ook verandert (andere name gegeven die daar niet in voorkomen..)
Thomas - 11/10/2005 23:01
Moderator
NOW() is een MySQL-functie, geen PHP-functie.
Is je kolom datewhen van het type DATETIME?
Xzero - 11/10/2005 23:04 (laatste wijziging 11/10/2005 23:06)
PHP beginner
ja... het is DATETIME
hier is mn sql geval:
CREATE TABLE guestbook(
id INT(5) not null AUTO_INCREMENT,
title VARCHAR(100) not null DEFAULT '',
whom VARCHAR(50) not null,
email VARCHAR(50) not null,
typeposter VARCHAR(10) not null,
message TEXT not null,
date DATETIME not null,
town VARCHAR(50) not null DEFAULT '',
country VARCHAR(50) not null DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY (id)
)
CREATE TABLE guestbook(
id INT( 5 ) not null AUTO_INCREMENT,
title VARCHAR( 100 ) not null DEFAULT '' ,
whom VARCHAR( 50 ) not null ,
email VARCHAR( 50 ) not null ,
typeposter VARCHAR( 10 ) not null ,
message TEXT not null ,
town VARCHAR( 50 ) not null DEFAULT '' ,
country VARCHAR( 50 ) not null DEFAULT '' ,
)
owja, ff vermelden dat ik die datewhen tog nog in date heb verandert, want ze werken allebei niet..
Gerard - 11/10/2005 23:18
Ouwe rakker
Je gehele query in een varabiele stoppen voordat je hem gaat uitvoeren. Deze kan je dan echoën zodat je de inhoud ziet.
<?php
$qQuery = "SELECT * FROM tabel";
echo $qQuery;
$rSomething = mysql_query($qQuery);
?>
<?php
$qQuery = "SELECT * FROM tabel" ;
?>
Xzero - 11/10/2005 23:21
PHP beginner
waarom is dit nodig, en moet dat in een aparte document of gewoon in dezelfde waar ik INSERT heb staan...
Gerard - 11/10/2005 23:24
Ouwe rakker
Hetzelfde, zo kan je zien of je variabelen wel goed meegegeven worden in de query en hoe je query er dus precies uit ziet wanneer hij naar de dbms wordt verzonden.
Thomas - 11/10/2005 23:24 (laatste wijziging 11/10/2005 23:26)
Moderator
Dat heet "debuggen" en kan in dit geval wel handig zijn.
Desnoods doe je die($query); voordat je de query daadwerkelijk uitvoert. Als je maar ziet wat je query doet voordat je het in je db probeert te wurmen (wat blijkbaar niet lukt).
Xzero - 11/10/2005 23:28 (laatste wijziging 11/10/2005 23:32)
PHP beginner
Ok ik heb nu dit, ik weet niet of het echt klopt
<?PHP
if (count($error) == "0") {
$title = addslashes($_POST['title']);
$name = addslashes($_POST['name']);
$email = $_POST['email'];
$town = addslashes($_POST['town']);
$country = addslashes($_POST['country']);
$message = nl2br($_POST['message']);
$message = eregi_replace("\n", "", $message);
$qQuery = "SELECT * FROM guestbook";
echo $qQuery;
$rSomething = mysql_query($qQuery);
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('".$title."', '".$name."', '".$email."', guest, '".$message."', '".NOW()."', '".$town."', '".$country."')";
mysql_query($insert) or die(mysql_error());
?>
<?PHP
if ( count ( $error ) == "0" ) { $email = $_POST [ 'email' ] ;
$message = nl2br ( $_POST [ 'message' ] ) ;
$qQuery = "SELECT * FROM guestbook" ;
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('" . $title . "', '" . $name . "', '" . $email . "', guest, '" . $message . "', '" . NOW( ) . "', '" . $town . "', '" . $country . "')" ;
?>
en als u (Fangorn) dit over zou kunnen overnemen en neerzetten waar die die($query) moet, dan zou ik dat echt waarderen
EDIT: Trouwens, mijn resultaat als ik op submit klik, dan krijg ik SELECT * FROM guestbook te zien...
Gerard - 11/10/2005 23:32 (laatste wijziging 11/10/2005 23:32)
Ouwe rakker
<?PHP
if (count($error) == "0") {
$title = addslashes($_POST['title']);
$name = addslashes($_POST['name']);
$email = $_POST['email'];
$town = addslashes($_POST['town']);
$country = addslashes($_POST['country']);
$message = nl2br($_POST['message']);
$message = eregi_replace("\n", "", $message);
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('".$title."', '".$name."', '".$email."', 'guest', '".$message."', 'NOW()', '".$town."', '".$country."')";
echo $insert;
mysql_query($insert) or die(mysql_error());
?>
<?PHP
if ( count ( $error ) == "0" ) { $email = $_POST [ 'email' ] ;
$message = nl2br ( $_POST [ 'message' ] ) ;
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('" . $title . "', '" . $name . "', '" . $email . "', 'guest', '" . $message . "', 'NOW()', '" . $town . "', '" . $country . "')" ;
?>
Gerard - 11/10/2005 23:50
Ouwe rakker
"het klopt niet".... wat klopt niet? is het niet de juiste datum? loopt de tijd achter?
Thomas - 12/10/2005 07:42
Moderator
Mja wat was de fout nu (leermoment voor de rest)?
Xzero - 15/10/2005 17:32 (laatste wijziging 15/10/2005 17:51)
PHP beginner
Ik had dit:
<?PHP
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('".$title."', '".$name."', '".$email."', 'guest', '".$message."', 'NOW()', '".$town."', '".$country."')";
?>
<?PHP
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('" . $title . "', '" . $name . "', '" . $email . "', 'guest', '" . $message . "', 'NOW()', '" . $town . "', '" . $country . "')" ;
?>
maar die 'NOW()' moest zonder die '... dus NOW() gewoon
Tijdens het debuggen:
<?PHP
if (count($error) == "0") {
$title = addslashes($_POST['title']);
$name = addslashes($_POST['name']);
$email = $_POST['email'];
$town = addslashes($_POST['town']);
$country = addslashes($_POST['country']);
$message = nl2br($_POST['message']);
$message = eregi_replace("\n", "", $message);
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('".$title."', '".$name."', '".$email."', 'guest', '".$message."', 'NOW()', '".$town."', '".$country."')";
echo $insert;
mysql_query($insert) or die(mysql_error());
?>
<?PHP
if ( count ( $error ) == "0" ) { $email = $_POST [ 'email' ] ;
$message = nl2br ( $_POST [ 'message' ] ) ;
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('" . $title . "', '" . $name . "', '" . $email . "', 'guest', '" . $message . "', 'NOW()', '" . $town . "', '" . $country . "')" ;
?>
Na het debuggen:
<?PHP
if (count($error) == "0") {
$title = addslashes($_POST['title']);
$name = addslashes($_POST['name']);
$email = $_POST['email'];
$town = addslashes($_POST['town']);
$country = addslashes($_POST['country']);
$message = nl2br($_POST['message']);
$message = eregi_replace("\n", "", $message);
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('".$title."', '".$name."', '".$email."', 'guest', '".$message."', NOW(), '".$town."', '".$country."')";
mysql_query($insert) or die(mysql_error());
?>
<?PHP
if ( count ( $error ) == "0" ) { $email = $_POST [ 'email' ] ;
$message = nl2br ( $_POST [ 'message' ] ) ;
$insert = "INSERT INTO `guestbook`(`title`, `whom`, `email`, `typeposter`, `message`, `date`, `town`, `country`) VALUES ('" . $title . "', '" . $name . "', '" . $email . "', 'guest', '" . $message . "', NOW(), '" . $town . "', '" . $country . "')" ;
?>
Gesponsorde links
Dit onderwerp is gesloten .