Jump to content

Data truncated for column 'sale_price at row 1.


Deeda

Recommended Posts

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> 
[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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.