Need help finding a bug...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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

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.


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']);
            $sql = "SELECT MAX(id) FROM clients";
            $rs = mysql_query($sql);
            $row = mysql_fetch_array($rs);
            $cnum = $row['id'] +1;

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

Re: Need help finding a bug...

Anthony wrote:
Quoted text here. Click to load it

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', ...)


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

i.e. the values want to go in a list after the VALUES keyword.

Quoted text here. Click to load it

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.


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

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


Site Timeline