【问题标题】:how can i insert the values of my checkboxes into my database?如何将复选框的值插入到数据库中?
【发布时间】:2013-01-26 05:10:28
【问题描述】:

我在这里有关于我的复选框的代码,但是当我点击我的提交按钮时出现了一些错误。尽管它打印了我在复选框上选择的所有值,但我在我的 sql 脚本上出现错误,说“警告:mysqli_query() 需要至少 2 个参数,1 个在 C:\xampp\htdocs\project\candidate\president2.php 中给出在第 21 行"。我只想保存我在数据库中选择的值。请帮忙..

         <?php session_start(); ?>
         <?php
         //server info
          $server = 'localhost';
         $user = 'root';
         $pass = 'root';
        $db = 'user';

            // connect to the database
          $mysqli = new mysqli($server, $user, $pass, $db);

          // show errors (remove this line if on a live site)
               mysqli_report(MYSQLI_REPORT_ERROR);
           ?>
           <?php

           if ($_POST['representatives']){

         $check = $_POST['representatives'];
         foreach ($check as $ch){
           //this is my line 21 error. what i want here is to save the selected checkbox into my database but i got some error and i couldnt save it to my database
         mysqli_query("INSERT INTO sample (name) VALUES ('". $ch ."') ");
            echo  $ch. "<br>";
            }
            }
           ?>
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
      <html>
     <head>  
        <script type="text/javascript">
        <!--
    function get_representatives_value()
     {
      for (var i=0; i < document.list.representatives.length; i++)

      {
     if (document.list.representatives[i].value = true)
    {
    return document.getElementById('txt').innerHTML =document.list.representatives[i].value

    }
     }
     }

   //-->
    </script>
  title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <link href="candidate.css" rel="stylesheet" type="text/css">
   </head>
   <body> <p id="txt"></p>
   <form name="list" action="president2.php" method="post" onSubmit="return get_representatives_value()">
<div id="form"> 
     <?php
    // get the records from the database
     if ($result = $mysqli->query("SELECT * FROM candidate_info WHERE position= 'representatives' AND department ='CCEITE' ORDER BY cand_id"))
        {
      // display records if there are records to display
        if ($result->num_rows > 0)
          {
           // display records in a table
        echo "<table border='1' cellpadding='10'>";

         // set table headers
         echo "<tr><th>Student ID</th><th>Candidate ID</td><th>Course</th><th colspan = '3'>Name</th></tr>";

        while ($row = $result->fetch_object())
                  {
         // set up a row for each record
        echo "<tr>";
      echo "<td>" . $row->cand_studid . "</td>";
  echo "<td>".$row->cand_id."</td>";
 echo "<td>" . $row->course . "</td>";
     echo "<td coslpan ='5'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
  echo "<td><input type ='checkbox' name='representatives[]' id='". $row->studid ."' value='" . $row->fname . " ". $row->mname ." ". $row->lname .  "'onchange='get_representatives_value()' /></td>";
 echo "</tr>";
                                    }
echo "</table>";
                            }
         // if there are no records in the database, display an alert message
                            else
                            {
          echo "No results to display!";
                            }
                    }
           // show an error if there is an issue with the database query
            else
                    {
                 echo "Error: " . $mysqli->error;
                    }

           // close database connection
           $mysqli->close();

    echo "<input type='submit' name='representatives  value='Submit' />";

       ?> 
   </div>
 </form>
    </body>
   </html> 

这是我输出的预览,第一张图片是我选择了 2 个候选者,另一个是一个。

【问题讨论】:

  • 看起来您正在混合使用 OOP 和程序 mysqli 函数。
  • mysqli_query 的第一个参数应该是链接或使用 $link-> 参见这个文档php.net/manual/en/mysqli.query.php
  • @ njk & farmer1992.. 谢谢你的建议,现在我得到了我需要的东西...... 非常感谢......

标签: php mysqli


【解决方案1】:

mysqli_query 函数需要 $mysqli 链接作为第一个参数。有两种方法可以修复错误。以下是错误

mysqli_query("INSERT INTO sample (name) VALUES ('". $ch ."') ");

要解决此问题,只需将其更改为以下两个之一 >(我使用第一个选项,因为您已经在代码中的某个地方使用它。)

$mysqli->query("INSERT INTO sample (name) VALUES ('". $ch ."') ");

 mysqli_query($mysqli, "INSERT INTO sample (name) VALUES ('". $ch ."') ");

【讨论】:

    【解决方案2】:

    正如它所说,函数mysqli_query() 至少需要两个参数。根据PHP documentation,第一个参数应该是:

    mysqli_connect()mysqli_init() 返回的链接标识符

    紧随其后的查询作为第二个参数。您似乎没有在代码中使用这些函数中的任何一个。看到您声明了一个 mysqli 对象,您可能打算改用$mysqli-&gt;query()

    【讨论】:

      【解决方案3】:

      看看我的例子,我希望它会对你有所帮助..:-

      <?php
      if(isset($_POST['team']))
      {
      foreach($_POST['team'] as $value){
      $insert=mysql_query("INSERT INTO team('team') VALUES ('$value')");
      }
      }
      ?>
      <html>
      <body>
      <form method="post" action="lol.php">
      <input type="checkbox" name="team[]" value="IN"> India<br />
      <input type="checkbox" name="team[]" value="DK"> Dark <br />
      <input type="checkbox" name="team[]" value="LA"> lolax <br />
      <input type="submit" name="submit" value="submit">
      </form>
      </body>
      </html>
      

      【讨论】:

      • 问题出在mysqli上。虽然这确实解释了如何使用已弃用的 mysql_ 函数,但它并没有解决这里的问题。
      • @TJohnW。现在我的问题不再是计算那些被选中的候选人了。先生,如果您能建议我怎么做,我会很高兴……但再次感谢您的帮助……
      猜你喜欢
      • 1970-01-01
      • 2017-02-27
      • 2010-09-19
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多