这是从html页面中的表单触发的脚本。
<?php
// title , notice , to , from , subject . time
// var: flag1, flag2 ;
require('check_session.php');
require('db_init_.php');
$emailid = $_SESSION["emailid"];
$title = $_POST["title"];
$notice = $_POST["notice"];
if(empty($_POST["firstyear"])) {$firstyear="";}
else
{ $firstyear = "";
foreach ($_POST['firstyear'] as $s)
{
$firstyear = $firstyear." , " .$s; // setting Selected Value
}
}
if(empty($_POST["secondyear"])) {$secondyear="";}
else
{ $secondyear = "";
foreach ($_POST['secondyear'] as $s)
{
$secondyear = $secondyear ." , ".$s; // setting Selected Value
}
}
if(empty($_POST["thirdyear"])) {$thirdyear="";}
else
{ $thirdyear = "";
foreach ($_POST['thirdyear'] as $s)
{
$thirdyear = $thirdyear ." , ".$s; // setting Selected Value
}
}
if(empty($_POST["fourthyear"])) {$fourthyear="";}
else
{ $fourthyear = "";
foreach ($_POST['fourthyear'] as $s)
{
$fourthyear = $fourthyear ." , ".$s; // setting Selected Value
}
}
$to = $firstyear.$secondyear.$thirdyear.$fourthyear;
$subject = $_POST["subject"];
echo $emailid . $title .$notice . $to. $subject ;
$table="accounts_faculty";
$sql_4="SELECT * FROM $table WHERE emailid='$emailid' ";
$result4=mysqli_query($conn,$sql_4);
$row4 = mysqli_fetch_row($result4);
$first = $row4[1];
$last = $row4[2];
$name = $first." ".$last;
echo $name ;
$count4=mysqli_num_rows($result4);//faculty
if($count4 == 1) {$flag1 = 1;}
else $flag1 = 0;
if($flag1 == 1)
{ echo " in flg 1 ";
$table = "notices";
$sql_5= "INSERT INTO `minor_naps`.'$table' (`title`, `notice`, `to`, `from`, `subject`, `time`)
VALUES ('$title', '$notice', '$to', '$name', '$subject', CURRENT_TIMESTAMP);";
$result5=mysqli_query($conn, $sql_5); // to be continued
//var_dump($result5);
if($result5==1)
{$flag2= 1;
// header("location:main-page.php");
echo 1;
}
else {
$flag2 = 0;
// header("location:create_notice.html");
}
}
echo $flag1.",".$flag2;
?>
check_session.php检查用户会话是否已设置。 和db_init_.php是
<?php
//database details
$host = "localhost";
$username = "root";
$password = "";
$db="minor_naps";
//$table="accounts";
// Create connection
$conn = new mysqli($host, $username, $password,$db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
# code...
//echo "<h1>db initialised and Connected successfully</h1>";
}
?>
从html收到数据时,它会从accounts_faculty表中提取数据以获取其名称。然后将其插入表格通知中。
我在此代码中有标记。 运行后,没有语法错误,标志1为1但标志2仍为0。 那就是数据没有插入表格通知中。
我错过了什么吗?这里有什么错误? 以及改进此代码的任何建议吗?