nomis Posted August 3, 2009 Share Posted August 3, 2009 Hi, sorry if this is a dumb question, but I've been searching for help for this issue for a while in vain. I'm trying to get my form to pass a real null value when there is nothing entered, but to pass along the correct value if the data has been entered. For example, I have a field for "middle_name" for individuals that is not required. Here is a simplified version of a query from one of my update screens "UPDATE person SET person_type = '$person_type' , last_name ='$last_name' , first_name = '$first_name' , middle_name = '$middle_name' " ; the PHP form inputs a blank character instead of an actual null value into the middle_name field. If I remove the single quote marks from this field, as below: "UPDATE person SET person_type = '$person_type' , last_name ='$last_name' , first_name = '$first_name' , middle_name = $middle_name " ; it transmits nulls fine, but if I try to include an entry for this field (for instance "John") I get an error stating: Error: Unknown column 'John' in 'field list' I have tried quite a few things, but mostly all they do is enter a text string of "NULL" into the database, which of course is not the same thing as a null value. for instance this if (!empty($_POST['middle_name'])) $middle_name = $_POST['middle_name']; else $middle_name = "NULL"; just enters "NULL" into the database. If I try changing the variable to $middle_name = "NULL"; it just errs. Can someone help? Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/ Share on other sites More sharing options...
nomis Posted August 3, 2009 Author Share Posted August 3, 2009 Okay, I feel dumb. I finally figured out what was wrong... if you remove the quotes from the query, you need to re-enter them if there actually is a value, or the sql query freaks out. Here's what I used: if (!empty($_POST['middle_name'])) $middle_name = "'$middle_name'"; else $middle_name = 'NULL' ; Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-889240 Share on other sites More sharing options...
roopurt18 Posted August 3, 2009 Share Posted August 3, 2009 Just a small point. It's generally a good idea to include curly brackets even though they might be optional. The rationale is you may later add a statement (such as for debugging) and then forget to add the curly brackets. Then your code may do all sorts of weird things and you'll go on some pretty wild goose chases. if (!empty($_POST['middle_name'])) { $middle_name = "'$middle_name'"; }else{ $middle_name = 'NULL' ; } Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-889281 Share on other sites More sharing options...
nomis Posted August 3, 2009 Author Share Posted August 3, 2009 thanks, good advice. However, I've run into another minor problem. The results of my update into a form (the same form used to submit the data, so that more changes can be made if necessary), which echos any data entered back to the form. If there is a null, it prints "NULL" on the form, which is okay, but if a user submits the form again, it enters a text string of "NULL" back into the table. It works fine if the "NULL" is deleted, but this could cause problems if a user is not careful. This is how it looks <tr> <td><label for "middle_name">Middle Name:</label> </td> <td><input type="text" id="middle_name" name="middle_name" value="<?php echo $middle_name; ?>" /> </td> </tr> I tried putting an if statement into the form field like so: <tr> <td><label for "middle_name">Middle Name:</label> </td> <td><input type="text" id="middle_name" name="middle_name" value="<?php if ($middle_name == NULL) echo $middle_name; ?>" /> </td> </tr> however, though the data is successfully updated, returns a null value in all instances. I'd like to have it return the actual value if possible. Any suggestions? (yes, I know I left out the curly brackets) I feel like I'm stuck in an endless loop Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-889761 Share on other sites More sharing options...
roopurt18 Posted August 3, 2009 Share Posted August 3, 2009 You're printing $middle_name if it is EQUAL TO null. Instead you want to print it when it is NOT EQUAL TO null. if( $middle_name != null ) <-- NOT EQUAL Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-889782 Share on other sites More sharing options...
nomis Posted August 3, 2009 Author Share Posted August 3, 2009 Doh! of course. thanks again. This should work fine (except that because of my earlier solution, the results come back in single quotes... this shouldn't be a problem unless someone submits the form again without removing those quotes... is there an escape string that will work to remove those in the display?) Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-889852 Share on other sites More sharing options...
roopurt18 Posted August 3, 2009 Share Posted August 3, 2009 I'm not sure exactly what you mean, but trim(), rtrim(), or ltrim() might be what you're looking for. $var = "'hello there'"; // 'hello there' echo trim( $var, "'" ); // prints: hello there echo ltrim( $var, "'" ); // prints: hello there' echo rtrim( $var, "'" ); // prints: 'hello there Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-889872 Share on other sites More sharing options...
nomis Posted August 3, 2009 Author Share Posted August 3, 2009 okay, that sortof works, but now, instead of 'NULL' appearing in the field, just NULL appears upon submit. A true null has been entered into the database, but as the form comes back with the world NULL, if it is submitted again, the text string NULL goes right back in the database. Is there a way to eliminate an entire word (in this case the word "NULL") using an else statement? something like this? if ($middle_name != null) { echo trim($middle_name,"'") ; } else echo trim($middle_name,"NULL"); note, tried that and it didn't work. funny how the easy ones end up never ending, eh? Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-889938 Share on other sites More sharing options...
roopurt18 Posted August 3, 2009 Share Posted August 3, 2009 1) User submits the form. 2) NULLs go into the database. 3) The form reappears, some fields say NULL in them. 4) User submits the form again. 5) The database now has the string 'NULL' in it. Is this what is happening? Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890034 Share on other sites More sharing options...
nomis Posted August 3, 2009 Author Share Posted August 3, 2009 yes, exactly. any ideas? I'd rather not have the word null not appear ever; just a blank space that gets translated as null on post. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890087 Share on other sites More sharing options...
roopurt18 Posted August 3, 2009 Share Posted August 3, 2009 <?php // Assume connect to mysql is made if( empty( $_POST ) ) { // // DISPLAY FORM // Here we retrieve value from database based on GET parameter named 'id' $sql = "select `column_name` from `thetable` where id='" . mysql_real_escape_string( $_GET['id'] ) . "'"; $value = ''; // Initialize value to empty string $q = mysql_query( $sql ); if( $q ) { $row = mysql_fetch_object( $q ); if( $row ) { $value = $row->column_name; // If we made it this far then the value was retrieved } } // if value has zero length it is set to zero-length string // this effectively turns raw NULL from the database into empty string $value = !strlen( $value ) ? "" : htmlentities( $value ); // dump the form echo <<<FORM <form action="{$_SERVER['REQUEST_URI']}" method="post"> <input type="text" name="txtTest" value="{$value}" /> <input type="submit" value="Submit" name="btnSubmit" /> </form> FORM; }else{ // // INSERT INTO DATABASE // Extract value from $_POST values if it's there and positive length; // otherwise we'll insert a NULL into the database $value = isset( $_POST['txtTest'] ) && strlen( trim( $_POST['txtTest'] ) ) ? "'" . mysql_real_escape_string( $_POST['txtTest'] ) . "'" : 'NULL'; $sql = "insert into `thetable` ( `column_name` ) values ( {$value} )"; mysql_query( $sql ); echo mysql_affected_rows() > 0 ? 'success' : 'failure'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890140 Share on other sites More sharing options...
nomis Posted August 3, 2009 Author Share Posted August 3, 2009 wow, you did a lot of rewriting here... unfortunately, I'm still relatively new to this and am not able to clearly understand object-oriented models. I suppose if I spend some time with this I'll figure it out. (in the meantime, I did figure out the problem has to do with some of my loops... if they go through the first time it's okay, but on subsequent times, the way I have it working, I've ended up renaming the values of some of the variables, so they will continue changing with the echo statement inside the form fields). if you want I can show you the entire script (which is almost done; I'd hate to have to rewrite this as well as four other ones in a new way... ). Thanks; you've been particularly good in responding... I do appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890148 Share on other sites More sharing options...
roopurt18 Posted August 4, 2009 Share Posted August 4, 2009 The only object oriented thing in that script is mysql_fetch_object() and the only effect it has is how you access columns in the returned database row. As an example: <?php mysql_query( "insert into `some_table` ( 'first_name', 'last_name' ) values ( 'Larry', 'Smith' )" ); $q = mysql_query( "select `first_name`, `last_name` from `some_table`" ); $row = mysql_fetch( $q ); echo $row[0]; // Larry $q = mysql_query( "select `first_name`, `last_name` from `some_table`" ); $row = mysql_fetch_assoc( $q ); echo $row['first_name']; // Larry $q = mysql_query( "select `first_name`, `last_name` from `some_table`" ); $row = mysql_fetch_object( $q ); echo $row->first_name; // Larry ?> Everything else in my post was demonstrating how you properly convert empty POST values into NULL to insert into the database and how to take NULLs from the database and use them as value-attributes in INPUT-tags. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890219 Share on other sites More sharing options...
nomis Posted August 4, 2009 Author Share Posted August 4, 2009 okay, thanks. I also realized (subsequently) that none of the data is coming from the database; I'm only grabbing the Post variables that have been entered and echoing them to the form fields if there is a missing required field (not the null fields, but other fields; I just wanted everything entered to be echoed back to the form so the user wouldn't need to keep re-typing the same thing... the issue is dealing with the blank fields. I believe it has something to do with the way I'm echoing these variables. You use this: // Here we retrieve value from database based on GET parameter named 'id' $sql = "select `column_name` from `thetable` where id='" . mysql_real_escape_string( $_GET['id'] ) . "'"; $value = ''; // Initialize value to empty string $q = mysql_query( $sql ); if( $q ) { $row = mysql_fetch_object( $q ); if( $row ) { $value = $row->column_name; // If we made it this far then the value was retrieved } } which is grabbing data from the database... is this the best way of ensuring the nulls remain null? I'd rather not have anything enter the tables until I'm certain that there are no nulls being entered. The way I have it works fine with the exception of a user resubmitting the erred form (it gets worse with each resubmit). I've figured out why; it appears I am renaming the null variables adding more and more quotes with each subsequent error with this bit of code if (!empty($_POST['middle_name'])) { $middle_name = "'$middle_name'"; }else{ $middle_name = 'NULL' ; } ... I'd rather have it not echo anything of there is nothing entered. Am I making this too difficult? Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890769 Share on other sites More sharing options...
premiso Posted August 4, 2009 Share Posted August 4, 2009 You need to provide us with more code. Where does the data get checked and entered into the database? As the issue seems to lie within that. You are not doing enough checks before data is entered as it seems no matter what is entered it goes into the database anyways. You need to add a check before the data gets entered to not enter into the DB if there was a null or error on a required field. If I am missing something I am sorry, just an observation I noticed. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890821 Share on other sites More sharing options...
roopurt18 Posted August 4, 2009 Share Posted August 4, 2009 More code would be helpful. You are displaying 1 form. The form will be pre-populated with values when either: 1) It is loaded from the database 2) It is submitted by the user but with errors, thus it must be re-displayed. How you check for empty values to populate the form depends on 1 or 2. It is NOT typically the same for both. Post more code and add comments on where you think it may be going wrong if you want more specific help. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890885 Share on other sites More sharing options...
nomis Posted August 4, 2009 Author Share Posted August 4, 2009 Thanks... I apologize, I will provide more code (yes there are validation checks, which may be at the root of the problem). I tried your modifications, and got exactly the same problems I had earlier, so it could be my problem is with the validation checks, which are what bring up the form with pre-populated values. Here is a large chunk of what I had originally as person.php . There are more fields, but I'm testing on just variable. (I know I define my variables at the top, and it's not necessary, but it's just an old habit, and makes for fast reference to me... I don't think they'll cause a problem?) (Note that the table person also has an auto increment primary key, which is not mentioned below) <?php //database connection require_once ('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //form variables if (isset($_POST['submit'])) { $person_type = $_POST['person_type']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $middle_name = $_POST['middle_name']; $output_form = false; //if any required fields are missing produce error.. simple check once, rather than each if ((empty($person_type)) || (empty($first_name)) || (empty($last_name)) { echo '<h3>Missing Data</h3>'; echo 'You need to enter at least person type, first name, last name <br />'; $output_form = true; } //make sure NULL values actually get passed as NULL if (!empty($_POST['middle_name'])) { $middle_name = "'$middle_name'"; } else { $middle_name = 'NULL' ; } //If all required exist, report and execute insert query if ((!empty($person_type)) && (!empty($first_name)) && (!empty($last_name)) { // insert values into table $query = "INSERT into person (person_type, last_name, first_name, middle_name)" . "VALUES ('$person_type','$last_name','$first_name',$middle_name)"; $result = mysqli_query($dbc, $query) or die ('Error: '.mysqli_error($dbc)); //echo data entry results to the browser echo 'You entered: <p />';echo 'Person Type: ' . $person_type . '<br />'; echo 'First name: ' . $first_name . '<br />'; echo 'Last name: ' . $last_name . '<br />'; echo 'Middle name: ' . $middle_name . '<br />'; echo '<a href="person.php">Enter another person</a> | <a href="ppmanage.php>Manage Entries</a>'; } } else { $output_form = true; } if ($output_form) { ?> <table width=500 border=0 cellpadding=5> <tr> <td><label for "person_type">Person Type:</label> </td> <td><input type="text" id="person_type" name="person_type" value="<?php echo $person_type; ?>" /> </td> </tr> <tr> <td><label for "last_name">Last Name:</label> </td> <td><input type="text" id="last_name" name="last_name" value="<?php echo $last_name; ?>" /> </td> </tr> <tr> <td><label for "first_name">First Name:</label> </td> <td><input type="text" id="first_name" name="first_name" value="<?php echo $first_name; ?>" /> </td> </tr> <tr> <td><label for "middle_name">Middle Name:</label> </td> <td><input type="text" id="middle_name" name="middle_name" value="<?php echo $middle_name; ?>" /> </td> </tr> </table> <br /> <p> <input type="submit" value="Submit" name="submit" /> </p> </form> <?php } mysqli_close($dbc); ?> Here are the modifications I made with your suggestions with which I got identical results: <?php //database connection require_once ('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //form variables if (isset($_POST['submit'])) { $person_type = $_POST['person_type']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $middle_name = $_POST['middle_name']; $output_form = false; //Validate required variables //if any required fields are missing produce error if ((empty($person_type)) || (empty($first_name)) || (empty($last_name)) { echo '<h3>Missing Data</h3>'; echo 'You need to enter at least person type, first name, last name. <br />'; $output_form = true; } //make sure NULL values actually get passed as NULL if( empty( $_POST ) ) { $sql = "select middle_name from person where person_id='" . mysqli_real_escape_string( $_GET['person_id'] ) . "'"; $middle_name = ''; // Initialize value to empty string $q = mysqli_query( $sql ); if( $q ) { $row = mysqli_fetch_object( $q ); if( $row ) { $middle_name = $row->middle_name; // If we made it this far then the value was retrieved } } $middle_name = !strlen( $middle_name ) ? "" : htmlentities( $middle_name ); } else { //is there an entry with any length $middle_name = isset( $_POST['middle_name'] ) && strlen( trim( $_POST['middle_name'] ) ) ? "'" . mysql_real_escape_string( $_POST['middle_name'] ) . "'" : 'NULL'; } //If all required exist, report and execute insert query if ((!empty($person_type)) && (!empty($first_name)) && (!empty($last_name)) { //database insert $query = "INSERT into person (person_type, last_name, first_name, middle_name)" . "VALUES ('$person_type','$last_name','$first_name',$middle_name)"; $result = mysqli_query($dbc, $query) or die ('Error: '.mysqli_error($dbc)); //echo data entry results to the browser echo 'You entered: <p />';echo 'Person Type: ' . $person_type . '<br />'; echo 'First name: ' . $first_name . '<br />'; echo 'Last name: ' . $last_name . '<br />'; echo 'Middle name: ' . $middle_name . '<p />'; echo '<a href="person.php">Enter another person</a> | <a href="ppmanage.php>Manage Entries</a>'; } } //if there is no submission, show the form else { $output_form = true; } if ($output_form) { ?> <h3>Person Entry</h3> <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <table width=500 border=0 cellpadding=5> <tr> <td><label for "person_type">Person Type:</label> </td> <td><input type="text" id="person_type" name="person_type" value="<?php echo $person_type; ?>" /> </td> </tr> <tr> <td><label for "last_name">Last Name:</label> </td> <td><input type="text" id="last_name" name="last_name" value="<?php echo $last_name; ?>" /> </td> </tr> <tr> <td><label for "first_name">First Name:</label> </td> <td><input type="text" id="first_name" name="first_name" value="<?php echo $first_name; ?>" /> </td> </tr> <tr> <td><label for "middle_name">Middle Name:</label> </td> <td><input type="text" id="middle_name" name="middle_name" value="<?php echo $middle_name; ?>" /> </td> </tr> </table> <br /> <p> <input type="submit" value="Submit" name="submit" /> </p> </form> <?php } mysqli_close($dbc); ?> I think the problem may be in the verification loop? This works fine, but it does a number on the entries. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-890982 Share on other sites More sharing options...
nomis Posted August 5, 2009 Author Share Posted August 5, 2009 Okay, I finally figured it out. It had nothing to do with the way we were assigning variables but did have to do at least partially with the verification loop. I started over and relooked at my earliest assumptions and discovered it was the way that the data was being echoed back to the form if there was an error. The wasn't checking to see whether a value was equal to NULL, but whether there was anything in the value field at all. This is what worked: <input type="text" id="middle_name" name="middle_name" value="<?php if (empty($middle_name)) echo $middle_name; else echo null; ?>" /> I still thank you for all your help and I learned a few new things in the process. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-891047 Share on other sites More sharing options...
nomis Posted August 5, 2009 Author Share Posted August 5, 2009 Okay, I spoke to soon. of course,what I want instead is <?php if (!empty($middle_name)) echo $middle_name; ?> And of course, I'm talking about two scripts instead of one (ARGH... don't kill me please). One is the data entry form (person.php), and the other is the edit form (edit.php). They use almost identical code. The difference is this: person.php (the one I showed you)- checks for valid data, if not, it returns the form with the items that had been entered pre-filled. When doing so, all the required fields report fine (because I have not needed to run a validation check on these for nulls). If one of the required fields is not included AND middle_name is NOT filled out, it brings back the form, and the word NULL appears in the form under middle_name. The user may then correct this, submit, and then the word "NULL" is entered into the database. edit.php - looks exactly like the person.php field, but retrieves its values using GET from a separate page. On first click, this form correctly brings in empty fields for values that are not entered, because if there really is a null in the database (the ppmanage.php form does a select on the database to get all values, then transmits these values into an HTML link which appends the data; this script works with no issues). However, if someone submits this form a second time, the true nulls (the empty values) get converted into "NULL" because of the same null control being done on the data (as shown earlier, with either the version I did or the one suggested by roopurt1). I know I can change the edit.php script to only allow one data change (which is at least a partial solution, if not ideal, and someone could still find a way to get bad data into the database, simply by deleting required fields and getting an error). Anyway, my real question is whether or not there is a function in php to assign an EMPTY value to a text field. In other words, I can use if(empty()) to determine if a field is empty... is there a way to do this backwards, and assign a variable or at least the contents of an HTML field to be truly empty? I'm thinking I can use unset to remove the word "NULL", but I know this is bad syntax, but something like this: if ($middle_name == 'null') $middle_name = unset($middle_name); I tried this and it didn't err, but of course it didn't work either. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-891081 Share on other sites More sharing options...
roopurt18 Posted August 5, 2009 Share Posted August 5, 2009 If you want the input to have an empty value, then the input must look something like this: <input type="text" name="blahblah" id="blahblah" value="" /> It's the value="" that makes it empty. In the part of code right before you echo your form, add this: <?php // bunch of stuff // now right before you echo the form add: echo '$person_type: '; var_dump( $person_type ); echo '<br />'; echo '$first_name: '; var_dump( $first_name ); echo '<br />'; echo '$last_name: '; var_dump( $last_name ); echo '<br />'; echo '$middle_name: '; var_dump( $middle_name ); echo '<br />'; // now echo the form... ?> Now take very careful note of what the values actually are when you expect them to be empty? Are they what you expect them to be? If they aren't, either fix your existing code so they are what you expect or add code to make them empty. In this case, empty should be either the empty string '' or the value null. As an example: <?php if( /* do a test for $middle_name IS empty */ ) { $middle_name = ''; } ?> <input type="text" name="middle_name" id="middle_name" value="<?php echo $middle_name; ?>" /> Also keep in mind the null value is NOT the same as the string containing the characters N, U, L, L. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-891085 Share on other sites More sharing options...
nomis Posted August 5, 2009 Author Share Posted August 5, 2009 I do understand the difference between a null string and a null value; that's why I've been trying to get this straightened out; I really want real nulls to appear in the table if nothing is entered in the non-required fields. Here's what I tried if (empty($_POST['middle_name'])) { $middle_name = "" ; } else { $middle_name = "'$middle_name'" ; } And purposely did not enter a required field and got this output. The results are got were: $first_name: string(0) "" $last_name: string(5) "Smith" $middle_name: string(0) "" which should be good... I'm getting a string length of zero for $middle_name. Then I add in a first name and submit. It first gives this SQL error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 Here's the query line: $query = "INSERT into person (last_name, first_name, middle_name)" . "VALUES ('$person_type','$last_name','$first_name',$middle_name)" ; I'm familiar with this problem, because I've been fighting with this for a while, so I add quotes back around $middle_name to be this: $query = "INSERT into person (last_name, first_name, middle_name)" . "VALUES ('$last_name','$first_name','$middle_name')" ; and script works as suspected, but instead of inserting a NULL value into the person for $middle_name, it inserts a blank space, which was the initial problem. I also tried getting the var_dump information with a succesful input and got these results (in blue) $first_name: string(4) "John" $last_name: string(5) "Smith" $middle_name: string(0) "" You entered: First name: John Last name: Smith Middle name: Though Middle name appears blank, it is actually in the database as " " (a blank space) The form field has this: <input type="text" id="middle_name" name="middle_name" value="<?php echo trim($middle_name) ; ?>" /> The way to get a true null in the database seems to be change the query to take $middle_name instead of '$middle_name', but the only way I can seem to make this work is putting the word "NULL" instead of the "" in the conditional. Then of course, I end up with a string of 4, which is NOT what I want, because that will echo the word "NULL" into the input field if there is an error, which again will then go into the database as the text string "NULL" I'm stuck in a loop still Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-891113 Share on other sites More sharing options...
roopurt18 Posted August 5, 2009 Share Posted August 5, 2009 $query = "INSERT into person (last_name, first_name, middle_name)" . "VALUES ('$person_type','$last_name','$first_name',$middle_name)" ; The query above, without the single quotes around $middle_name, is the proper query to use. As soon as you add the single quotes MySQL will treat the value as a string and will insert whatever is in $middle_name as a string, be it zero length or not. So right above that query, you need to have something like: if( strlen( trim( $middle_name ) ) { $middle_name = 'NULL'; // Set middle_name to the string NULL, which MySQL will insert as the value NULL because it is not wrapped in single quotes }else{ $middle_name = "'" . mysqli_real_escape_string( $middle_name ) . "'"; } $query = "INSERT into person (last_name, first_name, middle_name)" . "VALUES ('$person_type','$last_name','$first_name',$middle_name)" ; Now, by the time you've gotten to the part of your code where you output the form, $middle_name is tainted. It is no longer what the user submitted because you have either: 1) Set it to the string NULL 2) Or wrapped it in single quotes and mysqli_real_escape_string()'ed it So right before outputting the form, reset $middle_name back to what the user submitted. <?php // database stuff above $middle_name = $_POST['middle_name']; // Now echo your form ?> <!-- some html --> <input type="text" name="middle_name" id="middle_name" value="<?php echo $middle_name; ?>" /> <!-- finish the html --> Since $middle_name has been reset to the value in $_POST, the form input will get what the user initially entered and you are probably done. Unless... Your server has Magic Quotes turned on. If that's the case there's one more thing we must do. Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-891481 Share on other sites More sharing options...
nomis Posted August 5, 2009 Author Share Posted August 5, 2009 Okay, I fixed the query back to what it should be (which is what I expected). Then I removed my earlier NULL check, and entered in if ( strlen( trim($middle_name) )) { $middle_name = 'NULL'; // Set middle_name to the string NULL, which MySQL will insert as the value NULL because it is not wrapped in single quotes }else{ $middle_name = "'" . mysqli_real_escape_string( $dbc,$middle_name ) . "'"; } When entering nothing in the $middle_name, I got these results: $first_name: string(4) "John" $last_name: string(3) "Doe" $middle_name: string(4) "NULL" You entered: First name: John Last name: Doe Middle name: NULL However, even though the string length appears as 4, it really did insert a null into the tables (which is good). Unfortunately, when I entered a name into the middle name field, I got exactly the same results; the script seems to be interpreting anything in the $middle_name field as null. I did enter $middle_name = $_POST['middle_name']; in the same routine that echoes the form, but it seemed to make no difference... it outputs a null no matter what I do... what am I doing wrong? I did also check to see if there were magic quotes turned on, using echo get_magic_quotes_gpc() and it returned a "1," so they ARE apparently on... would that make the difference? I would have thought that this would have prevented NULLs from going in properly, but not from submitting actual values. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-891562 Share on other sites More sharing options...
nomis Posted August 5, 2009 Author Share Posted August 5, 2009 I should add that the form is echoing the right values properly (blank if NULL, the value if a value is entered), it's just submitting NULL no matter what Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-891566 Share on other sites More sharing options...
roopurt18 Posted August 5, 2009 Share Posted August 5, 2009 I'm sorry, the if should be as follows: if ( strlen( trim($middle_name) ) === 0 ) { $middle_name = 'NULL'; // Set middle_name to the string NULL, which MySQL will insert as the value NULL because it is not wrapped in single quotes }else{ $middle_name = "'" . mysqli_real_escape_string( $dbc,$middle_name ) . "'"; } Quote Link to comment https://forums.phpfreaks.com/topic/168580-solved-passing-null-varchar-value-from-php-form-to-mysql/#findComment-891607 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.