简体   繁体   English

将表单组合中的数据保存到 MySQL PHP 数据库

[英]Saving data from a combination of forms to a MySQL PHP database

I've successfully been able to save direct data from a form into a database, and then display this data onto a HTML page (the user's username).我已经成功地将表单中的直接数据保存到数据库中,然后将此数据显示到 HTML 页面(用户的用户名)上。 However, I am struggling to save data (text) that has been generated from a series of drop-down menus.但是,我正在努力保存从一系列下拉菜单生成的数据(文本)。

I have successfully allowed the user to generate a specific Workout Plan based on their chosen selections in four drop-down menus.我已经成功地允许用户根据他们在四个下拉菜单中选择的选项生成特定的锻炼计划

JavaScript code: JavaScript 代码:

<script>
jQuery(function ($) {

    var selects = $('#select-container select'),
    results = $('#results-container > div');

    selects.change(function () {
        var values = '';
        selects.each(function () {
            values += '.' + $(this).val();
        });
        results.filter(values).show().siblings().hide();
    });

});
</script>

HTML code snippet: HTML代码片段:

    <div class="margins1">
<h2>Goal</h2>
<div id='select-container'>
<select>
<option value='none'>Select Option</option>
<option value='FatLoss'>Fat Loss</option>
<option value='LeanMuscle'>Lean Muscle</option>
<option value='SizeMass'>Size & Mass</option>
</select>
<h2>Curent Level</h2>
<select>
<option value='none'>Select Option</option>
<option value='Beginner'>Beginner</option>
<option value='Intermediate'>Intermediate</option>
<option value='Advanced'>Advanced</option>
</select>
<h2>Gym Accessibilty</h2>
<select>
<option value='none'>Select Option</option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
<h2>Days Per Week</h2>
<select>
<option value='none'>Select Option</option>
<option value='3DaySplit'>3-Day Split</option>
<option value='4DaySplit'>4-Day Split</option>
<option value='5DaySplit'>5-Day Split</option>
</select>
</div>

<div class="margins3">
<h1>Workout Plan...</h1>
<div id='results-container'>
<div class='LeanMuscle Intermediate Yes 4DaySplit'><b>Day 1: Chest & Triceps </b><br>
Dumbbell/Barbell Bench Press (3 sets, 8-12 reps)<br>
Incline Dumbbell/Barbell Bench Press (3 sets, 8-12 reps)<br>
Dumbbell/Cable Flies (3 sets, 10-15 reps)<br>
Press-ups (3 sets, until failure)
Incline Treadmill Walk (10-15 minutes, slow pace)<br>
<b>Day 2: Back & Biceps</b><br>
Dumbbell/Barbell Rows (3 sets, 8-12 reps)<br>
Pull-ups/Chin-ups (3 sets, 6-12 reps)<br>
Seated Rows (3 sets, 8-15 reps)<br>
Dumbbell/Barbell Bicep Curls (3 sets, 8-15 reps)<br>
Dumbbell Hammer Curls (3 sets, 8-15 reps)<br>
<b>Day 3: Legs</b><br>
Barbell Squats (3 sets, 8-15 reps<br>
Leg Press Machine (3 sets, 8-15 reps)<br>
Leg Extension Machine (3 sets, 8-15 reps)<br>
Leg Curl Machine (3 sets, 8-15 reps)<br>
Calf Raises (3 sets, 8-20 reps)<br>
<b>Day 4: Shoulders & Trapezius</b><br>
Dumbbell/Barbell Shoulder Press (3 sets, 8-12 reps)<br>
Barbell Shrugs (3 sets, 6-12 reps)<br>
Dumbbell Rear Delt Flies (3 sets, 8-15 reps)<br>
Barbell Upright Rows (3 sets, 6-12 reps)<br>
Incline Treadmill Walk (10-15 minutes, slow pace)
    </div>
    </div>
</div>

EDIT: PHP code for registration编辑:用于注册的 PHP 代码

    <?php

define('DB_HOST', 'localhost');
define('DB_NAME', 'test');
define('DB_USER','root');
define('DB_PASSWORD','root');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

function NewUser()
{   
$username = $_POST['username'];
$password =  $_POST['password'];
$query = "INSERT INTO members (username,password) VALUES ('$username','$password')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
 header("Location: Correct.php");
}
}

function SignUp()
{
  if(!empty($_POST['username'])&&!empty($_POST['password']))
  {
    $query = mysql_query("SELECT username FROM members WHERE username = '$_POST[username]'") or die(mysql_error());
    $num_rows = mysql_num_rows($query);
    if($num_rows==0) 
    {
      NewUser();
    }
    else
    {
     header("Location: UsernameExisting.html");
   }
 }
 if(empty($_POST['username']))
 {
   header("Location: MissingUsername.html");
 }
 if(empty($_POST['password']))
 {
   header("Location: MissingPassword.html");
 }
 if (empty($_POST['username'])&&empty($_POST['password'])){
  header("Location: MissingDetails.html");
}
}
if(isset($_POST['submit']))
{
  SignUp();
}
?>

As you can see, the workout plan above is formed by a combination of specific selections in the four drop-down lists.如您所见,上面的锻炼计划是由四个下拉列表中的特定选项组合而成的。 I need to save this output in the mealPlan field from the members table in my MySQL PHP database named test (which I've already made).我需要将此输出保存在名为test (我已经创建)的 MySQL PHP 数据库中的members表中的mealPlan字段中。 I have tried many ways but cannot get this to work.我尝试了很多方法,但无法让它发挥作用。 Could anyone assist me in achieving this as simply as possible?任何人都可以帮助我尽可能简单地实现这一目标吗?

Your time and support is much appreciated.非常感谢您的时间和支持。 Many thanks.非常感谢。

Your select tags do not have a name attribute.您的选择标签没有名称属性。 Name attributes are required in order to send the data to the server.需要名称属性才能将数据发送到服务器。

Do some reading about html forms.阅读一些关于 html 表单的信息。

You can also submit it using JSON,您也可以使用 JSON 提交它,

Change your HTML code to have ids associated with all select tags .将您的 HTML 代码更改为具有与所有选择标签相关联的 id。

And then you can convert that in JSON like below,然后你可以像下面那样将它转换成 JSON,

        jQuery(function ($) {

var selects = $('#select-container select'),
results = $('#results-container > div');

selects.change(function () {


    var values = '';
    selects.each(function () {
        alert($(this).val());
        values += '.' + $(this).val();
    });


        var values = '{
        "goal" : $("#goal").val(),
        "current_level" : $("#current_level").val(),
        "gym_a" : $("#gym_a").val(),
        "days_P_W" : $("#days_P_W").val(),
        }';

var finalValue = eval('(' + values + ')');

});

}); });

here goal , current_level , gym_a etc are id's of your select tags.这里的目标,current_level,gym_a 等是您选择的标签的 id。

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

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