Deeda Posted November 1, 2006 Share Posted November 1, 2006 I am having a problem with a form I use to add a record. I have created several fields, but I run into a problem if I leave a float or a date field blank. For example I have a field called sale_price. If I leave it blank, I'll get the following message "Data truncated for column 'sale_price at row 1. " If I put a number in there is no problem. How can I set it up so I have the choice of leaving it blank if so desired? <html> <head> <title> Closing Insert Record</title> </head> <body> <?php $mysqli = mysqli_connect("localhost", "kelly", "ki", "lytledb"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } else { $sql = "INSERT INTO closing (date_added, date_modified, sale_price, buyer_settlement_fee) VALUES (now(), now(), '".$_POST["sale_price"]."','".$_POST["buyer_settlement_fee"]."')"; $res = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli)); mysqli_close($mysqli); } ?> <form method= "POST" action="closing_insert_form.php"> <input type="submit" value="Insert Another Record"> </form> <form method= "POST" action="closing_interface.php"> <input type="submit" value="Closing Interface"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/25811-data-truncated-for-column-sale_price-at-row-1/ Share on other sites More sharing options...
trq Posted November 1, 2006 Share Posted November 1, 2006 [code=php:0]if (!empty($_POST['sale_price'])) { $sql = "INSERT INTO closing (date_added, date_modified, sale_price, buyer_settlement_fee) VALUES (now(), now(), '".$_POST["sale_price"]."','".$_POST["buyer_settlement_fee"]."')";} else { $sql = "INSERT INTO closing (date_added, date_modified, buyer_settlement_fee) VALUES (now(), now(),'".$_POST["buyer_settlement_fee"]."')";}[/code]Of course the field will need to be setup to accept NULL values also.PS: You really should look into varifying the integrity of your data before inserting it aswell. ATM your are leaving yourslef wide open to sql injection. Link to comment https://forums.phpfreaks.com/topic/25811-data-truncated-for-column-sale_price-at-row-1/#findComment-117868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.