mysql problem

Discussion in 'PHP' started by pavelazad, Aug 7, 2009.

  1. #1
    hello

    i am facing a silly problem. i can connect to my mysql database from my host but when i insert data from a form its not saving into my mysql table.

    bdhost= localhost
    bduser=london1_pavel (here london1 is my hosting user name)
    password=password
    database name= london1_mydatabase (here london1 is my hosting user name)
    table name= london1_mydatabase_test (here london1_mydatabase is my database name and test is my table name, so table name is london1_mydatabase_test)

    i am using the following code:

    <?php

    mysql_connect("localhost", "london1_pavel", "password") or die(mysql_error());
    echo "Connected to MySQL<br />";
    mysql_select_db("london1_mydatabase") or die(mysql_error());
    echo "Connected to Database";
    $name=$_POST['name'];
    $add=$_POST['add'];

    $sql = "INSERT INTO london1_mydatabase_test (name, add) VALUES ('$name', '$add')";
    $result=mysql_query($sql);

    ?>

    this code connect to my database but there is no input in the table.

    anyone please can help me?
     
    pavelazad, Aug 7, 2009 IP
  2. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #2
    Try this

    $sql = "INSERT INTO london1_mydatabase_test (`name`, `add`) VALUES ('$name', '$add')"; 
    Code (markup):
     
    olddocks, Aug 7, 2009 IP
  3. pavelazad

    pavelazad Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Its not working bro.
     
    pavelazad, Aug 7, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You need to escape your SQL variables:

    '" . mysql_real_escape_string($name) . "', '" . mysql_real_escape_string($add) . "')"
    Code (markup):
    This is necessary when those values contain ' characters and simply to prevent SQL injection attacks.

    And if that isn't working, add this:

    
    if (!$result) {
        echo 'SQL: ' . $sql . '<br>';
        echo 'Error: ' . mysql_error();
        die();
    }
    PHP:
     
    premiumscripts, Aug 7, 2009 IP
  5. pavelazad

    pavelazad Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sory bro still not workin. i don know wher the problem is. i used ur code but getting the reply:

    Connected to MySQL
    Connected to DatabaseSQL: INSERT INTO amarlon1_mysite_test (name, add) VALUES ('aaa', 'sdfdf')
    Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add) VALUES ('aaa', 'sdfdf')' at line 1
     
    pavelazad, Aug 7, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oh, you should have this in the SQL: `name`, `add` (as the other user said, because "add" is a special word in mysql) then show the error message again.
     
    premiumscripts, Aug 7, 2009 IP
  7. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    lool funny man

    make sure your database is called london1_mydatabase_test and NOT just test

    once you have selected your db you dont need to prepend it with every query

    TRY

    $sql = "INSERT INTO test (`name`, `add`) VALUES ('$name', '$add')";
    Code (markup):
     
    oop, Aug 7, 2009 IP