简体   繁体   English

努力从另一个表中选择一个值

[英]Struggling to select a value from another table

Im currently constructing a database using PHP, HTML and MySQL.我目前正在使用 PHP、HTML 和 MySQL 构建一个数据库。

I have an events table.我有一个事件表。 Event_id is the primary key. Event_id是主键。 When someone registers for an event they input their student_id and they also select an event from a dropbox that pulls the events from my events table.当有人注册一个事件时,他们输入他们的student_id并且他们还从下拉框中选择一个事件,从我的事件表中提取事件。 I have an event_registration table which records all entries into this form.我有一个event_registration表,它将所有条目记录到此表单中。 I have event_id in my event_registration table and I'm trying to make it so that when someone selects a certain event, that event's id automatically goes into the event_registration table.我的event_registration表中有event_id并且我正在尝试这样做,以便当有人选择某个事件时,该事件的 id 会自动进入event_registration表。 My PHP code at present is this:我目前的 PHP 代码是这样的:

 $student_id = $_POST['student_id']; $title = $_POST['title']; $sql = "select event_id from events where title LIKE $title"; $db->select_db($database); $event_id = $db->query($sql); $q = "INSERT INTO event_registration ("; $q .= "student_id, event_id, title"; $q .= ") VALUES ("; $q .= "'$student_id', '$event_id', '$title')"; $result = $db->query($q);

The student_id , title , and registraion_id (auto- increment) are inserting fine, but event_id is always showing up as 0 , I'm not sure where I'm going wrong. student_idtitleregistraion_id (自动递增)插入正常,但event_id总是显示为0 ,我不确定我哪里出错了。 Any help is appreciated.任何帮助表示赞赏。

 <td style="width: 176px; height: 23px">Event Title</td> <td style="height: 23px"><select name="title" style="width: 124px"> <?php include ("detail.php"); $sql = mysqli_query($db, "SELECT title FROM events"); while ($row = $sql->fetch_assoc()){ ?> <option><?php echo ($row['title']); ?></option>"; <?php } ?> </select></td>

my dropdown code我的下拉代码

The problem you seem to be having is:您似乎遇到的问题是:

$event_id = $db->query($sql);

Probably doesn't give you the number you're expecting right away, instead you need to fetch like this:可能不会立即为您提供您期望的数字,而是您需要像这样获取:

$result= $db->query($sql);
$result = $result->fetch_assoc();
$event_id = $result['event_id'];

However, based on your comment, you're constructing your dropdown with this:但是,根据您的评论,您正在使用以下内容构建下拉列表:

<?php 
    include ("detail.php"); 

    $sql = mysqli_query($db, "title FROM events"); 

    while ($row = $sql->fetch_assoc()){ 

?> 

<option><?php echo ($row['title']); ?></option> 

<?php } ?>

You can do this instead:你可以这样做:

<?php 
    include ("detail.php"); 

    $sql = mysqli_query($db, "SELECT event_id,title FROM events"); 

    while ($row = $sql->fetch_assoc()){ 

?> 

<option value="<?php echo $row['event_id']; ?>"><?php echo ($row['title']); ?></option>"; 

<?php } ?>

Notice how I'm also adding event_id on the select caluse.注意我是如何在 select caluse 上添加 event_id 的。 WIth this the users will see the title as expected, but you'll get the id directly with POST like this:有了这个,用户将按预期看到标题,但您将直接通过 POST 获得 id,如下所示:

$event_id = $_POST['event_id'];

You DO have to change the name of the select dropdown to event_id instead of title like this:必须改变选择下拉列表事项标识,而不是像这样标题的名称

<select name="event_id">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM