我想将表格数据插入表寄宿家庭的mysql数据库寄宿家庭。我检查了很多次,但没有错误。我想知道是否是我的插入查询错误?
这里的人是将表单值插入寄宿家庭表
的php代码<?php
include("connection.php");
$sessid = $_SESSION["sess_id"];
$resultowner = mysqli_query($conn, "select * from owner where owner_id =$sessid");
$rowowner = mysqli_fetch_assoc($resultowner);
if(isset($_POST["addbtn"]))
{
$name = $_POST["name"];
$postcode = $_POST["postcode"];
$city = $_POST["city"];
$state = $_POST["state"];
$address = $_POST["address"];
$capacity = $_POST["capacity"];
$room = $_POST["room"];
$bathroom = $_POST["bathroom"];
$amenity = $_POST["amenity"];
$status= $_POST["status"];
$price= $_POST["price"];
$category = $_POST["category"];
$sessid = $_SESSION["sess_id"];
$result = mysqli_query($conn,"insert into homestay(homestay_name,homestay_postcode,homestay_city,homestay_state,homestay_address,homestay_capacity,homestay_rooms,homestay_bathrooms,homestay_amenities,homestay_blacklist_status,homestay_price_perday,category_id,owner_id)values('$name',$postcode,'$city','$state','$address',$capacity,$room,$bathroom,'$amenity','$status',$price,$category,$sessid)");
if($result)
echo "<script type='text/javascript'>alert('$name');</script>";
}
?>
homept数据库的connection.php链接
<?php
$conn = mysqli_connect("localhost","root","","homestay");
session_start();
?>
这是表单详细信息
<form id="form_register" name="homestay_form" method="post">
<fieldset>
<legend>Edit Homestays</legend>
<label for="name">Homestay Name</label>
<input type="text" name="name" id="name" maxlength="15"/>
<label for="room">Homestay Rooms</label>
<input type="text" name="room" id="room" maxlength="2"/>
<label for="room">Homestay Bathrooms</label>
<input type="text" name="bathroom" id="bathroom" maxlength="2"/>
<label for="capacity">Homestay Capacity</label>
<input type="text" name="capacity" id="capacity" maxlength="2" />
<label for="address">Homestay Address</label>
<textarea name="address" style="text-align:left;" maxlength="50" rows="5" cols="40"></textarea>
<label for="postcode">Homestay Postcode</label>
<input type="text" name="postcode" id="postcode" maxlength="5"/>
<label for="state">Homestay City</label>
<input type="text" name="city" id="state" maxlength="15"/>
<label for="reg_city">Homestay State</label>
<select name="state" style="width:300px;">
<option value="Johor">Johor</option>
<option value="Kedah">Kedah </option>
<option value="Kelantan">Kelantan</option>
<option value="Kuala Lumpur">Kuala Lumpur</option>
<option value="Melaka">Melaka</option>
<option value="Negeri Sembilan">Negeri Sembilan</option>
<option value="Pahang">Pahang</option>
<option value="Perak">Perak</option>
<option value="Perlis">Perlis </option>
<option value="Pulau Pinang">Pulau Pinang </option>
<option value="Sabah">Sabah</option>
<option value="Sarawak">Sarawak</option>
<option value="Selangor">Selangor</option>
<option value="Terengganu">Terengganu</option>
</select>
<label for="amenity">Homestay Amenities</label>
<input type="text" name="amenity" id="amenity" maxlength="100"/>
<label for="price">Homestay Price Per Day</label>
<input type="text" name="price" id="price" maxlength="4"/>
`
<label for="category">Homestay Category</label>
<select name="category">
<?php
$result2 = mysqli_query($conn,"select * from category");
while($row2 = mysqli_fetch_assoc($result2)){?>
<option value="<?php echo $row2['category_id']; ?>" > <?php echo $row2['category_types'] ;?>
</option>
<?php}?>
</select>
<label for="status">Homestay Blacklist Status</label>
<select name="status" style="width:300px;">
<option value="Available">Available</option>
<option value="Non-available">Non-available</option>
</select>
<label for="reg_profile">Profile Picture*</label>
<input type="file" name="profile_pic" id="reg_picture"/>
<input type="submit" name="addbtn" value="Add" id="reg_submit" />
</fieldset>
</form>
这是寄宿家庭表
homestay_id int(10)
homestay_name varchar(50)
homestay_address varchar(50)
homestay_postcode int(5) unsigned zerofill
homestay_state varchar(20)
homestay_city varchar(20)
homestay_capacity int(5)
homestay_rooms int(3)
homestay_bathrooms int(3)
homestay_amenities varchar(50)
homestay_blacklist_status varchar(50)
homestay_price_perday double
category_id int(11)
owner_id int(11)
答案 0 :(得分:0)
根据错误更新了答案
在您的PHP代码中,您忘记获取所有者ID
$sessid = $_SESSION["sess_id"];
$resultowner = mysqli_query($conn, "SELECT * FROM owner WHERE owner_id ='$sessid'");
$rowowner = mysqli_fetch_assoc($resultowner);
$ownerid = $rowowner[owner_id];
并在您的插入查询VALUES中将$sessid
替换为$ownerid
所以你的完整更新代码将是这样的
<?php
include("connection.php");
$sessid = $_SESSION["sess_id"];
$resultowner = mysqli_query($conn, "SELECT * FROM owner WHERE owner_id ='$sessid'");
$rowowner = mysqli_fetch_assoc($resultowner);
$ownerid = $rowowner[owner_id];
if(isset($_POST["addbtn"])){
$name = $_POST["name"];
$postcode = $_POST["postcode"];
$city = $_POST["city"];
$state = $_POST["state"];
$address = $_POST["address"];
$capacity = $_POST["capacity"];
$room = $_POST["room"];
$bathroom = $_POST["bathroom"];
$amenity = $_POST["amenity"];
$status= $_POST["status"];
$price= $_POST["price"];
$category = $_POST["category"];
//$sessid = $_SESSION["sess_id"]; don't need this here anymore
$result = mysqli_query($conn,"insert into homestay(homestay_name,homestay_postcode,homestay_city,homestay_state,homestay_address,homestay_capacity,homestay_rooms,homestay_bathrooms,homestay_amenities,homestay_blacklist_status,homestay_price_perday,category_id,owner_id) VALUES ('$name',$postcode','$city','$state','$address','$capacity','$room','bathroom','$amenity','$status','$price','$category','$ownerid')");
if($result)
echo "<script type='text/javascript'>alert('$name');</script>";
}
?>
旧答案
你错过了引语
'$name',$postcode,'$city','$state','$address',$capacity,$room,$bathroom,'$amenity','$status',$price,$category,$sessid
尝试以下查询
$result = mysqli_query($conn,"insert into homestay(homestay_name,homestay_postcode,homestay_city,homestay_state,homestay_address,homestay_capacity,homestay_rooms,homestay_bathrooms,homestay_amenities,homestay_blacklist_status,homestay_price_perday,category_id,owner_id) VALUES ('$name',$postcode','$city','$state','$address','$capacity','$room','bathroom','$amenity','$status','$price','$category','$sessid')");
答案 1 :(得分:0)
问题在于category_id设置为PRIMARY或UNIQUE。任何类型的键都不能有两行包含相同的值。这意味着用户已经在该类别中发布了一些内容。返回到您的数据库并交叉检查category_id是否已设置为密钥。尝试使用phpmyadmin来查看。这可能是个错误