cannot insert in mysql using php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krista

    cannot insert in mysql using php

    Hi guys, i have a problem about inserting data into the table. I dont
    know why the output always said cannot be added to the database!
    Anyone has idea for me?
    i check the connect is ok, but dont know why.
    The following is a function i will call to insert the database.
    Thx!!!
    function AddPart($Partnu mber, $Partname, $Partdescriptio n)
    {
    //Set the variables for the database
    $DBName="krista ";
    $TableName = "parts";


    $Link = mysql_connect(" localhost",""," ") or die ('I cannot connect to
    the database because: ' . mysql_error());
    mysql_select_db ($DBName,$Link) ;

    $Query = "INSERT into $TableName (Part_Number, Part_Name,
    Part_Descriptio n) values ($Partnumber, $Partname, $Partdescriptio n)";

    if (mysql_query($D BName, $Query, $Link)){

    print("Your information is successfully added to the database!!
    <BR>\n");
    }
    else{
    print("Your information cannot be added to the database! <BR>\n");
    }

    //close MySQL
    mysql_close($Li nk);

    }

    Krista
  • Tom Thackrey

    #2
    Re: cannot insert in mysql using php


    On 19-Dec-2003, ywan_ip@hotmail .com (Krista) wrote:
    [color=blue]
    > Hi guys, i have a problem about inserting data into the table. I dont
    > know why the output always said cannot be added to the database!
    > Anyone has idea for me?
    > i check the connect is ok, but dont know why.
    > The following is a function i will call to insert the database.
    > Thx!!!
    > function AddPart($Partnu mber, $Partname, $Partdescriptio n)
    > {
    > //Set the variables for the database
    > $DBName="krista ";
    > $TableName = "parts";
    >
    >
    > $Link = mysql_connect(" localhost",""," ") or die ('I cannot connect to
    > the database because: ' . mysql_error());
    > mysql_select_db ($DBName,$Link) ;
    >
    > $Query = "INSERT into $TableName (Part_Number, Part_Name,
    > Part_Descriptio n) values ($Partnumber, $Partname, $Partdescriptio n)";[/color]


    Unless the data are numeric, you need to enclose their values in
    apostrophies like this:
    Part_Descriptio n) values ('$Partnumbe'r, '$Partname', '$Partdescripti on')";

    [color=blue]
    >
    > if (mysql_query($D BName, $Query, $Link)){
    >
    > print("Your information is successfully added to the database!!
    > <BR>\n");
    > }
    > else{
    > print("Your information cannot be added to the database! <BR>\n");[/color]

    please change the above to
    print("Your information cannot be added to the database! <br>
    $Query failed because ".mysql_error() ."<BR>\n");

    You will find the additional information very helpful in debugging.

    [color=blue]
    > }
    >
    > //close MySQL
    > mysql_close($Li nk);
    >
    > }[/color]



    --
    Tom Thackrey

    tom (at) creative (dash) light (dot) com
    do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

    Comment

    Working...