我有2个表RD(休息日)和用于插入值的课程。 看起来像这样。我的第二张桌子看起来像这样
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
RD 13 13 13 13 13 13
如何在第二张桌子上验证。如果星期一有RD,则第二个表上的13为0,表示没有值,因为星期一是教师的RD。因为我目前正在做一个教师轮班制。这是我的插入查询
include('../dist/includes/dbcon.php');
$cewnd = $_POST['cewnd'];
$cewd = $_POST['cewd'];
$cewd = $_POST['cewd'];
$ckwd = $_POST['ckwd'];
$ckwd = $_POST['ckwd'];
$ckwd = $_POST['ckwd'];
$ckwnd = $_POST['ckwnd'];
$query=mysqli_query($con,"select * from t_lessons")or die(mysqli_error());
$count=($query);
if ($count)
{
echo "<script>document.location='test.php'</script>";
}else{
mysqli_query($con,"INSERT INTO t_lessons(Sun,Mon,Tue,Wed,Thu,Fri,Sat) VALUES('$cewnd','$cewd','$cewd','$cewd','$cewd','$cewd','cewnd ')")or
die(mysqli_error($con));
echo "<script type='text/javascript'>
alert('Successfuly added new Lessons');</script>";
echo "<script>document.location='test.php'</script>";
}
This is the code of my modal.
<table border="2" style="float:top; width=10%">
<thead>
<tr>
<th bgcolor="FFFF00">CE Order</th>
</thead>
</table>
<br><table border="2" style="float:top; width=10%">
<thead>
<tr>
<th> Day 1- 15<br>
<input type="text" name="1sthalf" value="3,500" required>
<br>Day 16-<br>
<input type="text" name="2ndhalf" value="3,500" required>
</th> </div><!-- /.form group -->
</thead>
</table>
<table border="2" style="float:top; width=10%">
<thead>
<tr>
<th bgcolor="FFFF00">CE Slots</th>
</thead>
<table border="2" style="float:top; width=10%"><br>
<thead>
<tr>
<th> CE Weekday<br>
<input type="text" name="cewd" placeholder="0">
<br>CE Weekend<br>
<input type="text" name="cewnd" placeholder="0">
<br> CK Weekday<br>
<input type="text" name="ckwd" placeholder="0">
<br>CK Weekend<br>
<input type="text" name="ckwnd" placeholder="0">
<br> <button class="btn btn-lg btn-primary btn-block signup-btn"
type="submit" name="add" id="add">
Add
</button>
</th>
</div><!-- /.form group -->
</thead>
</table>
</table>
答案 0 :(得分:0)
一段时间后,我成功地设法验证了插入查询。请设置相同的列名(如果两个表上的列名不同)。
<?php
$host="localhost";
$username="root";
$password="";
$database= "mydemo";
// Opens a connection to a mySQL server
$connection = mysqli_connect($host, $username, $password);
if (!$connection) {
die("Not connected : ".mysqli_error());
}
// Set the active mySQL database
$dbselected = mysqli_select_db($connection,$database);
if (!$dbselected) {
die ("Can\'t use db : " . mysqli_error());
}
function build_sql_insert($table, $data,$con) {
$key = array_keys($data);
$val = array_values($data);
$sql = "INSERT INTO $table (" . implode(', ', $key) . ") "
. "VALUES ('" . implode("', '", $val) . "')";
$res = mysqli_query($con, $sql) or die(mysqli_error($con));
return($res);
}
// Here you select data from the table rd so you get which columns has "RD" value
$result=mysqli_query($connection,"select sun,mon,tue,wed,thur,fri,sat from rd") or die(mysqli_error());
$arr1 = mysqli_fetch_assoc($result);
// In below there is some modification perform so it fit on your need
foreach($arr1 as $k=>$v){
if(strlen($v)){
$temp[$k] = 0;
}
}
// This is your array which is need to be manage by you. Here i write some static values but you have to manage it.
$arr2 = array(
'sun'=> 10,
'mon'=> 14,
'tue'=> 14,
'wed'=> 14,
'thur'=> 14,
'fri'=> 14,
'sat'=> 10
);
$finalarr = array_merge($arr2,$temp);
//Here is $connection has the connection object. 'lesson' is the table name and $finalarr is the array which we made to insert
$resi = build_sql_insert('lesson', $finalarr,$connection);
if($resi ==1){
echo "<script type='text/javascript'>
alert('Successfuly added new Lessons');</script>";
echo "<script>document.location='test.php'</script>";
}
?>
加油!您的问题已经解决。