2 Replies - 151 Views - Last Post: 07 February 2012 - 09:50 PM Rate Topic: -----

Topic Sponsor:

#1 Mycah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-February 12

Stopping if not $_POST

Posted 07 February 2012 - 09:32 PM

Hello!

I am trying to perfect my registration script (basic, just places variables into a mySQL database) and would like to make it so someone simply can't go to register.php directly and see the registration confirmation (this stops people from entering blanks).

Here is what I'm trying to do:

<?php
 
	 $host="localhost"; // Host name 
	 $username=""; // Mysql username 
	 $password=""; // Mysql password 
	 $db_name=""; // Database name 
	 $tbl_name="members"; // Table name
	 
	 // Connect to server and select database.
	 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
	 mysql_select_db("$db_name")or die("cannot select DB");
	 
	 // Get values from form 
	 $name=stripslashes($_POST['name']);
	 $email=stripslashes($_POST['email']);
	 $username=stripslashes($_POST['username']);
	 
	 //Check to see if there are duplicates 
	 $query = "SELECT COUNT(*) AS count FROM $tbl_name WHERE username='$username'";
	 $results = mysql_query($query) or die ("Error reading from database");
	 $existingEmails = mysql_fetch_array($results);
	 
	 
	 // Insert data into mysql 
	 $sql="INSERT INTO $tbl_name(name, email, username)VALUES('$name', '$email', '$username')";
	 $result=mysql_query($sql);
	 
	 
	 if($_SERVER['REQUEST_METHOD'] == "POST") {
			 if ($existingEmails['count'] > 0) {
				echo "<div style=\"color: #FFFFFF; font-family:'Macondo Swash Caps';\"></div>";
				} else { 
			mysql_query($sql);
			 //Send mail
			$to="$email";
			$subject="";
			$headers = '' . "\r\n";
			$headers .= 'Bcc: ' . "\r\n";
			$message="
			  
			";
			 
			 mail($to, $subject, $message, $hearders);
			 echo "<div style=\"font-family: 'Macondo Swash Caps'; color: #FFFFFF;\">T</a>";
			 }
		} else {
			print You did not fill out the form!; 
			
			}
	 
	 // close connection 
	 mysql_close();
 	?>




I'm getting this error: Parse error: syntax error, unexpected T_STRING in register.php on line 68.

Thanks for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: Stopping if not $_POST

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1760
  • View blog
  • Posts: 2,693
  • Joined: 08-June 10

Re: Stopping if not $_POST

Posted 07 February 2012 - 09:46 PM

First, your code already does what you are asking for. On line #29 it checks if the request method is POST, and only then sends the email. You could simply extend that to cover your database code as well.

Second, checking if the request method is POST works, but only if you assume that all POST requests will carry the data. (Which is a rather naive assumption, really.) What you should be doing is checking the $_POST array for the data you need, and only then proceed.
if (isset($_POST["first"], $_POST["second"], $_POST["and so forth"])) {
    // The data was sent. Do the database and/or mail stuff.
}
else {
    // No data was sent. Print an error, or redirect, or whatever.
}



Third, your error message references line #68 but your code is only 53 lines long. - I'm guessing, though, that it is pointing to line #47 in your code. The error there is obvious. If it's not, you should review the basic PHP syntax before proceeding. (I suggest starting with string syntax.)
Was This Post Helpful? 1
  • +
  • -

#3 Mycah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-February 12

Re: Stopping if not $_POST

Posted 07 February 2012 - 09:50 PM

Thank you for your answer, and I see the error I made now. It is working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1