www.webdeveloper.com
Results 1 to 5 of 5

Thread: PHP form and MySQL

Hybrid View

  1. #1
    Join Date
    Dec 2013
    Posts
    3

    PHP form and MySQL

    Hi

    Am getting a error when am trying connect inputs from my contact form to my database. Please have a look at following code
    HTML

    HTML Code:
    <div id='frmFormMailContainer'>
    <h2>Contact Us</h2><form name="Contacts" method="post" action="Insert.php">
    <div class="formItem" id="fnamef">
    <label for="FirstName" class="testLabel">First name</label>
      <input type="text" name="FirstName" id="FirstName">
    </div>
    <div id="lnamef">
    <label for="LastName" class="textLabel">Last name</label>
    <input type="text" name="LastName" id="LastName">
    </div>
    <div id="emailf">
    <label for="Email" class="textLabel">Email </label>
    <input type="text" name="Email" id="Email">
    </div>
    <div id="mobilef">
    <label for="Mobile" class="textLabel">Mobile</label>
    <input type="tel" name="Mobile" id="Mobile">
    
    <div id="Message">
    <label for="Message" class="textLabel">Message</label>
    <input type="text" name="Message" id="Message">
    </div>
    <br>
    <div id="submitf">
    <input type="submit" name="submit" id="submit" value="Submit">
    </form> </div>
    </div>
    and PHP
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    </body>
    </html>
    <?php
        $username
    ="root";
        
    $password="";
        
    $database="test1";
        
        
    $FirstName $_POST["FirstName"];
        
    $LastName $_POST["LastName"];
        
    $Email $_POST["Email"];
        
    $Mobile $_POST["Mobile"];
        
    $Message $_POST["Message"];

            echo 
    $FirstName;
            echo 
    $LastName;
            echo 
    $Email;
            echo 
    $Mobile;
            echo 
    $Message;

      
    $conn=mysql_connect("localhost""root""")
            or die (
    "NO connection");

        
    $db=mysql_select_db("test1",$conn)
        or die (
    "Brak polaczenia");

        
    $sql "INSERT INTO 'test1'.'contacts' ('FirstName', 'LastName', 'Email', 'Mobile', 'Message')
                VALUES ('
    $FirstName', '$LastName', '$Email', '$Mobile', '$Message');";

        
    $db=mysql_query($sql,$conn)
        or die (
    "Error");

    ?>
    Thanks

  2. #2
    Join Date
    Nov 2013
    Posts
    66
    I think you may be having a problem with your connect function call and your insert statement.

    INSERT INTO contacts

    Since you are creating variables for you database info, you should use them, and also specify what database you want to connect to in the call.

    PHP Code:
    $conn=mysqli_connect($host,$username,$password,$database); 
    Then your insert statement should look like this. I also noticed an extra semicolon at the end of the values.

    PHP Code:
    $sql "INSERT INTO 'test1'.'contacts' ('FirstName', 'LastName', 'Email', 'Mobile', 'Message')
              VALUES ('
    $FirstName', '$LastName', '$Email', '$Mobile', '$Message')"
    Last edited by wevie; 12-03-2013 at 09:41 PM. Reason: left out a closing php tag

  3. #3
    Join Date
    Nov 2013
    Posts
    33
    My biggest issue with your code is mysql please please please look into mysqli or PDO
    ok so your code
    bad code below -> you don't need to declare the database in the query
    Code:
    $sql = "INSERT INTO 'test1'.'contacts' ('FirstName', 'LastName', 'Email', 'Mobile', 'Message') 
                VALUES ('$FirstName', '$LastName', '$Email', '$Mobile', '$Message');";
    good code. notice also i have used ticks ` and not single quotes ' . singles quotes should only be used in the VALUES
    Code:
    $sql = "INSERT INTO `contacts` (`FirstName`, `LastName`, `Email`, `Mobile`, `Message`) 
                VALUES ('$FirstName', '$LastName', '$Email', '$Mobile', '$Message');";
    hopefully this will get you started towards fixing your code.

  4. #4
    Join Date
    Nov 2013
    Posts
    66
    Why the ticks? I have queries written that is working without the ticks. I am most curious as to what the propose of those are. Should I start using them?

  5. #5
    Join Date
    Nov 2013
    Posts
    33
    the backticks help to avoid any reserved keyword collisions. but are not always necessary and in all honesty not best practice. But i find if i have issues using backticks can help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles