Code not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paul@sunnyland.com.au

    #1

    Code not working

    Hi could anyone please help me. I am a newbie to php. I am using php4
    and trying to put together some code to update a db but I am not having
    much luck. This is the code I have come up with so far.

    <?php

    if (isset($_POST['submitted'])) { // if the form has been submitted
    handle it

    // check the required form fields.
    if (!empty($_POST['JobNumber'])) {

    // Include MYSQL info
    require_once
    '/home/e-smith/files/ibays/mct/cgi-bin/mysql_connect.i nc.php';

    // Create the query
    $q = "INSERT INTO MyDB (JobNumber) VALUES ('{$_POST['JobNumber']}')";

    // Execute the query
    $r = mysql_query ($dbc, $q);

    //print a message indicating success
    if (mysql_affected _rows($dbc) == 1) {
    echo '<b><font color="green">I t Has Been entered!</font></b>';
    } else {
    echo '<b><font color="red">Sor ry it has failed!</font></b>';
    }

    Close the connection
    mysql_close($db c)

    } else { //print a message if they failed to enter a catagory
    echo '<b><font color="red">You forgot to enter a value</font></b>';
    }

    } else {
    ?>

    Add a new category to the Job Number:<br />
    <form action="add_exp ense_category.p hp" method="post">
    <input type="text" name="JobNumber " size="30" maxlength="30" /><br />
    <input type="hidden" name="submitted " value="true" />
    <input type="submit" name="submit" value="Submit!" />
    </form>

    <?php
    }
    ?>

    but for some reason it doesn't work. Am I missing something I have
    looked over it 100 times but everything seems fine. Can anyone help me
    out.

    Thanks in advance
    Paul

  • Emil

    #2
    Re: Code not working

    paul@sunnyland. com.au napisał(a):[color=blue]
    > Hi could anyone please help me. I am a newbie to php. I am using php4
    > and trying to put together some code to update a db but I am not having
    > much luck. This is the code I have come up with so far.
    >
    > <?php
    >
    > if (isset($_POST['submitted'])) { // if the form has been submitted
    > handle it
    >
    > // check the required form fields.
    > if (!empty($_POST['JobNumber'])) {
    >
    > // Include MYSQL info
    > require_once
    > '/home/e-smith/files/ibays/mct/cgi-bin/mysql_connect.i nc.php';
    >
    > // Create the query
    > $q = "INSERT INTO MyDB (JobNumber) VALUES ('{$_POST['JobNumber']}')";
    >
    > // Execute the query
    > $r = mysql_query ($dbc, $q);
    >
    > //print a message indicating success
    > if (mysql_affected _rows($dbc) == 1) {
    > echo '<b><font color="green">I t Has Been entered!</font></b>';
    > } else {
    > echo '<b><font color="red">Sor ry it has failed!</font></b>';
    > }
    >
    > Close the connection
    > mysql_close($db c)
    >
    > } else { //print a message if they failed to enter a catagory
    > echo '<b><font color="red">You forgot to enter a value</font></b>';
    > }
    >
    > } else {
    > ?>
    >
    > Add a new category to the Job Number:<br />
    > <form action="add_exp ense_category.p hp" method="post">
    > <input type="text" name="JobNumber " size="30" maxlength="30" /><br />
    > <input type="hidden" name="submitted " value="true" />
    > <input type="submit" name="submit" value="Submit!" />
    > </form>
    >
    > <?php
    > }
    > ?>
    >
    > but for some reason it doesn't work. Am I missing something I have
    > looked over it 100 times but everything seems fine. Can anyone help me
    > out.
    >
    > Thanks in advance
    > Paul
    >[/color]

    Is it the whole code? I can't see where you connect to database
    (mysql_connect( ), mysql_select_da tabase()).
    Anyway try to be more precise. What is the real problem?
    There is nothing in $_POST after submit or database returns an error?
    If there is database error then what it is? Try putting
    error_reporting (E_ALL) in the beginning of your script it could help
    to diagnose your problem.
    greetz Emil

    Comment

    • Kimmo Laine

      #3
      Re: Code not working


      "Emil" <emjot_wytnij_t o_@podczta.onet .pl> wrote in message
      news:e63eob$vch $1@news.onet.pl ...[color=blue]
      > Is it the whole code? I can't see where you connect to database
      > (mysql_connect( ), mysql_select_da tabase()).[/color]

      I'd assume those are called via this require:

      require_once
      '/home/e-smith/files/ibays/mct/cgi-bin/mysql_connect.i nc.php';

      --
      "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
      spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


      Comment

      • David Haynes

        #4
        Re: Code not working

        paul@sunnyland. com.au wrote:[color=blue]
        > Hi could anyone please help me. I am a newbie to php. I am using php4
        > and trying to put together some code to update a db but I am not having
        > much luck. This is the code I have come up with so far.[/color]
        [snip][color=blue]
        >
        > Close the connection
        > mysql_close($db c)
        >[/color]

        should be:
        // Close the connection
        mysql_close($db c);


        -david-

        Comment

        • Alan Little

          #5
          Re: Code not working

          Carved in mystic runes upon the very living rock, the last words of
          <paul@sunnyland .com.au> of comp.lang.php make plain:
          [color=blue]
          > but for some reason it doesn't work.[/color]

          "It doesn't work" isn't much to go on. Can you provide more detail? What
          are you expecting to happen, that doesn't, or not expecting, that does?



          Try outputting the query to be sure it's being properly formed. If there's
          a db error, check mysql_error().

          --
          Alan Little
          Phorm PHP Form Processor

          Comment

          • william.clarke@gmail.com

            #6
            Re: Code not working

            Usually when I'm debugging I 'echo' program flow (eg. "Attempting DB
            connection" so I can see at which point my logic has gone astray) and
            as many variable values as I think could be causing issues (often
            things like the actual query being run are a good place to start, as
            others have already suggested).

            Comment

            • paul@sunnyland.com.au

              #7
              Re: Code not working

              Thanks for all your help.

              I have put into action your suggestions and I have found the problem
              (turns out I missed a ; bugger!)

              anyway it was great to find out the ways to debug.

              Thanks again.

              Paul

              Comment

              Working...