gpt4 book ai didi

PHP使用 session 从数据库中获取clientID

转载 作者:行者123 更新时间:2023-11-30 22:48:51 25 4
gpt4 key购买 nike

我有一个登录页面,当用户成功登录时,它会被带到欢迎页面,然后他可以点击预订事件。当用户点击预订事件时,日历会显示所有可以预订的事件。当用户点击预订某个事件时,clientEvent 表应该使用从登录页面和 activityEventID 中获取的 clientID 进行更新。但我收到错误:无法添加或更新子行:外键约束失败(fitnessconnectclientevent,CONSTRAINT clientevent_ibfk_1 FOREIGN KEY ( activityEventID) 引用资料 activityevent (activityEventID))

如果我没有从登录页面或其他地方正确获取 clientID,我不确定我在哪里犯了错误。

我也不确定如何传递 activityEventID a href='CreateNewActivityBooking.php?activityEventID=activityEventID' 我用过这个,但我不确定它是否正确

为了创建两个表,我使用了这个

 create table activityEvent
(
activityEventID integer auto_increment,
date date,
title varchar(30),
description varchar(50),
price integer,
location varchar(30),
startTime time,
endTime time,
activityTitle varchar(30),
noOfPlaces integer,
restricted varchar(30),
personalTrainer_CoachID integer,
primary key (activityEventID),
foreign key (personalTrainer_CoachID) references personalTrainer_Coach(personalTrainer_CoachID)
);

create table clientEvent
(
clientEventID integer auto_increment,
activityEventID integer,
clientID integer,
primary key (clientEventID),
foreign key (activityEventID) references activityEvent(activityEventID),
foreign key (clientID) references client(clientID)
);

这是 login.html

 <HTML>

<BODY>

<FORM action='login.php' method = 'POST'>
<label>Enter your username</label>
<input name="username" type="text"/>
<br/>
<label>Enter your password</label>
<input name="password" type="password"/>
<br/>
<input type="submit"/>
</FORM>

</BODY>


</HTML>

登录.php

<?php

session_start();


$userName = $_POST['username'];
$passWord = $_POST['password'];

require('connection.php');


$dbpwd="";
$con=mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);

$sql ="select clientID, password from client where userName = '$userName'";

$result = mysqli_query($con,$sql);

while($row = $result->fetch_assoc())
{
$dbpwd =$row['password'];

$clientID =$_SESSION['clientID'];
$_SESSION['clientID']='$clientID';


}

if($dbpwd == $passWord)
{


header("Location: welcome.html");

}
else
{
echo"Passwords do not match";
echo"</br>";
echo"Please Try Again";


}

?>

welcome.html 这只是一个开始,它没有太多的功能

<html>
<head>
<title> Welcome </title>
</head>
<body>
<h1>Welcome!</h1>

<button onClick="window.location='calendarTest2.php';" > Book Activity</button>
</body>
</html>

calenderTest2.php

<?php


$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
?>

<?php
if (!isset($_REQUEST["month"]))
$_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"]))
$_REQUEST["year"] = date("Y");
?>
<?php
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 )
{
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 )
{
$next_month = 1;
$next_year = $cYear + 1;
}

require("BookingsDB.php");
$myBookingsDB = new BookingsDB();
$bookings = $myBookingsDB->getMonthlyBookings($cMonth,$cYear);


?>
<div align="left">
<table width="400" border="5" align="left" id="calendar">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF"><table width="100%" border="0"
cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left"><a href="<?php echo $_SERVER["PHP_SELF"]
. "?month=". $prev_month . "&year=" . $prev_year; ?>"
style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"]
. "?month=". $next_month . "&year=" . $next_year; ?>"
style="color:#FFFFFF">Next</a></td>
</tr>
</table></td>
</tr>
<tr>
<td align="center"><table width="100%" border="2" cellpadding="2"
cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong> <?php
echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>S</strong></td>
</tr>


<?php

require("connection.php");

$con=mysqli_connect("$mysql_host","$mysql_user","$mysql_password","$mysql_database");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
$today = getdate();


