Jump to content


Photo

php+mysql is killing me


  • Please log in to reply
26 replies to this topic

#21 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 30 October 2006 - 10:51 AM

OK, the problem you have here is that you're not actually submitting the form.  The moment you click on the link, it's navigating to signup.php as opposed to posting to it.  What you really needed was an image field in your form.

If you'd typed
echo $HTTP_POST_VARS['email'];
at the top of signup.php, you'd have found that it didn't actually echo anything.

I'd change the code slightly if I were you.  Rather than have a button for subscribe and one for un-subscribe, just have one submit button.  So I enter my email address and hit submit, if I'm subscribed already then it un-subscribes me, if I'm not then it subscribes me.  How does that sound?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#22 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 30 October 2006 - 11:30 AM

The following code has done away with your form, and just uses one file, signup.php, you should be able to call it signup.php and drop it onto your server as it is.  I'd advise backing up your existing code first in case you don't like this.

It uses the above concept of adding you if you don't exist, and removing you if you do exist.

Regards
Huggie

<?php
include('header1.tpl');

if (isset($HTTP_POST_VARS['email']) && !empty($HTTP_POST_VARS['email'])){
   $email = $HTTP_POST_VARS['email'];
   $email = trim($email);
   $email = addslashes($email);

   // open connection to MySQL server
   $connection = mysql_pconnect('localhost', '', '') or die ('Unable to connect!');

   // select database for use
   mysql_select_db('sme_sme2') or die ('Unable to select database!');

   // run query to see if email is subscribing or un-subscribing
   $query = "SELECT id FROM newsletter WHERE email = '$email'";
   $result = mysql_query($query) or die ("Unable to run query $query: " . mysql_error());
   $exists = mysql_num_rows($result);

   if ($exists == 0){
      // user doesn't exist
      $insert = "INSERT INTO newsletter (email) VALUES ('$email')";
      $result = mysql_query($insert) or die ("Unable to insert $insert: " . mysql_error());
      
      echo '<br/>';
      echo '<center>';
      echo "Thank you for signing up for smeresources newsletter, $email has been added to our list";
      echo '</center>';
   }
   else if ($exists == 1){
      // user exists
      $delete = "DELETE FROM newsletter WHERE email = '$email'";
      $result = mysql_query($delete) or die ("Unable to delete $delete: " . mysql_error());
      
      echo '<br/>';
      echo '<center>';
      echo "$email has been un-subscribed from the smeresources newsletter";
      echo '</center>';
   }
}

echo <<<HTML
<form method="post" action="{$_SERVER['PHP_SELF']}" style="display:inline;">
 Enter Email:<br />
 <input type="text" size="20"  name="email" style="font-size: 10px;"  /><br />
 <input type="image" name="submit" align="absmiddle" src="images/buttons_r1_c1.gif" alt="Submit" /></a>
</form>
HTML;
?>

</div>

<?php
include('footer.tpl');
?>

Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#23 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 30 October 2006 - 11:39 AM

Huggie,
        Thanks a Zillion,let me implement those changes and I will keep you inform.
My Regards
XUX ;D

#24 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 30 October 2006 - 12:07 PM

Huggie,
          There is a minor challenge.the form is part of a larger code and am wondering how to implement the changes.can you please break it down,i think it will be easier to implement.Thanks
My Regards
XUX

#25 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 30 October 2006 - 12:21 PM

Here you go:

signup.php
<?php
include('header1.tpl');

if (isset($HTTP_POST_VARS['email']) && !empty($HTTP_POST_VARS['email'])){
   $email = $HTTP_POST_VARS['email'];
   $email = trim($email);
   $email = addslashes($email);

   // open connection to MySQL server
   $connection = mysql_pconnect('localhost', '', '') or die ('Unable to connect!');

   // select database for use
   mysql_select_db('sme_sme2') or die ('Unable to select database!');

   // run query to see if email is subscribing or un-subscribing
   $query = "SELECT id FROM newsletter WHERE email = '$email'";
   $result = mysql_query($query) or die ("Unable to run query $query: " . mysql_error());
   $exists = mysql_num_rows($result);

   if ($exists == 0){
      // user doesn't exist
      $insert = "INSERT INTO newsletter (email) VALUES ('$email')";
      $result = mysql_query($insert) or die ("Unable to insert $insert: " . mysql_error());
      
      echo '<br/>';
      echo '<center>';
      echo "Thank you for signing up for smeresources newsletter, $email has been added to our list";
      echo '</center>';
   }
   else if ($exists == 1){
      // user exists
      $delete = "DELETE FROM newsletter WHERE email = '$email'";
      $result = mysql_query($delete) or die ("Unable to delete $delete: " . mysql_error());
      
      echo '<br/>';
      echo '<center>';
      echo "$email has been un-subscribed from the smeresources newsletter";
      echo '</center>';
   }
}
else {
   header("Location: form.htm");
}
?>

</div>

<?php
include('footer.tpl');
?>

form.htm
<form method="post" action="signup.php" style="display:inline;">
 Enter Email:<br />
 <input type="text" size="20"  name="email" style="font-size: 10px;"  /><br />
 <input type="image" name="submit" align="absmiddle" src="images/buttons_r1_c1.gif" alt="Submit" /></a>
</form>

Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#26 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 30 October 2006 - 12:36 PM

Thanks a lot,am not sure whether you will want to solve another simple problem for me again,sending the newsletter to all the emails in the database.
<?php
   $message = "<img src=\"http://www.smeresources.org/images/newsletter-header.jpg\" alt=\"Signature\"><h1>Testing Your Newsletter</h1>";

   // Set some parameters
   $toAddress = "d4dayo2001@yahoo.com";
   $subject = "Test Subject";
   $headers = "Content-type: text/html\n\n";

   // Try to send the message
   $success = mail($toAddress,$subject,$message,$headers);

   if($success){
      print "Newsletter Sent";
   }
   else{
      echo'Newwsletter could not be sent';
   }   
?>

how can i implement integrate it to the codes above.
My Highest Regards
XUX

#27 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 30 October 2006 - 01:41 PM

I suggest posting this as a new question in the forum.  Tagging new questions onto existing posts like this reduces the amount of viewers considerably.

I'll take a look at it in the new post.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users