我正在尝试通过ajax将数据添加到数据库中,并且在网络中收到错误500。
ajax
<script>
$( document ).ready( function() {
$("#modalsave1").click(function(e){
e.preventDefault();
$.ajax({
type: "post",
url: "{{ url('admin/addnewsuboptionprodcreat') }}",
data: {
'_token': $('input[name=_token]').val(),
'subopttitle': $('input[name=subopttitle]').val(),
'opt_id': $('#opt_id').val(),
'subopt_price': $('#subopt_price').val(),
'subopt_sku': $('#subopt_sku').val(),
'optstat_id': $('#optstat_id').val(),
},
success: function (data) {
$('#msg').append('<span class="text-success">Option added successfully!</span>');
console.log(data);
},
error: function (data) {
console.log('Error:', data);
}
});
});
});
</script>
blade (form)
{{Form::open()}}
//form inputs
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" id="modalsave1" class="modalsave1 btn btn-primary">Save</button>
</div>
{{Form::close()}}
controller
public function addnewsuboptionprodcreat(Request $reqs)
{
$add = Suboption::create([
'title' => $reqs->subopttitle,
'option_id' => $reqs->opt_id,
'price' => $reqs->subopt_price,
'sku' => $reqs->subopt_sku,
'status_id' => $reqs->optstat_id,
]);
}
"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' (SQL: insert into `suboptions` (`title`, `option_id`, `price`, `sku`, `status_id`, `updated_at`, `created_at`) values (ssssssss, 1, 3232323, sfdsfsf, 1, 2018-07-17 08:03:41, 2018-07-17 08:03:41))"
PS:虽然这里提到了
1062 Duplicate entry
,但我可以保证 您会发现我的表中没有相同的信息是duplicated
。
有什么主意吗?
suboptions table schema
public function up()
{
Schema::create('suboptions', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->unique();
$table->string('price');
$table->string('sku')->nullable();
$table->integer('option_id')->unsigned();
$table->integer('status_id')->unsigned();
$table->timestamps();
});
Schema::table('suboptions', function (Blueprint $table) {
$table->foreign('option_id')->references('id')->on('options');
$table->foreign('status_id')->references('id')->on('statuses');
});
}
答案 0 :(得分:0)
以某种方式在本地不起作用,并返回错误500,但在实时主机上就可以了!
PS:适度,请关闭此问题。
谢谢/。