for ($i=0; $i<($maxday+$startday); $i++)
{
if(($i % 7) == 0 )
{
echo "<tr> ";
}

if($i < $startday)
{
echo "<td></td> ";
}
else
{
$day = $i - $startday + 1;
$thisDate = new DateTime("$cYear-$cMonth-$day");
$jsEvent[] = "document.getElementById('trigger" . $i . "').onclick = function() {showForm()};";
echo "<td align='center' valign='middle' height='20px'><a href='CreateNewActivityBooking.php?activityEventID=activityEventID' id='trigger" . $i . "'>". ($i - $startday + 1) . "</a>";



foreach ($bookings as $key => $value)
{

if ($thisDate->format('Y-m-d') == $value['date']->format('Y-m-d'))
{

echo " ";
echo $value['title'];
echo " ";
echo $value['startTime'];
}

}

echo "</td>";

}
if(($i % 7) == 6 )
{
echo "</tr> ";
}
}

mysqli_close($con);
?>

<script type="text/javascript">
<?php foreach($jsEvent as $event)
{
echo $event;
}

?>

function showForm()
{
document.getElementById('timeslots').style.display="block";
};
</script>
</table></td>
</tr>
</table>

预订数据库.php

<?php

session_start();



class BookingsDB
{
private $bookings = array();
private $con;

public function BookingsDB()
{
require("connection.php");
$this->con = mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);

}

public function getMonthlyBookings($thisMonth,$thisYear)
{
$sql = "SELECT activityEventID,date,title,startTime FROM activityEvent WHERE YEAR(date) = $thisYear AND MONTH(date) = $thisMonth";
$result = mysqli_query($this->con,$sql);
while($row=mysqli_fetch_array($result))
{

$this->bookings[] = array('date'=>new DateTime($row['date']), 'title'=>$row['title'],'startTime'=>$row['startTime']);

//$activityEventID =$_SESSION['activityEventID'];
//$_SESSION['activityEventID']='activityEventID';

}
return $this->bookings;
}

public function close()
{
mysqli_close($this->con);
}

}

?>

CreateNewActivityBooking.php

<?php

session_start();

require("connection.php");


$con=mysqli_connect("$mysql_host","$mysql_user","$mysql_password","$mysql_database");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$clientID=$_SESSION['clientID'];
$activityEventID =$_SESSION['activityEventID'];

$sql="Insert into clientEvent(clientID, activityEventID) Values('$clientID','$activityEventID')";


if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));

}
echo "1 record added";

?>

最佳答案

从您的代码来看,您似乎没有将 activityEventID 存储在 session 变量中。您正在尝试在 CreateNewActivityBooking.php 页面上访问它,同时将事件插入到“clientEvent”表中。

查看代码中的以下几行:

$clientID=$_SESSION['clientID'];
$activityEventID =$_SESSION['activityEventID']; //Here $_SESSION['activityEventID'] doesn't exists

因为“clientEvent”表对“activityEventID”列有外键约束。由于此约束将失败,查询将不会执行,您会收到错误。

所以基本上你需要找到一种方法来在那里传递正确的值。

编辑:至于您的评论,您发布的脚本似乎没有在 session 中存储“activityEventID”,因为您引用的行实际上是对您发布的相关脚本的评论。

这里要具体一点就是你需要做的。

在 BookingsDB.php 上

$this->bookings[] = array('date'=>new DateTime($row['date']), 'title'=>$row['title'],'startTime'=>$row['startTime'], 'activityEventID'=>$row['activityEventID']);

在你的 calenderTest2.php 上

echo "<td align='center' valign='middle' height='20px'>";
foreach ($bookings as $key => $value)
{

if ($thisDate->format('Y-m-d') == $value['date']->format('Y-m-d'))
{
echo "<a href='CreateNewActivityBooking.php?activityEventID=".$value['activityEventId']."' id='trigger" . $i . "'>". ($i - $startday + 1) . "</a>";
echo " ";
echo $value['title'];
echo " ";
echo $value['startTime'];
}

}
echo '</td>';

现在您正在为每个链接在查询字符串中传递 activityEventID。您可以在 CreateNewActivityBooking.php 中检索它,如下所示:

$activityEventID =$_GET['activityEventID'];

您可能需要在布局方面重新格式化一些东西,但我认为应该这样做。

关于PHP使用 session 从数据库中获取clientID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28749271/

25 4 0
文章推荐: php - 使用