Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Posts
    1

    Unanswered: php insert statement in mysql

    Hello everybody
    i am wondering if someone could help me with this problem i am having.
    i am trying to insert some data from a web interface into the database but i am getting an error "error adding name to database".
    below i have copy the code:

    FORM

    <?php
    $mytest=mysql_connect("localhost","root","");
    if (!$mytest)
    {
    exit("Connection Failed: " . $mytest);
    }
    echo'<form method="get" action="AddStudentTest.php">
    <input type="text" name="Firstname">
    <input type="text" name="Surname">
    <input type="submit" name="AddStudentTest" value="Submit">
    </form>';
    mysql_close($mytest);
    echo "</table>";
    ?>

    AND THE INSERT CODE
    <?php
    $mytest=mysql_connect("localhost","root","");
    if (!$mytest)
    {
    echo("Connection Failed: " . $mytest);
    exit();
    }
    if(!mysql_select_db("test"))
    {
    echo "I cant connect to that db";
    exit;
    }
    else
    //$sql="INSERT INTO studenttest values('$Firstname','$Surname')";
    mysql_query("insert into studenttest('Firstname') values('$Firstname')");
    echo $Firstname;
    if (mysql_query ($sql))
    {
    echo("<p>Your Name has been added.<p>");
    } else
    {
    echo("Error adding name to database.");
    }

    mysql_close($mytest);
    echo"</body>";
    echo"</html>";
    ?>

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    this is wrong --

    insert into studenttest('Firstname') values('$Firstname')

    the list of column names may not contain a string

    this might work --

    insert into studenttest(Firstname) values('$Firstname')
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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