Jump to content

Empty Value in MySQL Database?


funstein

Recommended Posts

Hello everyone,

I am working on the registration script of an academic conference. In the form, there are 3 registration types, which are to Regular, Student, Reduced. If the person has checked student, the script checks if his paper is listed as a student, and if they choose Reduced, the scripts check if their country is in the array of reduced countries. Then, the type is inserted into mysql alongside other information.

 

So here is the problem : I see blanks in phpMyAdmin. All other fields are OK while this one is not.

  Capture.PNG

 

Here is the code :

(All variables are defined correctly, only parts related to the problem are here. MySQL connection is OK.)

 

 





//check if fee conditions are met
if($formtype == "student") {
    if($check['student'] == "T") {
        $warning['studentwarning'] = false ;
        $type = "Student" ;
    }
    else{
        $warning['studentwarning'] = true ;
        $type = "Regular" ;
    }
}
elseif($formtype == "reduced") {
    if(in_array($country, $reducedcountries)) {
        $warning['reducedwarning'] = false ;
        $type = "Reduced" ;
    }
    else{
        $warning['reducedwarning'] = true ;
        $type = "Regular" ;
    }
}
else {
    $type == "Regular" ;
}


$type = mysql_real_escape_string($type);



mysql_query("INSERT INTO registration (type) VALUES('$type') ") 
or die(mysql_error());






 

Hope someone can help me. Thanks,

Funstein

Link to comment
Share on other sites

You probably want to start doing test cases and echoing/var_dumping the $formtype and $type variables before running the query. Also could echo out the query how does it look like when you do the tests. And see based on this what happens and when, then try to figure out why.

Link to comment
Share on other sites

I have tried lots of different ways and I havent been able to come across this problem myself. Do you think you can help if I send you the whole code? Because this is overwhelming and I am going to be stuck with lots of untagged information if this goes on.

Link to comment
Share on other sites

Yes I think I can help, but I am at work at the moment and I do not have too much extra time to work on big files (if yours is big). Better option would be probably to paste the relevant codes in here on the forum inside the code tags so other people also can read them and maybe find you an answer for your problem.

 

Some questions:

1. What debugging you have done to find out the source of the problem?

2. Do you have error reporting turned on?

Link to comment
Share on other sites

Some questions:

1. What debugging you have done to find out the source of the problem?

2. Do you have error reporting turned on?

 

I havent done any of these, because I cant simulate the error, I dont know why. None of my queries end up empty in the database. I'd start echoing variables but it wont help a lot since my queries are OK. I'm sending you the source code now, with MySQL information blanked.

Link to comment
Share on other sites

Well if you have not done any of these how can you know your variables and queries are OK? Add

ini_set('display_errors', 1);
error_reporting(-1);

in the beginning of your scripts to see if there is any errors. And do some variable / query debugging like I said few posts ago to see if your query / variables have the data that you expect them to have.

Link to comment
Share on other sites

Have you tried submitting the form leaving some fields empty or something similar? Think what the users could do. Do you validate the form data good enough? Checking for empty values etc? Is there any other wai users could insert data to these tables than this form?

Link to comment
Share on other sites

There is no other way of insertion to the table. I have sent the code to you by PM, you can see that there is a validation, it kicks back if the field is null, and as the field is checkboxes, people have to select either regular,reduced or student. Or it kicks them back to the form and tells them to pick one.

Link to comment
Share on other sites

Keeping track of more information, such as the date the record was added, and the user id if someone is logged in, may help troubleshoot the issue.  The code to me looks like it should always insert a $type, but just in case you could try restructuring your code to something like this:

 

//Set default values
$warning['reducedwarning'] = true ;
$type = "Regular" ;

//check if fee conditions are met
if($formtype == "student") {
    if($check['student'] == "T") {
        $warning['studentwarning'] = false ;
        $type = "Student" ;
    }
}
elseif($formtype == "reduced") {
    if(in_array($country, $reducedcountries)) {
        $warning['reducedwarning'] = false ;
        $type = "Reduced" ;
    }
}

$type = mysql_real_escape_string($type);

mysql_query("INSERT INTO registration (type) VALUES('$type') ") 
or die(mysql_error());

 

It may be that those records were added when you had coded this differently, or something along those lines.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.