prcollin Posted July 14, 2007 Share Posted July 14, 2007 i have done everything right and i cant get my form to post to my database can someone post me their working connect script to a mysql database hosted by phpmyadmin Quote Link to comment https://forums.phpfreaks.com/topic/59955-ahhhhh/ Share on other sites More sharing options...
pocobueno1388 Posted July 14, 2007 Share Posted July 14, 2007 <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> That was straight from: http://www.php.net/function.mysql-connect More helpful sites: http://www.w3schools.com/php/php_mysql_connect.asp http://www.databasejournal.com/features/mysql/article.php/1469211 Quote Link to comment https://forums.phpfreaks.com/topic/59955-ahhhhh/#findComment-298150 Share on other sites More sharing options...
prcollin Posted July 14, 2007 Author Share Posted July 14, 2007 Still wont connect to my database or table to post the information. I dont kon wwhat else to do. ok i created the connect.php file here it is <?php $hostname="localhost"; $username="prc****_******"; $password="n*******"; $dbname="pr******ers"; $usertable="sign_up"; $dbh=mysql_connect ("localhost", "prcblog_prcollin", "nikolas17") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("prcblog_newcustomers"); ?> and here is the code for my form <?php include "connect.php"; include "connect.css"; //first check to see if user submits if (!isset($_POST[submit])) { //if the submit button is not pushed, show the form $show_form = “yes”; } elseif (isset($_POST[submit])) { //if the submit button is pushed, check errors if (empty($_POST[username])) { ; //show the form $show_form = “yes”; //…and so on with the errors } else { //after no more error checks, if user passes all $show_success = “yes”; } //end if } //end if ->you can put if statements inside if statements //if show form is true …then display it if ($show_form == “yes”) { $display = "<center>\n"; $display .= "$error\n"; $display .= "<form action = \"$_SERVER[php_SELF]\" method = \"post\">\n"; $display .= "<table border = >\n"; $display .= "<td align = \"right\">Company Name:</td>\n"; $display .= "<td><input type = \"text\" name = \"company_name\" size = 20 maxlength = 30></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Address:</td>\n"; $display .= "<td><input type = \"text\" name = \"address\" size = 20 maxlength = 64></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">City:</td>\n"; $display .= "<td><input type = \"text\" name = \"city\" size = 20 maxlength = 50></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">State:</td>\n"; $display .= "<td><input type = \"text\" name = \"state\" size = 20 maxlength = 30></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Zip Code:</td>\n"; $display .= "<td><input type = \"text\" name = \"zip_code\" size = 20 maxlength = 30></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Phone Number:</td>\n"; $display .= "<td><input type = \"text\" name = \"phone\" size = 20 maxlength = 30></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Fax:</td>\n"; $display .= "<td><input type = \"text\" name = \"fax\" size = 20 maxlength = 30></td>\n"; $display .= "</tr>\n"; $display .= "<td align = \"right\">E-mail Address:</td>\n"; $display .= "<td><input type = \"text\" name = \"email\" size = 20 maxlength = 30></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Chose a User Name:</td>\n"; $display .= "<td><input type = \"text\" name = \"user_name\" size = 20 maxlength = 30></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Choose A Password:</td>\n"; $display .= "<td><input type = \"password\" name = \"password\" size = 20 maxlength = 30></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Re-Type Password:</td>\n"; $display .= "<td><input type = \"password\" name = \"pass2\" size = 20 maxlength = 64></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Type A Security Question:</td>\n"; $display .= "<td><input type = \"text\" name = \"security_question\" size = 20 maxlength = 64></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Type A Security Question Answer:</td>\n"; $display .= "<td><input type = \"text\" name = \"security_questionans\" size = 20 maxlength = 100></td>\n"; $display .= "</tr>\n\n"; $display .= "<td align = \"right\">Please Choose a Subdomain Name:</td>\n"; $display .= "<td><input type = \"text\" name = \"subdomain_choice\" size = 20 maxlength = 30></td>\n"; $display .= "<td><input type = \"submit\" name = \"submit\" value = \"Register\"></td>\n"; $display .= "</tr>\n"; $display .= "</table>\n"; $display .= "</form>\n"; $display .= "</center>\n"; } //if if ($show_success == “yes”) { $company_name = ($_POST[company_name]); $address = ($_POST[address]); $city = ($_POST[city]); $state = ($_POST[state]); $zip_code = ($_POST[zip_code]); $phone = ($_POST[phone]); $fax = ($_POST[fax]); $email = ($_POST[email]); $user_name = ($_POST[user_name]); $password = ($_POST[password]); $pass2 = ($_POST[pass2]); $security_question = ($_POST[sercurity_question]); $security_questionans = ($_POST[security_questionans]); $subdomain_choice = ($_POST[subdomain_choice]); $query = mysql_query("INSERT * INTO sign_up (companyName, address, city, state, zipeCode, phone, fax, eMail, userName, passWord, retypePassword, securityQuestion, subDomain) VALUES ('$company_name', '$address', '$city', '$state', '$zip_code', '$phone', '$fax', '$email', '$user_name', '$password', '$pass2', '$security_question', '$security_questionans', '$subdomain_choice')")or die(mysql_error()); $result = mysql_query($query) or die(mysql_error()); //then display a success message ; } //end if ?> <html> <head> <title>New Customer Sign Up Form</title> </head> <body> <h3>New Customer Sign Up Form</h3> <p><?php print "$display"; ?></p> </body> </html> Please someoen tell me what i am doing wrong Quote Link to comment https://forums.phpfreaks.com/topic/59955-ahhhhh/#findComment-298192 Share on other sites More sharing options...
prcollin Posted July 14, 2007 Author Share Posted July 14, 2007 ok this line was in my code "read above" $display .= "<form action = \"$_SERVER[php_SELF]\" method = \"post\">\n"; is there another code i should have used to post to a database, instead of this one. Quote Link to comment https://forums.phpfreaks.com/topic/59955-ahhhhh/#findComment-298215 Share on other sites More sharing options...
KevinM1 Posted July 14, 2007 Share Posted July 14, 2007 You're running mysql_query twice. You should have: ??php $query = "INSERT INTO....*; $result = mysql_query($query); ?> I'm not sure if that will cure all your problems, but it'll help. Quote Link to comment https://forums.phpfreaks.com/topic/59955-ahhhhh/#findComment-298221 Share on other sites More sharing options...
redarrow Posted July 14, 2007 Share Posted July 14, 2007 lookup heredoc http://uk3.php.net/language.types.string also at minuim use addslashes or mysql_reel_escape_string(); function. <?php $url=$_SERVER['PHP_SELF']; echo<<<form <form method="POST" action="$url"> ect ect ect </form> form; ?> Quote Link to comment https://forums.phpfreaks.com/topic/59955-ahhhhh/#findComment-298224 Share on other sites More sharing options...
wildteen88 Posted July 14, 2007 Share Posted July 14, 2007 What do you get when you run the script? A blank page or an error. It helps if you explain what is happening when you run the script and what is supposed to happen. Also I have seen a few problems with your code. I saq this line: include "connect.css"; What is that file for? Is it for a stylesheet? if it is why are you including a Stylesheet with PHP? You should use the link HTML tag for applying a stylesheet to an HTML document not PHP. Unless you are using PHP to generate dynamic CSS. Another problem I see is you don't wrap associative array indices in quotes. You should wrap associative array indices within quotes otherwise PHP will assume you are using a constant. For example: $_POST[submit] should be $_POST['submit']. Notice the quotes between the square brackets. Another problem I can see is you use the wrong style of quote when defining strings, eg you use “ instead of ". PHP only accepts " (double) or ' (single) quote characters for defining strings. Also when outputting large amount of HTML go for the HEREDOC Syntax. This allows you to then write strings without having to escape quotes. Variables can also be used too with HEREDOC. Quote Link to comment https://forums.phpfreaks.com/topic/59955-ahhhhh/#findComment-298231 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.