Page 1 of 1

Odd Error

Posted: Fri Oct 20, 2006 1:43 pm
by RobertBr
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am just starting on PHP and have encountered a very odd error (obviously I have encountered lots of errors, but this one is odd). I have a page I have built based around an online tutorial at http://www.kushan.org/php/form.php. It is a single page which displays a form and then posts the information to itself and displays the result. Now the weird thing is I added a section to append the data into a MySQL database. But nothing happens - the form continues to pass the data and redisplay it as before, but no error, and not data gets into the table. The input seems to work when seperated from the rest, so I am at a bit of a loss. Here is the code:

Code: Select all

<?php
//Access the database
include("db.php");

//Variables passed from the post function
$name = $_POST["name"];
$message = $_POST["message"];
$topic = $_POST["topic"];

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Name:<input type="text" size="20" maxlength="20" name="name"><br />
Subject:<input type="text" size="20" maxlength="20" name="topic"><br />
<textarea rows="5" cols="20" name="message" wrap="physical"></textarea><br />

<input type="submit" value="submit" name="submit">
</form>
<?
// if the page is submitted to itself
} else {
echo "You just posted this message<p><b>".$topic."</b><br>".$message."<br>".$name.".<br />";

[b]$sql = 'INSERT INTO `comment` (`postnumber`, `date`, `subject`, `message`, 
`author`, `page`) VALUES (\'1\', \'2006-10-19\', $topic, $message, 
$name, \'http://www.kushan.org/test.php\')'; [/b]
}
?>
I would be very grateful if anyone can see anything obvious.

Thanks,

Robert


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Oct 20, 2006 2:32 pm
by volka
The script builds the sql statement but it doesn't send the query to the server.
see http://de2.php.net/mysql_query

Posted: Fri Oct 20, 2006 5:51 pm
by RobertGonzalez

Code: Select all

<?php
//Access the database
include("db.php");

//Variables passed from the post function
$name = $_POST["name"];
$message = $_POST["message"];
$topic = $_POST["topic"];

 // if page is not submitted to itself echo the form
if (!isset($_POST['submit'])) { ?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Name:<input type="text" size="20" maxlength="20" name="name"><br />
Subject:<input type="text" size="20" maxlength="20" name="topic"><br />
<textarea rows="5" cols="20" name="message" wrap="physical"></textarea><br />

<input type="submit" value="submit" name="submit">
</form>
<?php } else { 
echo 'You just posted this message<p><b>' . $topic . '</b><br>' . $message . '<br>' .  $name . '.<br />';

$sql = "INSERT INTO `comment` (`postnumber`, `date`, `subject`, `message`, `author`, `page`) 
        VALUES ('1', '2006-10-19', $topic, $message, $name, 'http://www.kushan.org/test.php')";
// Now you need to send this query to the database or it will never know you wanted it done...
}
?>

Posted: Tue Oct 24, 2006 9:12 am
by RobertBr
Thanks, problem solved. Next time I will format it better.

Robert