简体   繁体   中英

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. Teammembers has 2 columns for the ID's of users and teams.

I have created some code, that selects the names and id's for both teams and users in the dropdown menu. The first problem I am encountering is only the names are showing and not the id's within the dropdown box.

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.

Does anyone know where i've gone wrong?

// 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

<?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

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

and also print the sql before execute it and try to run the printed SQL directly in phpMyAdmin.

echo $sql; exit;

Also check the table columns if there is space after or before the column names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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