Jump to content

zimmo

Members
  • Posts

    171
  • Joined

  • Last visited

Everything posted by zimmo

  1. Oni its not giving me any errors now, but it is stating that the username is taken, even though I have emptied the tables so nothing is there now, it just wont register now?
  2. Doh... sorry, yes I just missed that. I normally do us php not just the ?. Thanks for pointing that out. Sorry I forgot to add about the function. The function I posted up, is actually held within an external file and called from the header using an include. The actual page that calls the form has the following code, but it does not work, it gives me no error, but it does not actually perform the sql query? it just tries to insert a duplicate. Here is the code calling the function: if ( empty($username) ) { $error['username_error'] = '<div class="formerror">Please enter your username</div>'; } elseif (!validUsername ($username) ) { $error['username_error'] = '<div class="formerror">Username taken</div>'; }
  3. Hi Can you tell me where? I have not heard of this, still learning. Thanks
  4. I am learning how to use functions, and they are a great. I am unsure of how to perform an sql query in a function. It is checking to see if a username is taken. Can this be done with a function? Here is the query, but it is failing when called. <? function validUsername($username) { $sql = "SELECT * FROM tableA WHERE username = '$_POST[username]' "; $sql_result = mysql_query($sql); if (mysql_num_rows($sql_result) !=0) { $isValid = true; } else { $isValid = false; } return $isValid; } ?>
  5. Have been reading through several articles now whilst seeing if anyone can help. I am reading that you have to use hidden fields or the like to call the function on FORMS. Is this true? How should I call this function, I am totally stuck as how to get it to work
  6. Sorry to bother people, I am just learning and hope someone out there can help me. I just want to understand how to call the function and get it to work with the form?
  7. Thanks for all the help. I am going to use the function that was suggestion as seems to do what I want. Now I have not had much dealings with functions, and something I am really trying to understand. So, if someone could show me here, It will not only help BUT learn me how they actually work. I find it so much easier doing it in real world apps, rather than tutorials.. you can grasp it better. So I have my error check end then move on to the insert sql, but I need to check the email is a valid one (or is at least genuine) and then tell me. if ( empty($email) ) { $error['email_error'] = '<div class="formerror">Please enter your email</div>'; } // End of error checking, all fields covered. if (!$error) { function validEmail($email) { $isValid = true; $atIndex = strrpos($email, "@"); if (is_bool($atIndex) && !$atIndex) { $isValid = false; } else { $domain = substr($email, $atIndex+1); $local = substr($email, 0, $atIndex); $localLen = strlen($local); $domainLen = strlen($domain); if ($localLen < 1 || $localLen > 64) { // local part length exceeded $isValid = false; } else if ($domainLen < 1 || $domainLen > 255) { // domain part length exceeded $isValid = false; } else if ($local[0] == '.' || $local[$localLen-1] == '.') { // local part starts or ends with '.' $isValid = false; } else if (preg_match('/\\.\\./', $local)) { // local part has two consecutive dots $isValid = false; } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { // character not valid in domain part $isValid = false; } else if (preg_match('/\\.\\./', $domain)) { // domain part has two consecutive dots $isValid = false; } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { // character not valid in local part unless // local part is quoted if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $isValid = false; } } if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { // domain not found in DNS $isValid = false; } } return $isValid; } $sql = "SELECT * FROM tablea WHERE username = '$_POST[username]' "; $sql_result = mysql_query($sql); if (mysql_num_rows($sql_result) !=0) etc.. etc.. What I dont understand/trying to learn is after it has executed the function how to I return the value? or how to I get it to work with the form. Here is the form field html extract <p><label for="email">Email</label> <input class="long" type="text" value="<?php echo $email; ?>" name="email" id="email" /><br><?php echo $error['email_error']; ?></p> Appreciate help as learning...
  8. Thanks. The thing is I need to also check that it is not blank? So that they actually enter something.
  9. I have been playing with the validation and extending what I have got already. I am having problems with it not executing the code I have added, it is not checking the email field for a valid domain. Here is the snippet of code for the error check and my extended with the email check. if ( empty($email) ) { $error['email_error'] = '<div class="formerror">Please enter your email</div>'; } else { function validate_email($email) { // Create the syntactical validation regular expression $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"; // Presume that the email is invalid $valid = 0; // Validate the syntax if (eregi($regexp, $email)) { list($username,$domaintld) = split("@",$email); // Validate the domain if (getmxrr($domaintld,$mxrecords)) $valid = 1; } else { $valid = 0; } return $valid; } } Also the query that comes after that. I think this is where I need to add something for the function to validate the email address. I have just included the snippet again. // End of error checking, all fields covered. if (!$error) { $sql = "SELECT * FROM tablea WHERE username = '$_POST[username]' "; $sql_result = mysql_query($sql);
  10. Thanks guys. The problem is like you say the variation of different email addresses, and almost impossible to sort. Lets say that I want to just query a few types, what would be the best way? If anyone knows of any links to help me locate things better, more than welcome.
  11. I currently have a standard error check on my form for registration. I have read too many conflictions to do with validating the email address to make sure it is valid. Could someone either help me with this, or point me in the right direction to the best way to find out. I need to extend this to validate the email they are inputting. here is the current code if ( empty($email) ) { $error['email_error'] = '<div class="formerror">Please enter your email</div>'; } As you can see its very basic and just checks the field is not empty.
  12. I am not sure if my mysql query is correct. If you look at the code below you will see. I have never done this kind of update before and its exactly what I need. $SQL = " INSERT INTO fishery_b_details (fishery_id, venue_name, primary_contact, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES (fishery_id = '$_SESSION[fishery_id]', venue_name = '$_SESSION[venue_name]', primary_contact = '$_POST[primary_contact]', address_1 = '$_POST[address_1]', address_2 = '$_POST[address_2]', address_3 = '$_POST[address_3]', town = '$_POST[town]', county = '$_POST[county]', postcode = '$_POST[postcode]', email = '$_POST[email]', telephone_1 = '$_POST[telephone_1]', telephone_2 = '$_POST[telephone_2]', fax = '$_POST[fax]') ON DUPLICATE KEY UPDATE fishery_id = '$_SESSION[fishery_id]', venue_name = '$_SESSION[venue_name]', primary_contact = '$_POST[primary_contact]', address_1 = '$_POST[address_1]', address_2 = '$_POST[address_2]', address_3 = '$_POST[address_3]', town = '$_POST[town]', county = '$_POST[county]', postcode = '$_POST[postcode]', email = '$_POST[email]', telephone_1 = '$_POST[telephone_1]', telephone_2 = '$_POST[telephone_2]', fax = '$_POST[fax]' ";
  13. I checked and it is inserting the fishery id into all the fields I am entering data into. Its not inserting the id into the correct field or the venue name. I checked and they were not in the query so I changed it but still the same. Also, if I then resbmit the form and enter the data it actually submits.?
  14. I have worked this out by placing the html inside: <? ob_start(); ?> <html> <? echo ob_get_clean(); ?> So have managed to get it to work. Now I have issues with the data being inserted, I am using mysql version 5.0.32 which I understand supports the INSERT INTO and ON DUPLICATE KEY UPDATE. My data is not updating correctly at all. When I checked in most fields it is inserting the number 1 (this is the id number) and not the data from the form. The code I have for the query is: $SQL = " INSERT INTO fishery_b_details (primary_contact, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES (primary_contact = '$_POST[primary_contact]', address_1 = '$_POST[address_1]', address_2 = '$_POST[address_2]', address_3 = '$_POST[address_3]', town = '$_POST[town]', county = '$_POST[county]', postcode = '$_POST[postcode]', email = '$_POST[email]', telephone_1 = '$_POST[telephone_1]', telephone_2 = '$_POST[telephone_2]', fax = '$_POST[fax]') ON DUPLICATE KEY UPDATE primary_contact = '$_POST[primary_contact]', address_1 = '$_POST[address_1]', address_2 = '$_POST[address_2]', address_3 = '$_POST[address_3]', town = '$_POST[town]', county = '$_POST[county]', postcode = '$_POST[postcode]', email = '$_POST[email]', telephone_1 = '$_POST[telephone_1]', telephone_2 = '$_POST[telephone_2]', fax = '$_POST[fax]' "; The fishery_id in the database is a primary key.
  15. I have a problem with my script. I have been modifying it to get it to work but without success. I am not sure of the best way to do this. I need to query the database to see if something exists, if it does to enter the data into the form fields, if it does not to just show the blank form. Then I need it to process the form. As you can see I have changed the form as I am using mysql 5 so doing a duplicate query. Here is how I have it set up at present <?php session_start(); if (!(isset($_SESSION['username']) && $_SESSION['username'] != '')) { header ("Location: fishery_login.html"); exit; } // Include the connections script to make a database connection. include("inc/connect.php"); if ( $_POST['submit'] ) { $primary_contact = $_POST['primary_contact']; $address_1 = $_POST['address_1']; $address_2 = $_POST['address_2']; $address_3 = $_POST['address_3']; $town = $_POST['town']; $county = $_POST['county']; $postcode = $_POST['postcode']; $email = $_POST['email']; $telephone_1 = $_POST['telephone_1']; $telephone_2 = $_POST['telephone_2']; $fax = $_POST['fax']; if ( empty($primary_contact) ) { $error['primary_contact_error'] = '<div class="formerror">Please enter your Primary Contact Name.</div>'; } if ( empty($town) ) { $error['town_error'] = '<div class="formerror">Please Enter your Town/City.</div>'; } if ( empty($county) ) { $error['county_error'] = '<div class="formerror">Please Select your County.</div>'; } if ( empty($postcode) ) { $error['postcode_error'] = '<div class="formerror">Please enter your Postcode.</div>'; } if ( empty($telephone_1) ) { $error['telephone_1_error'] = '<div class="formerror">Please enter your Main Telephone Number.</div>'; } if (!$error) { # setup SQL statement $SQL = " INSERT INTO fishery_b_details (primary_contact, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES (primary_contact = '$_POST[primary_contact]', address_1 = '$_POST[address_1]', address_2 = '$_POST[address_2]', address_3 = '$_POST[address_3]', town = '$_POST[town]', county = '$_POST[county]', postcode = '$_POST[postcode]', email = '$_POST[email]', telephone_1 = '$_POST[telephone_1]', telephone_2 = '$_POST[telephone_2]', fax = '$_POST[fax]') ON DUPLICATE KEY UPDATE primary_contact = '$_POST[primary_contact]', address_1 = '$_POST[address_1]', address_2 = '$_POST[address_2]', address_3 = '$_POST[address_3]', town = '$_POST[town]', county = '$_POST[county]', postcode = '$_POST[postcode]', email = '$_POST[email]', telephone_1 = '$_POST[telephone_1]', telephone_2 = '$_POST[telephone_2]', fax = '$_POST[fax]' "; #execute SQL statement $result = mysql_db_query( *****,"$SQL",$connection ); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } else { header("Location: http://www.*****.com/development/fisheries_admin/fishery_details_view.html"); } } } ?> Then within my form I have the following: <? $sql = "SELECT * FROM fishery_b_details WHERE fishery_id = '$_SESSION[fishery_id]' "; $sql_result = mysql_query($sql); if (mysql_num_rows($sql_result) >0) { echo ("\n"); } else { while ($row = mysql_fetch_array($sql_result)){ $primary_contact = $row["primary_contact"]; $address_1 = $row["address_1"]; $address_2 = $row["address_2"]; $address_3 = $row["address_3"]; $town = $row["town"]; $county = $row["county"]; $postcode = $row["postcode"]; $email = $row["email"]; $telephone_1 = $row["telephone_1"]; $telephone_2 = $row["telephone_2"]; $fax = $row["fax"]; ?> As you can see the echo ("\n"); is causing the form to not display. And without me putting the whole form in that echo it skips it. Is there an easier way?
  16. Thanks for peoples assistance todate. I am moving forward thanks to this great site. I am not sure where this has gone wrong, I know it will be something in the wrong place. I am performing a check first to see if entry exists, if it does not then to insert, if it does then update. There is no entry at the moment, and when I click submit it is sending the form off even though I have not entered any data, it should error check to make sure certain fields have information? Where have I gone wrong? <?php session_start(); if (!(isset($_SESSION['username']) && $_SESSION['username'] != '')) { header ("Location: login.html"); } // Include the connections script to make a database connection. include("inc/connect.php"); if ( $_POST['submit'] ) { $primary_contact = $_POST['primary_contact']; $address_1 = $_POST['address_1']; $address_2 = $_POST['address_2']; $address_3 = $_POST['address_3']; $town = $_POST['town']; $county = $_POST['county']; $postcode = $_POST['postcode']; $email = $_POST['email']; $telephone_1 = $_POST['telephone_1']; $telephone_2 = $_POST['telephone_2']; $fax = $_POST['fax']; if ( empty($primary_contact) ) { $error['primary_contact_error'] = '<div class="formerror">Please enter your Primary Contact Name.</div>'; } if ( empty($town) ) { $error['town_error'] = '<div class="formerror">Please Enter your Town/City.</div>'; } if ( empty($county) ) { $error['county_error'] = '<div class="formerror">Please Select your County.</div>'; } if ( empty($postcode) ) { $error['postcode_error'] = '<div class="formerror">Please enter your Postcode.</div>'; } if ( empty($telephone_1) ) { $error['telephone_1_error'] = '<div class="formerror">Please enter your Main Telephone Number.</div>'; } // End of error checking, all fields covered. $sql = "SELECT * FROM details WHERE fishery_id = '$_SESSION[fishery_id]' "; $sql_result = mysql_query($sql); if (mysql_num_rows($sql_result) ==0) { // Start of insert // The form should post to itself. if (!$error) { # setup SQL statement $SQL = " INSERT INTO details "; $SQL = $SQL . " (fishery_id, venue_name, primary_contact, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES "; $SQL = $SQL . " ('$_SESSION[fishery_id]', '$_SESSION[venue_name]', '$primary_contact', '$address_1', '$address_2', '$address_3', '$town', '$county', '$postcode', '$email', '$telephone_1', '$telephone_2', '$fax') "; #execute SQL statement $result = mysql_db_query( *****,"$SQL",$connection ); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } } } // END of insert else { // Start of update // The form should post to itself. if (!$error) { # setup SQL statement $SQL = " UPDATE details SET venue_name = '{$_SESSION[venue_name]}', primary_contact = '{$_POST['primary_contact']}', address_1 = '{$_POST['address_1']}', address_2 = '{$_POST['address_2']}', address_3 = '{$_POST['address_3']}', town = '{$_POST['town']}', county = '{$_POST['county']}', postcode = '{$_POST['postcode']}', email = '{$_POST['email']}', telephone_1 = '{$_POST['telephone_1']}', telephone_2 = '{$_POST['telephone_2']}', fax = '{$_POST['fax']}' WHERE fishery_id = '$_SESSION[fishery_id]' "; #execute SQL statement $result = mysql_db_query( *****,"$SQL",$connection ); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } } } // End of update header("Location: http://www.******.com/development/admin/details_view.html"); exit; } ?>
  17. Fixed it, needed to put my error code into relevant places. Thanks people
  18. Thanks again, found it in the connect, was not obvious though, its not throwing any error now. The only thing its not processing either, have I got the statements correct? I need to error check as you can see for empty fields then check the username and then insert? Its just staying on the same page when I fill in and submit?
  19. Sorry here is the error: Warning: Cannot modify header information - headers already sent by (output started at /httpdocs/development/admin/test/sign_up.php:2) in /development/admin/test/sign_up.php on line 54
  20. Nothing in the top before that, no spaces. The code given is all the error checking etc... then move onto check username exist and then insert. Cant see where its being sent?
  21. Can someone just help me with this code, the headers are already being sent from the form, not sure how to fix them here is the code; <?php // Include the connections script to make a database connection. include("../inc/connect.php"); // Start the session to make sure no duplicates. Do later // session_start(); // $_SESSION['session_name'] = '$sid'; // The form should post to itself. if ( $_POST['submit'] ) { $venue_name = $_POST['venue_name']; $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; if ( empty($venue_name) ) { $error['venue_name_error'] = '<div class="formerror">Please Enter your Fishery Venue Name.</div>'; } if ( empty($username) ) { $error['username_error'] = '<div class="formerror">Please enter a username</div>'; } if ( empty($password) ) { $error['password_error'] = '<div class="formerror">Please enter a password</div>'; } if ( empty($email) ) { $error['email_error'] = '<div class="formerror">Please enter your email</div>'; } // End of error checking, all fields covered. if (!$error) { $sql = "SELECT * FROM fishery_a_signup WHERE username = '$_POST[username]' "; $sql_result = mysql_query($sql); if (mysql_num_rows($sql_result) !=0) { $error['username_taken'] = '<div class="formerror">The username is taken please choose another.</div>'; } else { # setup SQL statement $SQL = " INSERT INTO fishery_a_signup "; $SQL = $SQL . " (venue_name, username, password, email) VALUES "; $SQL = $SQL . " ('$venue_name', '$username', '$password', '$email') "; #execute SQL statement $result = mysql_db_query( *****,"$SQL",$connection ); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } else { $id = mysql_insert_id(); header("Location: http://www.*****.com/development/login.html?id=$id"); } } } ?>
  22. I am having difficulty getting the page cache to not reload content in a form when you hit refresh. I have used the meta tags which do not work well, so I then tried the php code to stop it. The only thing that happens is when you hit the back button it states the page has expired and to do a refresh. When you refresh it loads the information in the form fields that you entered before. I would have thought that it would have given me a blank form again? The php code I am using to stop the caching of the form is: <?php // prevent caching (php) header('Cache-Control: no-cache'); header('Pragma: no-cache'); header('Expires: ' . gmdate(DATE_RFC1123, time()-1)); ?> Any ideas how to stop this?
  23. Thanks. I thought about doing the query and checking the other data as well, but what happens if the user decides to change some of the data in the form when they hit the back button, it wont match so will insert as a new row?
  24. Would I be better of using a php session? and then query the session on each page? what would be the best way to do that?
  25. I have my code now working thanks to guys off here. Next step is for me to extend this, and not sure the best way to do this. This is the scenario: User fills in the first page of the form, if they miss any data that is required it asks them for it. Once done, they click the submit button. This then enters the information into the database. As the id field is auto_increment, it creates a new "id" number for the insert. The problem: The user hits the back button on the browser and then submits the data AGAIN This would then create another insert but with the next "id" number So we then have 2 records the same with different id numbers. I need to be able to check that they have entered only once and not allow them again? My problem is that it creates the id on the first entry, how can php check this? Code for the php insert to mysql (taken from file) if (!$error) { # setup SQL statement $SQL = " INSERT INTO dtable "; $SQL = $SQL . " (venue_name, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES "; $SQL = $SQL . " ('$venue_name', '$address_1', '$address_2', '$address_3', '$town', '$county', '$postcode', '$email', '$telephone_1', '$telephone_2', '$fax') "; #execute SQL statement $result = mysql_db_query( dbname,"$SQL",$connection ); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } else { $id = mysql_insert_id(); header("Location: http://www.domain.com/admin/next_contact.html?fishery_id=$id"); exit; } } }
×
×
  • 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.