【问题标题】:How do I separate the result of an sql query?如何分离 sql​​ 查询的结果?
【发布时间】:2016-01-11 22:14:04
【问题描述】:

所以我使用 jQuery 在我的表单上动态实现了文本字段,然后用它插入到我的数据库中。现在我遇到了两个问题,一个是每次用户发布表单条目时,我都会将行内的所有条目以逗号分隔。其次,当我从数据库中检索相同的内容时,我得到了整个行。我想要的是每个单词单独放在一个单独的 div 中。这是两者的代码:

form.php

<?php 
include "init.php";
?>

<!DOCTYPE html>
  <html>
    <head>
      <!--Import Google Icon Font-->
      <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

      <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>
      <link type="text/css" rel="stylesheet" href="css/animation.css">

      <!--Let browser know website is optimized for mobile-->
      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>

    <body>
          <form action ="result.php" method="post">

                    <div class="row">
                      <div class="input-field col s4 green-text text-lighten-3">

                        <input name="set_name" type="text" class="validate">
                        <label for="set_name">Set Name</label>

                      </div>
                    </div>

                    <div class="details">
                    </div>


                    <br>
                    <br>
                    <br>


                    <button class="btn waves-effect waves-light orange darken-4 text" type="submit" id="" name="action">Save
                    </button>

                    <div class="right bottom">
                    <a class="btn-floating btn-large waves-effect waves-light pink add_field_button"><i class="material-icons">add</i></a>  
                    </div>
                  </form>

      <!--Import jQuery before materialize.js-->
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
      <script type="text/javascript" src="js/materialize.min.js"></script>
           <script type="text/javascript">

      $(document).ready(function() {
          var max_fields      = 10; //maximum input boxes allowed
          var wrapper         = $(".details"); //Fields wrapper
          var add_button      = $(".add_field_button"); //Add button ID

          var x = 1; //initlal text box count
          $(add_button).click(function(e){ //on add input button click
              e.preventDefault();
              if(x < max_fields){ //max input box allowed
                  x++; //text box increment
                  $(wrapper).append('<div class="row"><div class="input-field col s6 green-text text-lighten-3"><input name="word[]" type="text" class="validate"><label for="word[]">Word</label></div><div class="input-field col s6 green-text text-lighten-3"><input name="def[]" type="text" class="validate"><label for="def">Definition</label></div><a class="btn waves-effect waves-light blue-grey darken-4 remove_field"><i class="material-icons">delete</i></a>  </div>'); //add input box
              }
      });

      $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
          e.preventDefault(); $(this).parent('div').remove(); x--;
      })
  });
  </script>
</body>

这是result.php

<?php 
include "init.php";
$name = $_POST["set_name"];
if($_POST){
    $word = implode(",", $_POST["word"]);
    $def = implode(",", $_POST["def"]);
    echo $word;
    echo "<br>";
    echo $def;
}
$sql_query = "insert into table values('$name', '$word', '$def');";
if(mysqli_query($conn, $sql_query)) {
        echo '<script language="javascript">';
        echo 'alert("Inserted!")';
        echo '</script>';

    }
    else {
        echo '<script language="javascript">';
        echo 'alert("Error in database, cannot insert.")';
        echo '</script>';
    }


$result = mysqli_query($conn, "SELECT set_word FROM ayush95 WHERE set_name = 'Hindi';");
if($result->num_rows > 0){

    echo $result->fetch_object()->set_word;

}

$conn->close();
?>

【问题讨论】:

    标签: php jquery html mysql


    【解决方案1】:

    您可以使用explode php 函数。这是 PHP 文档中的一个示例。

    // Example 1
    $pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
    $pieces = explode(" ", $pizza);
    echo $pieces[0]; // piece1
    echo $pieces[1]; // piece2
    

    【讨论】:

      【解决方案2】:

      TheZozoOwner 部分正确;但是,该代码仍会在一行中全部回显。

      如果您希望它在单独的行中回显,试试这个...

      $pieces = explode(",", $result->fetch_object()->set_word);
      $count = count($pieces);
      
      for($i=0; $i < $count; $i++) {
      echo $pieces[$i].'<br>';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 2019-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-08
        相关资源
        最近更新 更多