Need help finding a bug...

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

    #1

    Need help finding a bug...

    Hello Everyone,

    I have been working with a piece of code for the past few hours that I
    just can't seem to get working. I'm hoping someone here can help me
    find the bug. Basically, the code is supposed to take all the data
    entered into a forms fields, stripslashes() it then insert it into the
    database. Pretty straightforward . The only thing that is unique is
    that I am checking if a particular field is blank then allowing the
    program to auto-assign a number to the field if it is. Still. pretty
    straightforward .

    What's happening, however, is that the information is never being
    entered into the database. When I echo the sql statement being
    constructed back it looks right. But I am sure there is a problem
    somewhere. Can anyone help me find it? I will pay you $10 via paypal if
    you can help me here. The code is below.

    Thanks,
    Anthony

    if($_POST['fname'] != "" && $_POST['minit'] != "" && $_POST['lname'] !=
    "" && $_POST['ssn'] != ""){

    $fname = stripslashes($_ POST['fname']);
    $minit = stripslashes($_ POST['minit']);
    $lname = stripslashes($_ POST['lname']);
    $ssn = stripslashes($_ POST['ssn']);
    $phone = stripslashes($_ POST['phone']);
    $cdate = date("Y-m-j");
    if($_POST['cnum'] != ""){
    $cnum = stripslashes($_ POST['cnum']);
    }
    else{
    include("db.php ");
    $sql = "SELECT MAX(id) FROM clients";
    $rs = mysql_query($sq l);
    $row = mysql_fetch_arr ay($rs);
    $cnum = $row['id'] +1;
    }
    include_once("d b.php");

    $sql = "INSERT INTO clients(' ', '$fname', '$minit', '$lname',
    '$ssn', '$phone', '$cdate', '$cnum'";
    echo $sql;
    //exit;
    mysql_query($sq l);
    echo "<font size=\"+1\"><b> ACCOUNT CREATED</b></font><br>";
    echo "The new account for ".$fname." ".$minit." ".$lname." has been
    successfully created.";
    exit;
    }

  • Tim Martin

    #2
    Re: Need help finding a bug...

    Anthony wrote:[color=blue]
    > Hello Everyone,
    >
    > I have been working with a piece of code for the past few hours that I
    > just can't seem to get working. I'm hoping someone here can help me
    > find the bug. Basically, the code is supposed to take all the data
    > entered into a forms fields, stripslashes() it then insert it into the
    > database. Pretty straightforward . The only thing that is unique is
    > that I am checking if a particular field is blank then allowing the
    > program to auto-assign a number to the field if it is. Still. pretty
    > straightforward .
    >
    > What's happening, however, is that the information is never being
    > entered into the database. When I echo the sql statement being
    > constructed back it looks right. But I am sure there is a problem
    > somewhere. Can anyone help me find it? I will pay you $10 via paypal if
    > you can help me here. The code is below.[/color]
    [color=blue]
    > $sql = "INSERT INTO clients(' ', '$fname', '$minit', '$lname',
    > '$ssn', '$phone', '$cdate', '$cnum'";[/color]

    I can't see a matching close bracket on that statement.

    Also, you should be doing either

    INSERT INTO clients (field1, field2, field3, ...) VALUES (' ', '$fname',
    '$minit', ...)

    or

    INSERT INTO clients VALUES (' ', '$fname', '$minit', ...)

    i.e. the values want to go in a list after the VALUES keyword.
    [color=blue]
    > echo $sql;
    > //exit;
    > mysql_query($sq l);
    > echo "<font size=\"+1\"><b> ACCOUNT CREATED</b></font><br>";
    > echo "The new account for ".$fname." ".$minit." ".$lname." has been
    > successfully created.";
    > exit;[/color]

    Note that you're ignoring the error status here. Check the return value
    of mysql_query() to determine whether an error occurred, and if so echo
    the result of mysql_error(). Had you done this you'd probably have
    realised you had a syntax error in your SQL.

    Tim

    Comment

    • Anthony

      #3
      Re: Need help finding a bug...

      Thanks Tim,

      Looks like ignoring the return code proved to be my downfall. I wasn't
      aware that MySQL returned a status code on an insert. You were right,
      of course, that my SQL statement was incomplete. After I changed it, it
      worked.

      Thanks again and please check your inbox for a personal email from me.

      Anthony

      Comment

      Working...