jeliot Posted April 2, 2006 Share Posted April 2, 2006 Here I have a working php form script its nothing to write home about but it works<head><title>Join Mailing List</title></head><?php$db_host = "host name";$db_user = "jeliot";$db_pwd = "password";$db_name = "email";mysql_connect($db_host, $db_user, $db_pwd);mysql_select_db($db_name);if (!isset($_POST['submit'])) {?><form action="" method="post">Name: <input type="text" name="name" size="20"><br>Email: <input type="text" name="email" size="20"><br><input type="submit" name="submit" value="Submit!"></form><?php} else {$name = $_POST['name'];$email = $_POST['email'];$query = "INSERT INTO email_list (name,email) VALUES ('".$name."','".$email."')";$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.}?>Here is my cleaner more organized script if have been woring with <head><title>Join Mailing List</title></head><body><form action="" method="post">Name: <input type="text" name="name" size="20"><br>Email: <input type="text" name="email" size="20"><br><input type="submit" name="submit" value="Submit!"></form><body><?phpif (!isset($_POST['submit'])) {inlclude('./connect.php');$name = $_POST[‘name’];$email = $_POST[‘email’];$query = "INSERT INTO email_list (name, email) VALUES ('$name', '$email' )";$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.}?></head></html>and here is my script to connect to my database<html><head> <title>Untitled</title></head><body><?$db_host = "host name";$db_user = "jeliot";$db_pwd = "password";$db_name = "email";mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());mysql_select_db($db_name) or die(mysql_error());?></body></html>I'm hoping for some different opinions on how i SHOULD be shorting this scipt to clean it up and descriptions on why those changes are betterALL OPINIONS WELCOMETHANKS Quote Link to comment Share on other sites More sharing options...
Desdinova Posted April 2, 2006 Share Posted April 2, 2006 first of all, tab your code.lets pretend '-' is a tab. Do it like this:if ($something){- do this;}if ($somethingtwo){- if ($somethingthree){- - do that;- }}this way you can see what is happening where. I suggest you download crimson editor, and go to options to add another vertical guideline halfway the screen. al the //comments and /* comments */ could be placed right next to it, giving you a short and brief overview.another quick comment, I wouldn't suggest the submit button as an identification as to what process you should run. Somebody hitting enter in your form will not activate the button, thus not POSTing the submit button value. Rather place a hidden field with name submit and value set or something, so you really know what's going on. Unless you have multiple formbuttons with different option. Then you should use the submitname, and preferably disable the enter button. The code itself looks ok. You could be able to connect your database using 2 lines mysql_connect("localhost","username","pass");mysql_select_db("dbname");just 2 cents :)mind the tabbing! You'll need it lateron. I promise. Quote Link to comment Share on other sites More sharing options...
jeliot Posted April 2, 2006 Author Share Posted April 2, 2006 [!--quoteo(post=360910:date=Apr 2 2006, 12:04 PM:name=Desdinova)--][div class=\'quotetop\']QUOTE(Desdinova @ Apr 2 2006, 12:04 PM) [snapback]360910[/snapback][/div][div class=\'quotemain\'][!--quotec--]first of all, tab your code.lets pretend '-' is a tab. Do it like this:if ($something){- do this;}if ($somethingtwo){- if ($somethingthree){- - do that;- }}this way you can see what is happening where. I suggest you download crimson editor, and go to options to add another vertical guideline halfway the screen. al the //comments and /* comments */ could be placed right next to it, giving you a short and brief overview.another quick comment, I wouldn't suggest the submit button as an identification as to what process you should run. Somebody hitting enter in your form will not activate the button, thus not POSTing the submit button value. Rather place a hidden field with name submit and value set or something, so you really know what's going on. Unless you have multiple formbuttons with different option. Then you should use the submitname, and preferably disable the enter button. The code itself looks ok. You could be able to connect your database using 2 lines mysql_connect("localhost","username","pass");mysql_select_db("dbname");just 2 cents :)mind the tabbing! You'll need it lateron. I promise.[/quote]Thanks the tabbing sounds like a good idea and i just downloaded crimson...........i'm still at a loss for why that second scpript i posted isn't working in conjunction with my connect.php script to work like the first script does i must have somthing wrong somewhere........... Quote Link to comment Share on other sites More sharing options...
Desdinova Posted April 2, 2006 Share Posted April 2, 2006 Just keep the .connect with the PHP code. No need to include HTML..connect.php:<?$db_host = "host name";$db_user = "jeliot";$db_pwd = "password";$db_name = "email";mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());mysql_select_db($db_name) or die(mysql_error());?>you have a typo in the code where the script gets included.Other than that, change the include line to:require (".connect.php");require will kill the script when the file can't be found. include will not. Quote Link to comment Share on other sites More sharing options...
sford999 Posted April 2, 2006 Share Posted April 2, 2006 Heres how I would`ve done it[code]<?phprequire ("./connect.php");echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html><head><title>Join Mailing List</title></head><body><form action="" method=\"post\">Name: <input type=\"text\" name=\"name\" size=\"20\"><br />Email: <input type=\"text\" name=\"email\" size=\"20\"><br /><input type=\"submit\" name=\"submit\" value=\"Submit!\" /></form>";if (!isset($_POST["submit"])) { $name = $_POST['name']; $email = $_POST['email']; $query = "INSERT INTO email_list (name, email) VALUES ('".$name.",'".$email."')"; $result = mysql_query($query) or die(mysql_error()); }echo "</head></html>";?>[/code][code]<?php$db_host = "host name";$db_user = "jeliot";$db_pwd = "password";$db_name = "email";mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());mysql_select_db($db_name) or die(mysql_error());?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.