我有两张桌子:
如果名为TINA(id 3)的用户登录并发布问题,则她的问题应保存在表格QUESTIONS中,其中包含各自的ID,即3。 如何实现这一目标?
<div id="content">
</br>
<form action="" method="post">
<center> <h3> <font color="green"> Ask a new question:
<select name="type" required>
<option value="" disabled selected> Select the type of question </option>
<option value="technical"> Technical </option>
<option value="entertainment"> Entertainment </option>
<option value="fashion"> Fashion </option>
<option value="food"> Food </option>
<option value="education"> Education </option>
<option value="lifestyle"> Lifestyle </option>
<option value="relationships"> Relationships </option>
<option value="health"> Health </option>
<option value="random"> Random </option>
</select>
</font> </h3>
<textarea rows="3" cols="100" style="resize:none" placeholder="Please type your question here..." name="ques" required></textarea><br>
<input type="submit" value="POST" name="POST" align="middle">
</center>
</form>
<?php
if(isset($_POST['POST']))
{
$type=$_POST['type'];
$ques=$_POST['ques'];
$date = strftime("%B %d, %Y");
$time = strftime("%r");
$sql="INSERT INTO questions (type,question,date,time) VALUES ('$type', '$ques', '$date', '$time')";
$query = mysqli_query($conn, $sql);
if(isset($_SESSION['id']))
{
echo "<div class='boxtype'>";
echo strtoupper($type);
echo "</div>";
echo "<div class='boxtime'>";
echo '['.$date.']'.' '.'['.$time.']';
echo "</div>";
echo "<div class='box'>";
echo "<font color='red'>".'<b>'.'<i>'.'<u>';
echo strtoupper($_SESSION['user']);
echo '</u>'.'</i>'.'</b>'.'</font>';
echo ':'.' '.$ques;
echo "</div>";
}
}
&#13;
答案 0 :(得分:0)
首先修改您的table
QUESTIONS
。创建field
user_id
以存储发布问题的id
user
。
当用户说Tina登录时,在登录期间创建session
并将id
存储在session
variable
SESSION['user_id'] = your_log_in_user_id
中。现在只需将其插入到问题表中,如
$user_id = SESSION['user_id'];
$sql="INSERT INTO questions (type,question,date,time,user_id) VALUES ('$type', '$ques', '$date', '$time','$user_id')";
$query = mysqli_query($conn, $sql);
答案 1 :(得分:0)
我很容易使用php session。当用户登录问题部分时,只需在会话中保存用户ID .id是USER表id,它是主键。
$_SESSION['ID']=$id;
问题根据会话ID保存到表中。只需在问题表中将该列创建为user_id。
$user_id=$_SESSION['ID'];
$sql="INSERT INTO questions (user_id,type,question,date,time) VALUES ('$user_id','$type', '$ques', '$date', '$time')";