0

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

在此处输入图像描述

在此处输入图像描述

4

3 回答 3

0

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

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

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

于 2013-01-26T05:21:38.540 回答
0

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 ."') ");
于 2013-01-26T05:15:08.913 回答
-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>
于 2013-01-26T05:16:54.170 回答