#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    MySql question with Xampp


    I have a website with a form that enables user to enter a valid email address, and the email they entered will be posted to my database on Xampp which I have already created. This is my php file:
    Code:
    <?php
    mysql_connect("localhost","user1","password");
    
    mysql_select_db("email");
    
    $email = $_POST['text1'];
    
    $query="INSERT INTO email (EmailAdd)VALUES ('$email')";
    
    mysql_query($query) or die ('Error updating database');
    
    echo "Database updated with : "$email";
    
    mysql_close();
    ?>
    and this is my form which enable user to enter email, its an html file:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
    <title>Form</title>
    
    <script type='text/javascript'> 
          function ValidateEmail(inputText)
          {
          var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
          if(inputText.value.match(mailformat))
          {
          document.form1.text1.focus();
          return true;
          }
          else
          {
          alert("You have entered an invalid email address!");
          document.form1.text1.focus();
          return false;
          }
          }
          </script>
    </head>
    
    <body>
    <div id="container">
    		<div id="mainpic">
    		<h1> * About Me * </h1>
    </div>   
            
    <div id="menu">
            	<ul>
                	<li class="menuitem"><a href="AboutMe.html">Home</a></li>
    	<li class="menuitem"><a href="Form.html">Form</a></li>
                    </ul>
    </div>
            
    		<div id="content">
            	<h2>Please enter your email address</h2>
                    <p>&nbsp;</p>
    		<form name="form1" action="email.php" method="post"> 
    		<input type='text' name='text1'/>
    		<input type="submit" name="submit" value="Submit" onclick="ValidateEmail(document.form1.text1)"/>
    		</form>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
             </div>
          </div>
       </div>
    </body>
    </html>
    However, when I run the html file, after putting in email and pressed submit, it displays a page with my php code and nothing else... I don't get it, is it because I should place the file in some particular place on my server? or is there something wrong with the code?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2008
    Posts
    1,928
    Rep Power
    378
    You have some missing quotation marks, but that wouldn't explain the error you describe.

    Change this...

    echo "Database updated with : "$email";

    ...to this...

    echo "Database updated with : \"$email\"";

    ...and TEMPORARILY change this...

    mysql_query($query) or die ('Error updating database');

    ...to this...

    mysql_query($query) or die (mysql_error());

    Change it back again once everything's working

    And now read up on MySQL injection and both PDO and mysqli connection methods.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Your PHP files need to end in .php, not .html.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo