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?

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.

 

What makes my php page not show sometimes? When I open and submit my form, instead of running the php page, it is now displaying the code instead. What makes that happen and how can I fix it??

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?

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.

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.

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

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>

    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.

I am running XP. I have a host, but it is suspended until I pay the monthly fee. What is a miny? It is strange because my server was working installation, just doesn't work some days for some reason.

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.

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?

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.