我有以下代码可以将数据添加到我的数据库中。似乎一切正常,但它只向数据库添加一次,之后它告诉我数据已正确保存到数据库中,但是当您想检查它是否已添加时,我可以看到它没有被添加。
插入.php
<?php
session_start();
$server = "";
$user_name = "";
$password = "";
$database = "";
$id = $_SESSION['id'];
$res_name = $_SESSION['username'];
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "INSERT INTO [db_table] (ID,name,category,dish_name,explanation,ingredients,price)
VALUES ('$id','$res_name','$_POST[cat]','$_POST[dname]','$_POST[about]','$_POST[ing]','$_POST[num]')";
$result = mysql_query($SQL);
mysql_close($db_handle);
print "Records added to the database";
echo "<BR>";
echo "<a href='newitem.php'>Back to main page</a>";
}
else {
mysql_close($db_handle);
}
?>
并以我的html形式:
<form class="form-inline" action="inser.php" method="post">
<input type="text" id="dname" name="dname" class="input-medium" placeholder="Dish Name">
<?php
$server = "";
$user_name = "";
$password = "";
$database = "";
$id = $_SESSION['id'];
$res_name = $_SESSION['username'];
$_SESSION['id'] = $id;
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SELECT category from Category where Rest_ID = '".$id."'";
$result = mysql_query($SQL,$db_handle) or die(mysql_error());
$i = 0;
echo "<select id='cat' name='cat'> <option selected='selected'>Select</option>";
while($row = mysql_fetch_array($result)){
$i = $i + 1;
echo "<option>".$i.". $row[category]</option>";
}
echo '</SELECT>';
}
else {
mysql_close($db_handle);
}
//echo $id;
?>
<input type="text" id="ing" name="ing" class="input-medium" placeholder="Ingredients">
<input type="text" id="about" name="about" class="input-medium" placeholder="About this food">
<input type="number" id="num" name="num" class="input-medium" onkeypress="return isNumberKey(event)" placeholder="Price">
<button type="submit" class="btn btn-success">Add</button>
</form>
我似乎无法弄清楚问题所在。