Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将复选框的值插入数据库?_Php_Mysqli - Fatal编程技术网

Php 如何将复选框的值插入数据库?

Php 如何将复选框的值插入数据库?,php,mysqli,Php,Mysqli,我这里有我的代码,关于我的复选框,但我得到了一些错误,当我点击我的提交按钮。虽然它会打印我在复选框中选择的所有值,但我的sql脚本中有一个错误,警告:mysqli_query至少需要2个参数,1个参数在第21行的C:\xampp\htdocs\project\candidate\president2.php中给出。我只想保存我在数据库中选择的值。请帮忙 <?php session_start(); ?> <?php //se

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

         <?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个候选图片,另一张是一个


mysqli_查询函数要求$mysqli链接作为第一个参数。有两种方法可以修复错误。下面是错误

要解决这个问题,只需将其更改为下面两个>Id中的一个,使用第一个选项,因为您已经在代码中的某个地方使用了它

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


看看我的例子,我希望它能帮助你-

<?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_查询函数至少需要两个参数。根据,第一个参数应为:

mysqli_connect或mysqli_init返回的链接标识符


后跟作为第二个参数的查询。您似乎没有在代码中使用这两个函数。看到您声明了一个mysqli对象,您可能打算改为使用$mysqli->query。

看起来您混合了OOP和过程mysqli函数。mysqli\u查询的第一个参数应该是link或use$link->see此doc@njk&farmer1992。。谢谢你的建议,现在我得到了我需要的。。。thaks很多..问题出在mysqli身上。虽然这确实解释了如何使用不推荐使用的mysql_uu函数,但它并没有解决这里的问题。@TJohnW。现在我的问题是如何计算被选中的候选人。如果您能建议我怎么做,我将不胜荣幸,先生。。。但再次感谢你的帮助。。。
 mysqli_query($mysqli, "INSERT INTO sample (name) VALUES ('". $ch ."') ");
<?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>