在php中插入外键

jjjwad0x  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(226)

我对php和mysql非常陌生;我想做一些简单的事。我有jquery函数:

//Operations for Add Booking button
$("#buttonAddBooking").on('click', function(e) {
 e.preventDefault();
 $.ajax({
  type: 'POST',
  url: 'http://localhost/add_booking.php',
  data: {'guestID': 6, 'arrivalDate': '2018-12-07', 'departureDate': '2018-
  12-16'},
  success: function(data){
  alert('DONE');}
 });
 return false;
});

以及add\u booking.php脚本:

//Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
//Check connection
if (mysqli_connect_errno()){
 echo "Connection failed:" . mysqli_connect_error();
}
if(isset($_POST['guestID']) && !empty($_POST['guestID'])) {
 $guest_id = $_POST['guestID'];
}
if(isset($_POST['arrivalDate']) && !empty($_POST['arrivalDate'])) {
 $arrival_date = $_POST['arrivalDate'];
}
if(isset($_POST['departureDate']) && !empty($_POST['departureDate'])) {
 $departure_date = $_POST['departureDate'];
}
$sql = "INSERT INTO bookings (bookingsID, guestsID, arrival_date, 
departure_date) VALUES (NULL, $guest_id,'$arrival_date', 
'$departure_date')";
if (mysqli_query($conn,$sql)) {
 echo "New record created successfully";
} else {
 echo ("Error: " . mysqli_error($conn));
}
mysqli_close($conn);

我正试图把一张新唱片插入 bookings table在哪里 guestsID 是外键,我不能。我想问题是 guestsIDint .

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题