dongzanghua8422 2014-04-24 09:38
浏览 55

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

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). 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:

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

    <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

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). 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.

  • 写回答

2条回答 默认 最新

  • dongqian5639 2014-04-24 09:50
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向