Jump to content

php post


zetcoby

Recommended Posts

hello, i have a small problem with this code


<html>

<hr>
<?php
if (isset($_POST['post']))
{
	$title = $_POST['title'];
	$body = $_POST['body'];
	if($title&&$body)
		{
			$connect= mysql_connect ("localhost","root","951623") or die ("error");
			mysql_select_db ("phplogin")  or die("table not found"); 
			$date = date("Y-m-d");
			$insert = mysql_query(" INSERT INTO news VALUES ('','$title','$body','$date')");
		}
	else
		{
			echo " Va rog umpleti toate casutele";
		}
}
?>
<form action='post.php'method='POST'>
Titlul:<br>
<input type="text" name='title'><p>

Tutorial:<br>
<textarea rows='6' cols='70' name='body'></textarea><p>

<input type="submit" name="post" value="Post">
</form>
<hr>
</html>

 

The problem is that the post action will not even appear on the site, i log on my site and no thing happens, no post action there, can someone tell me  is there something wrong with my code?

Link to comment
Share on other sites

Ok, first thing I noticed when I saw it is;

 


if($title&&$body)

 

You have two variables but you're not comparing them to anything. It's like saying if apple and apple return this. But if apple and apple are what? You haven't specified the what. I'm thinking that it is making the php stop and die there. When php performs die() it stops anything else on the page from appearing. Since you haven't written anything else to the page before that you won't see if it's echoing anything.

 

Now, if that isn't the problem, this might be.

 


mysql_select_db ("phplogin")  or die("table not found"); 

 

Databases are case sensitive and therefore if you have got any letter in the wrong case it wouldn't work. But saying that, if it didn't work it should say "table not found" as you have specified. I'll give you two tips here.

 

First, put something just under the html tag and before the php tag and see if it shows that.

 

Secondly, I use this method of connecting to a database. I find it easier and it looks better in my mind.

 

<?php

                                                                                $myServer = "server";  
									$myUser = "username";
									$myPass = "password";
									$myDB = "database";

									//connection to the database
									$dbhandle = mysql_connect($myServer, $myUser, $myPass)
									  or die("Couldn't connect to SQL Server on $myServer"); 

									//select a database to work with
									$selected = mysql_select_db($myDB, $dbhandle)
									  or die("Couldn't open database $myDB"); 
?>

 

 

Try correcting the things I have mentioned and see if that works. =]

 

Jacob.

Link to comment
Share on other sites

@Jacbey: it depends on how he's using it. He could be checking for a "true" boolean value, or to see if it exists. I suspect he's not though so @OP, maybe you can clarify?

 

Take this example:

if ($title) {
echo "Set or true";
} else {
echo "Not set or not true";
}

outputs "Not set or not true"

 

Yet this:

$title = "anything";
if ($title) {
echo "Set or true";
} else {
echo "Not set or not true";
}

will output "set or true"

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.