Jump to content

[SOLVED] Entries not adding to database from form


twilitegxa

Recommended Posts

This is what my page displays upon entering data into my form:

 

The $topic_title topic has been created.

 

"; ?>

New Topic Added

 

Here is the php code:

 

<?php
//check for required fields from the form
if ((!$_POST[post_owner]) || (!$_POST[topic_title])
|| (!$_POST[topic_owner])) {
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>
<head>
<title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php print $msg; ?>
</body>
</html>

 

And here is the html code for the form:

 

<html>
<head>
<title>Add A Topic</title>
</head>
<body>
<h1>Add A Topic</h1>
<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" siz=40 maxlength=150 /></p>
<p><strong>Post Text:</strong><br />
<textarea name="post_text" rows=8 cols=40 wrap=virtual></textarea></p>
<p><input type="submit" name="submit" value="Add Topic" /></p>
</form>
</body>
</html>

 

What have I done wrong?

Link to comment
Share on other sites

This line:

 

$msg = "<p>The <strong>$topic_title</strong> topic has been created.</p>";

 

  ----------------------------------------

 

replace with...

 

$msg = "<p>The <strong>" . $topic_title . "</strong> topic has been created.</p>";

 

and the "/" chars may need to be escaped ( ex. <//strong> and <//p> ) , not sure about that, try it and see

 

Edit: In most cases it is not nessesary to escape foreward slashes. But I use that as a debug failsafe and it sometimes works.

 

Link to comment
Share on other sites

Well, I didn't use the escape slashes yet, but the page is not runnign the php page now, and I tried changing to the recommended code and also going back to the original code and neither is allowing my php code to run now. What could be the problem? What causes this to happen?

Link to comment
Share on other sites

if ((!$_POST['post_owner']) || (!$_POST['topic_title'])
|| (!$_POST['topic_owner'])) {
header("Location: addtopic.html");
exit;
}

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

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

 

The post names need ' around them, unless they are constants, which I seriously doubt.

Link to comment
Share on other sites

I added the single quotes around those. I am using WAMPServer on my local computer rather than a host server, also, by the way. I am still not able to run the php page. The form, unpon submission, displays the php code instead of running the code.

Link to comment
Share on other sites

install nginx No install needed actually when you download it its already done.. you just need to run php in command line.

I put PHP folder into nginx folder so my command line is.

 

c:\nginx\php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini

 

you have to save that in a batch file I'd save it in a txt document then rename it to a batch file.. mines is  start php.bat.

 

I use batch file because i dont want to make it into a service since this is only for testing.. but if you want you can make it a service on your hosting computer.

 

It's made by russian and im russian myself so I feel pride.

 

its smaller then apache which is what WAMP is and around 200x-250x faster at loading php pages due to it's proxy support thingy it runs php on a proxy on port 9000 or w\e instead of calling PHP.exe everytime a page is about to show.

 

Only thing I don't like in nginx it doesn't support htaccess files which are for rewrites.. but it does support rewrites only thing u gotta restart server everytime you add or test a new rewrite as it loads it once not always

Link to comment
Share on other sites

Sure. Here is the code with the changes. And, I didn't check that WAMP was working before I changed anything, but even when I changed the code back to the original code (before changes), it still was not working. :-(

 

<?php
//check for required fields from the form
if ((!$_POST['post_owner']) || (!$_POST['topic_title'])
|| (!$_POST['topic_owner'])) {
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>
<head>
<title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php echo $msg; ?>
</body>
</html>

Link to comment
Share on other sites

    I think your server install is at fault, Double check or reinstall. Also make sure that you are typing in the server's correct address and you should make sure it is on your knownhosts file. If you are running vista you have to copy over the file with admin rights or else it will not allow a change to the knownhosts file.

    One more thing, there are lots of other servers available out there, try out a miny, so that no install is necessary and you just have to start up appache.

Link to comment
Share on other sites

Well then obviously your server configuration is broken. This is the wrong forum for such an issue. But the likely cause is that appache has not started the php engine, if it is throwing it directly. You are going to have to either troubleshoot or reinstall.

Link to comment
Share on other sites

Okay, I found out how to view the files. I thought I could view this from the folder, but I have to type in the address: localhost/filename, but now when I submit the form, I receive this error:

 

Parse error: parse error in C:\wamp\www\do_addtopic.php on line 15

 

Can someone tell me what a parse error is and how to fix it as well?

Link to comment
Share on other sites

Here are lines 9-17:

 

//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());

 

Does that help any?

Link to comment
Share on other sites

The first thing I notice is your $add_topic variable:

 

$add_topic = "INSERT INTO forum_topics VALUES ('', '".$_POST['topic_title']."',
now(), '".$_POST['topic_owner']."')";

You just forgot periods to do string concatenation.  I don't know if that will make a difference, though.

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.