Jump to content

[SOLVED] Entries not adding to database from form


twilitegxa

Recommended Posts

I deleted my old coding and started over, and now the only error I'm receiving is:

 

Out of range value adjusted for column 'topic_id' at row 1

 

I'm not sure why I get this error, because I used this code in the past with no problems, and now all of a sudden I get it. :-( This is code coming from a tutorial in a book, so it could be out-dated, but last year is was working just fine. Here is the statements I used to create the tables:

 

create table forum_topics (

topic_id int not null primary key auto_increment,

topic_title varchar (150),

topic_create_time datetime,

topic_owner varchar (150)

);

 

create table forum_posts (

post_id int not null primary key auto_increment,

topic_id int not null,

post_text text,

post_create_time datetime,

post_owner varchar (150)

);

 

Since topic_id is the problem, I'm assuming I need to change one of those fields, but I'm not sure which one or how to change it, or what to change it to I should say. Here is the code again, for reference. It appears that it would work if this error was resolved:

 

addtopic.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add A Topic</title>
</head>
<body>
<form method="post" action="do_addtopic.php">
<p><strong>Your E-Mail Address:</strong><br />
<input type="text" name="topic_owner" size="40" maxlength="150" /></p>
<p><strong>Topic Title:</strong><br />
<input type="text" name="topic_title" size="40" maxlength="150" /></p>
<p><strong>Post Text:</strong><br />
<textarea name="post_text" rows="8" cols="40"></textarea></p>
<p><input type="submit" name="submit" value="Add Topic" /></p>
</form>
</body>
</html>

 

do_addtopic.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
//check for required fields from the form
if ((!$_POST["topic_owner"]) || (!$_POST["topic_title"])
|| (!$_POST["post_text"])) {
header("Location: addtopic.html");
exit;
}

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

//create and issue the first query
$add_topic = "insert into forum_topics values ('', '$_POST[topic_title]',
now(), '$_POST[topic_owner]')";
mysql_query($add_topic,$conn) or die(mysql_error());

//get the id of the last query
$topic_id = mysql_insert_id();

//create and issue the second query
$add_post = "insert into forum_posts values ('', '$topic_id',
'$_POST[post_text]', now(), '$_POST[topic_owner]')";
mysql_query($add_post,$conn) or die(mysql_error());

//create nice message for user
$msg = "<P>The <strong>$topic_title</strong> topic has been created.</P>";
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php print $msg; ?>
</body>
</html>

Link to comment
Share on other sites

The version of PHP that the book is using is 4.2.3, so I'm assuming this is a problem that has developed in the newer version of PHP. Can anyone tell me how to fix the problem? I don't want to switch to a lower version of PHP.I'm using version 5.2.9-2.

Link to comment
Share on other sites

Can you post the entire error message?  Both tables have a column of "topic_id"...which makes it hard to figure out what is going on.

 

One small note...you have a doctype at the top of the page, and then you do validation checking with a redirect.  That redirect will fail because you have already sent output to the browser.

 

And, for your queries, try these:

 

$add_topic = "insert into forum_topics topic_title,topic_create_time,topic_owner values
('".$_POST['topic_title']."', now(), '".$_POST['topic_owner']."')";

$add_post = "insert into forum_posts topic_id,post_text,post_create_time,post_owner values
('$topic_id', '".$_POST['post_text']."', now(), '".$_POST['topic_owner']."')";

 

One very important thing to note is that you do not have ' or " around the keys in your $_POST array.  Also, separating the $_POST variables from the string often makes it easier to see.  If that still does not work, if you could then echo both queries so we can see the data, that would help.

Link to comment
Share on other sites

  • 2 weeks later...

Can you post the entire error message?  Both tables have a column of "topic_id"...which makes it hard to figure out what is going on.

 

One small note...you have a doctype at the top of the page, and then you do validation checking with a redirect.  That redirect will fail because you have already sent output to the browser.

 

And, for your queries, try these:

 

$add_topic = "insert into forum_topics topic_title,topic_create_time,topic_owner values
('".$_POST['topic_title']."', now(), '".$_POST['topic_owner']."')";

$add_post = "insert into forum_posts topic_id,post_text,post_create_time,post_owner values
('$topic_id', '".$_POST['post_text']."', now(), '".$_POST['topic_owner']."')";

 

One very important thing to note is that you do not have ' or " around the keys in your $_POST array.  Also, separating the $_POST variables from the string often makes it easier to see.  If that still does not work, if you could then echo both queries so we can see the data, that would help.

 

I changed the code you suggested, but now receive this error:

 

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 'topic_title,topic_create_time,topic_owner values ('Test', now(), 'email@aol.co' at line 1

 

Here is the revised code:

 

do_addtopic.php

<?php
//check for required fields from the form
if ((!$_POST["topic_owner"]) || (!$_POST["topic_title"])
|| (!$_POST["post_text"])) {
header("Location: addtopic.html");
exit;
}

//connect to server and select database
$conn = mysql_connect("mysql5.webdesignsbyliz.com", "twilitegxa", "minimoon")
or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

//create and issue the first query
$add_topic = "insert into forum_topics topic_title,topic_create_time,topic_owner values
('".$_POST['topic_title']."', now(), '".$_POST['topic_owner']."')";
mysql_query($add_topic,$conn) or die(mysql_error());

//get the id of the last query
$topic_id = mysql_insert_id();

//create and issue the second query
$add_post = "insert into forum_posts topic_id,post_text,post_create_time,post_owner values
('$topic_id', '".$_POST['post_text']."', now(), '".$_POST['topic_owner']."')";
mysql_query($add_post,$conn) or die(mysql_error());

//create nice message for user
$msg = "<P>The <strong>$topic_title</strong> topic has been created.</P>";
?>
<html>
<head>
<title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php print $msg; ?>
</body>
</html>

Link to comment
Share on other sites

I added the parentheses, but that didn't seem to correct the problem. I'm still receiving these errors:

 

Notice: Undefined variable: topic_title in C:\wamp\www\do_addtopic.php on line 28

 

The page displays the message that the topic and post have been added, but the table does nto contain the data so the script is not sending the information to the table for some reason. :-(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.