Jump to content

New row in sql when page loads


melbahtoast

Recommended Posts

Hi all!

 

I'm fairly new to php, don't really know the proper ins and outs but I'm learning. I'm stuck on two things. I have a simple form that submits an entered e-mail address to my email and also kicks back to a database. That part works, however, when ever I go to the page or hit the "submit" button it generates a blank row in the database.

 

Also (problemo #2), the submit button should echo "Please enter your email" when the submit button is hit with nothing in the field, but it doesn't. I've pulled the code and put it on a new file and it works, maybe it's just something else I have on the page that's interfering?

 

The form is same page, it echos below the e-mail input success or invalid e-mail. Using bootstrap if that helps at all. Here's the code:

<form name="emailcapture" method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<input name="email" type="text" value="<?php if(isset($_POST['submit'])){echo $email;} ?>" class="emailform"/>  <?php if(isset($_POST['submit'])){echo $erre;} ?> <input type="submit" name="submit" value="Submit" class="btn"/><br><span class="formvalidate"><?php$link = mysql_connect('localhost', 'username', 'password');if (!$link) { die('Could not connect: ' . mysql_error());} if(isset($_POST['submit'])){ $email=$_POST['email']; $to="myemail@email.com"; $re="Sign Up"; $msg = "$email"; $headers = "From: $email\r\n";
if(!$email){ $erre="Please enter your email."; }else{ $email = trim($email); $_ename = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_'{|}~]+"; $_host = "([0-9A-Z]+\.)+"; $_tlds = "([0-9A-Z]){2,4}$/i"; if ( !preg_match($_ename."@".$_host.$_tlds,$email)){ echo "Sorry, it appears the e-mail address you have entered is not valid, try again."; }else{ mail($to, $re, $msg, $headers); echo "Thank you. You have been added to our list and will be notified when we launch."; } } }mysql_select_db('database');mysql_query("INSERT INTO table (emails, date) VALUES ('$_POST[email]', now())");
mysql_close($link)
?></span>
</form>

Link to comment
Share on other sites

- 5.0.96

-

$link = mysql_connect('localhost', 'username', 'password');if (!$link) { die('Could not connect: ' . mysql_error());}

 

mysql_select_db('database');mysql_query("INSERT INTO table (emails, date) VALUES ('$_POST[email]', now())");
mysql_close($link)

 

- no errors outputted

- tried moving the code around some but nothing worked. I think it would work if I just threw it on a separate php page an changed the action on the form, but ideally I would like this all to work on one page, I don't want the page to refresh at all.

 

Also, replaced the code with "username", "password" etc.

 

Sorry for the lack of info, like I said I'm kind of new to this...

Edited by melbahtoast
Link to comment
Share on other sites

There is nothing wrong with using "email" as the index of the POST array, nor as the name of input fields. There are absolutely 0 side effects of doing so, with any input field[1].

It's great that you want to help people, but please ensure that what you're posting is accurate and actual facts. Otherwise you're not helping, but only making things worse. So, please, take the time to verify that what you think is the case is actually correct, before posting.

 

melbahtoast: Add some proper indentation and newlines to your script, so it's possible to read it. Remember, the easier you make it for us to help you, the higher the chance of you getting (quality) help.

Also, since you're using PHP_SELF, you should check out this snippet.

 

 

[1]With the possible exception of the naming the submit type "submit", but only for a certain old, and long outdated, browser. And then only in very specific uses of Javascript.

Edited by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.