PHP insert

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

    #1

    PHP insert

    Hello, PHP n00b here.

    Using SQL just working off some examples, I have no problem selecting
    data, but I cant seem to be able to insert. If someone could see where
    im going wrong


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Databa se Connection</title>
    </head>
    <body>
    <?php
    #Server Name - e.g. localhost:3309
    $dbserver = 'localhost';
    #Username used to log into MySQL database server.
    $dbusername = '';
    #Password used to log into MySQL database server.
    $dbpassword = '';
    #Error message shown if an error whilst attempt to establish a
    connection with the server.
    $dberrormessage = '<strong>Error connecting to database...</strong>';
    #Name of database on server.
    $dbdatabase = 'bonzollibrary' ;
    #Establish Connection
    @$conn = mysql_connect($ dbserver, $dbusername, $dbpassword) or exit
    ($dberrormessag e);
    #Selects which database is to be used on server.
    mysql_select_db ($dbdatabase);
    ?>
    <h1>Sample Database Connection</h1>

    <form action="db-conn2.php" method="get">
    <input type="text" id="name" name="name" />&nbsp;
    <input type="submit" value="Search" />
    </form>

    <?php if(isset($_REQU EST['name']))
    { ?>
    <table border="1">
    <tr>
    <th>Number</th>

    </tr>
    "Insert into tbl_category (cat_descriptio n)
    Values ('%{$_REQUEST['name']}%')


    <?php $query = "Insert into tbl_category (cat_descriptio n) Values
    ('%{$_REQUEST['name']}%')";
    @$result = mysql_query($qu ery) or exit('<strong>A n error has
    occured while connecting to the database. The following query is

    not valid: \''.$query.'\'. </strong>');
    }?>
    </table>
    <?php
    #Check if connection is still open
    if($conn)
    {
    #Close the connection
    mysql_close($co nn);
    }
    ?>
    </body>
    </html>


    Thanx in advance

  • petersprc@gmail.com

    #2
    Re: PHP insert

    Hi,

    Maybe mysql_error has some more info?

    $result = mysql_query($qu ery) or trigger_error(m ysql_error());

    Dunno if it's a problem having dbusername and dbpassword blank.
    occured while connecting to the database. The following query is
    >
    not valid: \''.$query.'\'. </strong>');
    }?>

    Bonzol wrote:
    Hello, PHP n00b here.
    >
    Using SQL just working off some examples, I have no problem selecting
    data, but I cant seem to be able to insert. If someone could see where
    im going wrong
    >
    >
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Databa se Connection</title>
    </head>
    <body>
    <?php
    #Server Name - e.g. localhost:3309
    $dbserver = 'localhost';
    #Username used to log into MySQL database server.
    $dbusername = '';
    #Password used to log into MySQL database server.
    $dbpassword = '';
    #Error message shown if an error whilst attempt to establish a
    connection with the server.
    $dberrormessage = '<strong>Error connecting to database...</strong>';
    #Name of database on server.
    $dbdatabase = 'bonzollibrary' ;
    #Establish Connection
    @$conn = mysql_connect($ dbserver, $dbusername, $dbpassword) or exit
    ($dberrormessag e);
    #Selects which database is to be used on server.
    mysql_select_db ($dbdatabase);
    ?>
    <h1>Sample Database Connection</h1>
    >
    <form action="db-conn2.php" method="get">
    <input type="text" id="name" name="name" />&nbsp;
    <input type="submit" value="Search" />
    </form>
    >
    <?php if(isset($_REQU EST['name']))
    { ?>
    <table border="1">
    <tr>
    <th>Number</th>
    >
    </tr>
    "Insert into tbl_category (cat_descriptio n)
    Values ('%{$_REQUEST['name']}%')
    >
    >
    <?php $query = "Insert into tbl_category (cat_descriptio n) Values
    ('%{$_REQUEST['name']}%')";
    @$result = mysql_query($qu ery) or exit('<strong>A n error has
    occured while connecting to the database. The following query is
    >
    not valid: \''.$query.'\'. </strong>');
    }?>
    </table>
    <?php
    #Check if connection is still open
    if($conn)
    {
    #Close the connection
    mysql_close($co nn);
    }
    ?>
    </body>
    </html>
    >
    >
    Thanx in advance

    Comment

    • .:[ ikciu ]:.

      #3
      Re: PHP insert

      Hmm Bonzol <Bonzol@hotmail .comwrote:
      <?php $query = "Insert into tbl_category (cat_descriptio n) Values
      ('%{$_REQUEST['name']}%')";
      @$result = mysql_query($qu ery) or exit('<strong>A n error has
      occured while connecting to the database. The following query is
      show us your table in db


      --
      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
      Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl
      ( ads: www.e6y.eu )

      2be || !2be $this =mysql_query();


      Comment

      • Petr Vileta

        #4
        Re: PHP insert

        "Bonzol" <Bonzol@hotmail .compíse v diskusním príspevku
        news:1161366905 .830555.89540@m 73g2000cwd.goog legroups.com...
        Hello, PHP n00b here.
        >
        <?php $query = "Insert into tbl_category (cat_descriptio n) Values
        ('%{$_REQUEST['name']}%')";
        Problem is that you use single quoutes twice ;-) Try this

        $name = $_REQUEST['name'];
        $query = "Insert into tbl_category (cat_descriptio n) Values ('%{$name}%')";

        --

        Petr Vileta, Czech republic
        (My server rejects all messages from Yahoo and Hotmail. Send me your mail
        from another non-spammer site please.)


        Comment

        • peter

          #5
          Re: PHP insert

          Problem is that you use single quoutes twice ;-) Try this
          >
          $name = $_REQUEST['name'];
          $query = "Insert into tbl_category (cat_descriptio n) Values
          ('%{$name}%')";
          no he isn't he is using single quotes within double quotes which is
          perfectly valid.

          Just a note to the op however. You are currently using $_REQUEST I would
          advise that you use either $_GET or $_POST although this is not cuasing the
          error it is something that is frowned upon unless you really HAVE too. The
          problem with using $_REQUEST is that you do not know where it has come from
          properly.

          As mentioned by another poster try using mysql_error to see what error
          message mysql is outputting. it might also help as also mentioned to infact
          show us the structure of your table.


          Comment

          • Colin Fine

            #6
            Re: PHP insert

            Bonzol wrote:
            Hello, PHP n00b here.
            >
            Using SQL just working off some examples, I have no problem selecting
            data, but I cant seem to be able to insert. If someone could see where
            im going wrong
            >
            >
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <title>Databa se Connection</title>
            </head>
            <body>
            <?php
            #Server Name - e.g. localhost:3309
            $dbserver = 'localhost';
            #Username used to log into MySQL database server.
            $dbusername = '';
            #Password used to log into MySQL database server.
            $dbpassword = '';
            #Error message shown if an error whilst attempt to establish a
            connection with the server.
            $dberrormessage = '<strong>Error connecting to database...</strong>';
            #Name of database on server.
            $dbdatabase = 'bonzollibrary' ;
            #Establish Connection
            @$conn = mysql_connect($ dbserver, $dbusername, $dbpassword) or exit
            ($dberrormessag e);
            #Selects which database is to be used on server.
            mysql_select_db ($dbdatabase);
            ?>
            <h1>Sample Database Connection</h1>
            >
            <form action="db-conn2.php" method="get">
            <input type="text" id="name" name="name" />&nbsp;
            <input type="submit" value="Search" />
            </form>
            >
            <?php if(isset($_REQU EST['name']))
            { ?>
            <table border="1">
            <tr>
            <th>Number</th>
            >
            </tr>
            "Insert into tbl_category (cat_descriptio n)
            Values ('%{$_REQUEST['name']}%')
            >
            >
            <?php $query = "Insert into tbl_category (cat_descriptio n) Values
            ('%{$_REQUEST['name']}%')";
            @$result = mysql_query($qu ery) or exit('<strong>A n error has
            occured while connecting to the database. The following query is
            >
            not valid: \''.$query.'\'. </strong>');
            }?>
            </table>
            <?php
            #Check if connection is still open
            if($conn)
            {
            #Close the connection
            mysql_close($co nn);
            }
            ?>
            </body>
            </html>
            >
            >
            Thanx in advance
            >
            You haven't told us what the problem actually is: do you get an error
            message? Does it apparently succeed but insert no rows? Does it insert
            empty rows?

            Colin

            Comment

            Working...