Help with PHP code and form code

Discussion in 'PHP' started by Alibaba143, Oct 31, 2012.

  1. #1
    hi guys can u help me with this code, the error in wamp is

    [TABLE="class: xdebug-error xe-notice"]
    [TR]
    [TH="bgcolor: #f57900, colspan: 5, align: left"]Notice: Undefined index: user in C:\wamp\www\index.php on line 53[/TH]
    [/TR]
    [TR]
    [TH="bgcolor: #e9b96e, colspan: 5, align: left"]Call Stack[/TH]
    [/TR]
    [TR]
    [TH="bgcolor: #eeeeec, align: center"]#[/TH]
    [TH="bgcolor: #eeeeec, align: left"]Time[/TH]
    [TH="bgcolor: #eeeeec, align: left"]Memory[/TH]
    [TH="bgcolor: #eeeeec, align: left"]Function[/TH]
    [TH="bgcolor: #eeeeec, align: left"]Location[/TH]
    [/TR]
    [TR]
    [TD="bgcolor: #eeeeec, align: center"]1[/TD]
    [TD="bgcolor: #eeeeec, align: center"]0.0007[/TD]
    [TD="bgcolor: #eeeeec, align: right"]678552[/TD]
    [TD="bgcolor: #eeeeec"]{main}( )[/TD]
    [TD="bgcolor: #eeeeec"]..\index.php:0[/TD]
    [/TR]
    [/TABLE]

    code is


    <?php
    $con = mysql_connect("localhost","aneeb","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    $selected = mysql_select_db("b_tips",$con)
    or die("Could Not Select Db");

    $mysql_post="INSERT INTO tips (User, Title, Tip)
    VALUES ('$_POST[user]','$_POST[title]','$_POST[tip]')";

    if (!mysql_query($mysql_post,$con))
    {
    die ('Error: ' . mysql_error());

    }
    echo "1 Record Added";

    mysql_close($con);
    ?>
     
    Alibaba143, Oct 31, 2012 IP
  2. NightCoffee

    NightCoffee Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    Does the form have a field with the name "user"?

    Also you should also use mysql_real_escape_string() to avoid SQL injection attacks or better still use parametrized queries e.g.:

    $db     = new PDO('mysql:dbname=b_tips;host=localhost', 'aneeb', '');
    $stmt   = $db->prepare('INSERT INTO tips (User, Title, Tip) VALUES (:user, :title, :tip)');
    
    $result = $stmt->execute(array(
    	':user'  => $_POST['user'],
    	':title' => $_POST['title'],
    	':tip'   => $_POST['tip']
    ));
    
    if(!$result)
    	die('Error: ' . $sth->errorInfo()[2]);
    
    echo "1 Record Added";
    PHP:
     
    NightCoffee, Oct 31, 2012 IP
  3. Alibaba143

    Alibaba143 Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for the tip and help, i have a field name user, stille facing the problem, form is working proprely .. but when i refresh the page on localhost, it automatically adds a blank data entry
     
    Alibaba143, Oct 31, 2012 IP
  4. NightCoffee

    NightCoffee Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    Try adding a check to make sure that the form has been submitted:

    // make sure the form has been submitted
    if($_POST)
    {
        // Form has been submitted so insert it
        $db     = new PDO('mysql:dbname=b_tips;host=localhost', 'aneeb', '');
        $stmt   = $db->prepare('INSERT INTO tips (User, Title, Tip) VALUES (:user, :title, :tip)');
    
        $result = $stmt->execute(array(
            ':user'  => $_POST['user'],
            ':title' => $_POST['title'],
            ':tip'   => $_POST['tip']
        ));
    
        if(!$result)
            die('Error: ' . $sth->errorInfo()[2]);
    
        echo "1 Record Added";
    }
    PHP:
     
    NightCoffee, Oct 31, 2012 IP
  5. Alibaba143

    Alibaba143 Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    this code also have error

    die('Error: ' . $sth->errorInfo () [2] );

    but when i remove [2]

    its works but Still by refreshing the page it repeats the last entry in database
     
    Alibaba143, Oct 31, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    $mysql_post="INSERT INTO tips (User, Title, Tip) VALUES ('$_POST[\'user\']','$_POST[\'title\']','$_POST[\'tip\']')";
     
    Rukbat, Nov 1, 2012 IP
  7. shubhamm

    shubhamm Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    it's saying you don;t have a index user and why it's coming

    $_POST['user'];
    not $_POST[user];

    check it and tell here..
     
    shubhamm, Nov 3, 2012 IP