简体   繁体   English

选择选项下拉菜单并插入功能PHP

[英]Select option dropdown menu and insert into function PHP

I am trying to create two dropdown menus, that will enable a selected user to be added to a selected team and submitted. 我正在尝试创建两个下拉菜单,这些菜单将使选定的用户可以添加到选定的团队并提交。

There are 3 tables users, teams and teammembers. 有3个表的用户,团队和团队成员。 Teammembers has 2 columns for the ID's of users and teams. 团队成员有两列用于用户和团队的ID。

I have created some code, that selects the names and id's for both teams and users in the dropdown menu. 我创建了一些代码,该代码在下拉菜单中为团队和用户选择名称和ID。 The first problem I am encountering is only the names are showing and not the id's within the dropdown box. 我遇到的第一个问题是仅显示名称,而不显示下拉列表中的ID。

Secondly, when submitting the form data is inputted into the teammembers table but both as 0 and 0 rather than the users id and team id submitted. 其次,在提交表单时,将数据输入到teammembers表中,但同时输入0和0,而不是提交的用户ID和团队ID。

Does anyone know where i've gone wrong? 有人知道我哪里出问题了吗?

// cpanel-addplayer.php // cpanel-addplayer.php

    <link href="default.css" rel="stylesheet" type="text/css" />

<form method="post" action="cpanel_addplayerprocessing.php">
<?
    session_start();
    include('../utils/dbc.php');
    error_reporting(-1);

echo 'Players';

$sql = "SELECT ID, user_name FROM users";
$result = mysql_query($sql);

echo "<select name='user_name'>";
while ($row = mysql_fetch_array($result)) {
    echo'<option value="'.$row['ID'].'">'.$row['user_name'].'</option>';
}   

echo "</select>";

?>

<?php

echo 'Teams';

$sql = "SELECT ID, name FROM teams";
$result = mysql_query($sql);

echo "<select name='teams'>";
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['ID'] ."'>" . $row['name'] ."</option>";
    $teamid = $row['ID'];
}
echo "</select>";

?>



<input type="submit" name="submit" value="Submit"> 
</form>

// cpanel-addplayerprocessing.php // cpanel-addplayerprocessing.php

<?php
error_reporting(-1);

    session_start();
    include('../utils/dbc.php');

// escape variables for security
a
$sql="INSERT INTO teammembers (userid, teamid)
VALUES ('$userid', '$teamid')";
$result = mysql_query($sql);

if($result){
header('Location: ../thankyou.php');
}
else {
echo "ERROR";
}

mysql_close();
?>

Thanks for your help! 谢谢你的帮助!

Use PHP concatanation to generate the SQL query 使用PHP合并生成SQL查询

$sql="INSERT INTO teammembers (userid, teamid) VALUES ('".$userid."', '".$teamid."')"; $ sql =“ INSERT INTO teammember(userid,teamid)VALUES('”。$ userid。“','”。$ teamid。“')”;

and also print the sql before execute it and try to run the printed SQL directly in phpMyAdmin. 并在执行之前打印sql,并尝试直接在phpMyAdmin中运行打印的SQL。

echo $sql; echo $ sql; exit; 出口;

Also check the table columns if there is space after or before the column names. 此外,还要检查表列是否在列名之后或之前有空格。

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

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