Just a simple suggestion Regarding this error...!

Discussion in 'PHP' started by strgraphics, Jan 13, 2010.

  1. #1
    Friends., i want to run 2 files in my server,

    1. a.php
    2. insert.php

    in a.php i have:

    <form action="insert.php" method="post">
    Value1: <input type="text" name="field1-name"><br>
    Value2: <input type="text" name="field2-name"><br>
    <input type="Submit">
    </form>
    PHP:

    in insert.php i have:

    <?php
    $username="testforum123";
    $password="utRHApEFr3xqrHPC";
    $database="testforum";
    
    $field1-name=$_POST['Value1'];
    $field2-name=$_POST['Value2'];
    
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $query = "INSERT INTO tablename VALUES
    ('','$field1-name','$field2-name');
    
    mysql_query($query);
    
    mysql_close();
    ?>
    PHP:


    When i click on submit button.....

    i am getting the error:

    Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\insert.php on line 6


    Whats wrong
     
    strgraphics, Jan 13, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    You can't use - within your $variables:

    Change:
    
    $field1-name=$_POST['Value1'];
    $field2-name=$_POST['Value2'];
    
    PHP:
    Too:

    
    $field1name=$_POST['Value1'];
    $field2name=$_POST['Value2'];
    
    PHP:
    and then call them accordingly.
     
    danx10, Jan 13, 2010 IP
  3. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You are also misplacing the " in the query statement.

    Change:
    $query = "INSERT INTO tablename VALUES
    ('','$field1-name','$field2-name');
    PHP:
    to something like this...

    
    $query = "INSERT INTO tablename VALUES ('{$field1name}','{$field2name}')";
    
    PHP:
    Regards,
    Steve
     
    Steve136, Jan 13, 2010 IP
  4. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Thanksssssssssss friends, thanks a lot i got..

    But friends...... will you tell me how to store them in my database..,

    i dont know how to create the database table..just neww. please tell.

    this is my final... question, how to create database table for above script ?
     
    Last edited: Jan 13, 2010
    strgraphics, Jan 13, 2010 IP
  5. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    But in my database i am getting as:

    [​IMG]

    Please help me in creating my database table
     
    strgraphics, Jan 13, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    a.php:

    <form action="insert.php" method="post">
    Value1: <input type="text" name="field1name"><br>
    Value2: <input type="text" name="field2name"><br>
    <input type="Submit">
    </form>
    Code (markup):
    insert.php:

    <?php
    error_reporting(E_ALL);
    
    /*
    Read up on MySQL INSERT @
    http://www.w3schools.com/PHP/php_mysql_insert.asp
    */
    
    $username="testforum123";
    $password="utRHApEFr3xqrHPC";
    $database="testforum";
    
    $field1name=$_REQUEST['field1name'];
    $field2name=$_REQUEST['field2name'];
    
    $con = mysql_connect("localhost",$username,$password);
    mysql_select_db($database, $con) or die(mysql_error());
    
    mysql_query("INSERT INTO tablename (`columnname1`, `columnname2`) VALUES
    ($field1name,$field2name)");
    
    mysql_close($con);
    ?>
    PHP:
     
    danx10, Jan 14, 2010 IP
  7. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Thanks for you help..., thanks a lot
     
    strgraphics, Jan 22, 2010 IP