PHP& MySQL数据库存储名称Array问题

时间:2010-04-24 06:32:42

标签: php mysql

我是PHP&的新手MySQL和我不断得到存储在我的MySQL表字段$skill$experience$years中的单词Array我想知道如何解决这个问题?

而且我知道我需要添加mysqli_real_escape_string我遗漏的代码更容易阅读希望。

这是PHP& MySQL代码。

if (isset($_POST['info_submitted'])) {

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT learned_skills.*, users_skills.* 
                                   FROM learned_skills 
                                   INNER JOIN users_skills ON learned_skills.id = users_skills.skill_id 
                                   WHERE user_id='$user_id'");

if (!$dbc) {
    print mysqli_error($mysqli);
    return;
}

$user_id = '5';
$skill = $_POST['skill'];
$experience = $_POST['experience'];
$years = $_POST['years'];

if (isset($_POST['skill'][0]) && trim($_POST['skill'][0])!=='') {

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT learned_skills.*, users_skills.* FROM learned_skills INNER JOIN users_skills ON users_skills.skill_id = learned_skills.id WHERE users_skills.user_id='$user_id'");

if (mysqli_num_rows($dbc) == 0) {

for ($s = 0; $s < count($skill); $s++){
    for ($x = 0; $x < count($experience); $x++){
        for ($g = 0; $g < count($years); $g++){

                    $mysqli = mysqli_connect("localhost", "root", "", "sitename");
                    $query1 = mysqli_query($mysqli,"INSERT INTO learned_skills (skill, experience, years) 
                                                    VALUES ('" . $s . "', '" . $x . "', '" . $g . "')");

                    $id = mysqli_insert_id($mysqli);

                    if ($query1 == TRUE) {
                            $mysqli = mysqli_connect("localhost", "root", "", "sitename");
                            $query2 = mysqli_query($mysqli,"INSERT INTO users_skills (skill_id, user_id) 
                                                            VALUES ('$id', '$user_id')");

                    }   
        }
    }
}
}
}
}

这是XHTML代码。

<li><label for="skill">Skill: </label><input type="text" name="skill[0]" id="skill[0]" />
<label for="experience">Experience: </label>
<?php

echo '<select id="experience[0]" name="experience[0]">' . "\n";
  foreach($experience_options as $option) {
    if ($option == $experience) {
      echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
    } else {
      echo '<option value="'. $option . '">' . $option . '</option>'."\n";
    }
  }
echo '</select>';

?>
<label for="years">Years: </label>
<?php

echo '<select id="years[0]" name="years[0]">' . "\n";
  foreach($grade_options as $option) {
    if ($option == $years) {
      echo '<option value="' . $option . '" selected="selected">' . $option . '</option>' . "\n";
    } else {
      echo '<option value="'. $option . '">' . $option . '</option>'."\n";
    }
  }
echo '</select>';

?>
</li>

1 个答案:

答案 0 :(得分:0)

我没有明白你的错误点,我想你可能会在这个地方编码错误请检查

  for ($s = 0; $s < count($skill); $s++){
    for ($x = 0; $x < count($experience); $x++){
        for ($g = 0; $g < count($years); $g++){

                    $mysqli = mysqli_connect("localhost", "root", "", "sitename");
                    $query1 = mysqli_query($mysqli,"INSERT INTO learned_skills (skill, experience, years) 
                                                    VALUES ('" . $skill[$s] . "', '" . $experience[$x] . "', '" . $years[$g] . "')");

                    $id = mysqli_insert_id($mysqli);

                    if ($query1 == TRUE) {
                            $mysqli = mysqli_connect("localhost", "root", "", "sitename");
                            $query2 = mysqli_query($mysqli,"INSERT INTO users_skills (skill_id, user_id) 
                                                            VALUES ('$id', '$user_id')");

                    }   
        }
    }
}