这是代码。由于某种原因,我无法找出它为什么不起作用
正如你所看到的,我已经添加了一个测试查询,看看它是否会影响我的数据库上的任何更改,但是没有:(
有趣的是,在另一个PHP脚本中,我已成功连接数据库,甚至通过php脚本添加了一些记录。无法找到问题,谢谢提前
BTW。正如你所看到的,我已经将var“month”定义为字符串“hey”并在php脚本的末尾回显它以查看它是否发生了变化。但没有任何事情发生!!
<form action="" name="form" id="form"> <label> Select the month which you want to display its days. <select name="month" form="form" required> <option value="january">January</option> <option value="february">February</option> <option value="march">March</option> <option value="april">April</option> <option value="may">May</option> <option value="june">June</option> <option value="july">July</option> <option value="august">August</option> <option value="september">September</option> <option value="october">October</option> <option value="november">November</option> <option value="december">December</option> </select> </label> <input type="submit" name="update" value="Display" /> </form> <?php $month = "hey"; if(isset($_POST["update"])) { $month = $_POST["month"]; $query = "SELECT * FROM `days` WHERE `month`='{$month}';"; $conn = mysqli_connect("localhost","root","","db123"); $result = mysqli_query($conn,$query); mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');"); if($result) { die("Sorry!"); } while($row = mysqli_fetch_row($result)) { echo $month; print_r($row); } mysqli_close($conn); echo $month; } ?
答案 0 :(得分:0)
您没有指定方法,默认情况下为GET。更改此行
if(isset($_POST["update"]))
到这个
if(isset($_GET["update"]))
。或者,如果要将方法用作POST,则只需将方法指定为POST即可 使用下面的代码
<form action="" name="form" id="form" method="POST">
<label>
Select the month which you want to display its days.
<select name="month" form="form" required>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</label>
<input type="submit" name="update" value="Display" />
</form>
<?php
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$query = "SELECT * FROM `days` WHERE `month`='{$month}';";
$conn = mysqli_connect("localhost","root","","db123");
$result = mysqli_query($conn,$query);
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
if($result)
{
die("Sorry!");
}
while($row = mysqli_fetch_row($result))
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?>
希望这有助于你
答案 1 :(得分:0)
<?php
$mysqli = new mysqli("localhost", "root", "", "db123");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$res = $mysqli->query("SELECT * FROM `days` WHERE `month`='{$month}'");
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
while($row = $res->num_rows)
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?>