Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax help please

Status
Not open for further replies.

madanthrax

IS-IT--Management
Sep 15, 2001
123
AT
I am changing over to PHP/MySQL/Apache from ASP. I am just going through all the stuff in basic form that I will need for my first PHP app and have already wasted an incredible amount of time on an insert to DB page. I feel really stupid, but I just can't find the right combination for the if statement. The script below at least loads (I know it cant be right) but when the form submit button reloads the page the_POST[action] data is not activating the insert code. Any changes I make to the syntax result in a blank page. The insert code on its own works fine.

Your help can speed me on my way to freedom from MS.....

Code:
<?php virtual('/connections/PHPvirtual.php'); ?>
<?php
if ('$_POST[action]' == "insert")
{
mysql_select_db($database_PHPvirtual) or die( "Unable to find database");
$sql = "INSERT INTO Users (FirstName, LastName, Email) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[email]')";
mysql_query($sql);
mysql_close();
}
else
{
echo "Post info not found<br />";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Basic insert to db</title>
</head>
<body>
INSERT Record<br />
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" /><br />
Lastname: <input type="text" name="lastname" /><br />
Email: <input type="text" name="email" /><br />
<input name="action" type="hidden" value="insert" />
<input type="submit" value="Enter"/>
</form>
 </body>
</html>

Anthony.

[sub]&quot;Nothing is impossible until proven otherwise&quot;[/sub]​
 
Code:
<?php virtual('/connections/PHPvirtual.php'); ?>
<?php
if ($_POST[[red]'[/red]action[red]'[/red]] == "insert") [green]\\quotes go around the $_POST variable key not around the POST variable itself.[/green]


The rest seems to be o.k.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for that, I tried it but it gives me a totally blank page..... There must be something else wrong.

For information: I am running 2.2 Apache with 5.2 PHP on an XP notebook, in case this is relevant.

[sub]&quot;Nothing is impossible until proven otherwise&quot;[/sub]​
 
Sorry, did not check all the way through, you can't use the $_POST value for your IF statement, because it might not be there if the form in not submitted. This will generate an error similar to this:

Notice: Undefined index: action in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\insert.php on line 2
Post info not found

Since you are not getting it, it tells me you need to modify your PHP.ini file. Since you are developing, and learning, I suggest you set error_reporting to E_ALL so that all errors warning and notices are displayed, and you know what is wrong.

Now to solve your problem, instead of checking if the $_POST['action'] is equal to something, just check that it exist, if it doesn't, then the form has not been submitted, if it does, then you can process the form.

Code:
if ([red]isset[/red]($_POST['action'])){
...
}


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Your SQL querys string has also had single quotes incorectly configured

you have:

$sql = "INSERT INTO Users (FirstName, LastName, Email) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST')";

I would try something like

$sql = "INSERT INTO Users (FirstName, LastName, Email) VALUES (".$_POST['firstname'].",".$_POST['lastname'].",".$_POST['email'].")";
 
Thanks all, that fixed it.

There is really lots of online documentation etc for PHP, which is great, but sometimes sites don't seem to agree exactly.

[sub]&quot;Nothing is impossible until proven otherwise&quot;[/sub]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top