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

时间:2013-01-26 05:10:28

标签: php mysqli

我这里有关于我的复选框的代码,但是当我点击我的提交按钮时出现了一些错误。虽然它打印了我在复选框上选择的所有值,但我的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候选人而另一张是一个。

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

mysqli_query函数需要$ mysqli链接作为第一个参数。有两种方法可以解决您的错误。以下是 ERROR

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

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

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

OR

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

答案 1 :(得分:0)

正如它所说,函数mysqli_query()至少需要两个参数。根据{{​​3}},第一个参数应为:

  

mysqli_connect()mysqli_init()

返回的链接标识符

其次是查询作为第二个参数。您似乎没有在代码中使用这些功能中的任何一个。看到你声明了一个mysqli对象,你可能想要改为使用$mysqli->query()

答案 2 :(得分:-1)

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

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