添加2位整数mysql时更新错误

时间:2018-02-21 14:54:57

标签: mysql insert

当我插入一个双位数整数时,我在mySql插页上出错了。当插入单个数字整数值时,一切正常,但对于双位数,例如18,我得到一个外键约束错误(下面),该列将默认为1.

  

无法添加或更新子行:外键约束失败(deposit_guardtenants,CONSTRAINT tenants_to_properties FOREIGN KEY(listing_id)参考propertiesproperty_id))

插入代码:

session_start();
require_once('../dbconnect.php');
if(isset($_POST["t_name"]))
/* multiline add to database and changed for my purposes http://www.webslesson.info/2017/03/multiple-inline-insert-data-using-ajax-jquery-php.html */
{
$t_name = $_POST["t_name"];
$t_email = $_POST["t_email"];
$t_deposit = $_POST["t_deposit"];
$t_start = $_POST["t_start"];
$t_duration = $_POST["t_duration"];
$prop_id = $_POST["prop_id"];
$query = '';
for($count = 0; $count<count($t_name); $count++)
{
$t_name_clean = mysqli_real_escape_string($conn, $t_name[$count]);
$t_email_clean = mysqli_real_escape_string($conn, $t_email[$count]);
$t_deposit_clean = mysqli_real_escape_string($conn, $t_deposit[$count]);
/* converting string to date: https://stackoverflow.com/questions/6238992/converting-string-to-date-and-datetime */
$t_duration_clean = mysqli_real_escape_string($conn, $t_duration[$count]);
$t_start_clean = date("y-m-d", strtotime($t_start[$count]));
$t_end = date('Y-m-d', strtotime("+" .$t_duration_clean[$count] ."months", strtotime($t_start_clean)));
$prop_id_clean = $prop_id[0];
if($prop_id_clean != '' && $t_name_clean != '' && $t_email_clean != '' && $t_deposit_clean != '' && $t_start_clean != ''  && $prop_id_clean != ''  && $t_duration_clean != '' && $t_end != '')
{
$query .= '
INSERT INTO tenants(listing_id, name, email, deposit_amount, s_date, duration, e_date) 
VALUES("'.$prop_id_clean.'","'.$t_name_clean.'", "'.$t_email_clean.'", "'.$t_deposit_clean.'", "'.$t_start_clean.'" , "'.$t_duration_clean.'" , "'.$t_end.'"); 
';
}
}
if($query != '')
{
if(mysqli_multi_query($conn, $query))
{
echo 'Tenant added';
}
else
{
    echo "Error updating record: " . mysqli_error($conn), $prop_id_clean;
}
}
else
{
echo 'All Fields are Required';
}
}
?>

为什么会发生这种情况的任何想法?我可以在必要时添加代码,如果有任何好处,我可以使用PHP。 谢谢!

PASTEBIN:https://pastebin.com/k6FZVBy6

1 个答案:

答案 0 :(得分:0)

  • 对于外键和主键数据类型和大小必须 两个表都相同。如果大小或数据类型不同 然后它会抛出一个错误。
  • 对于您的情况,整数的大小不同,在主表中的大小 是11字节,在辅助表中,大小是4字节。