尝试使用mysql和php插入数据

时间:2017-09-27 09:22:49

标签: php mysql

如果您查看下面的查询,我无法插入此查询,因为它返回的错误是:

  

未定义的变量:

error in C:\wamp64\www\course selector\senthil\insert_data.php on line 25

我的代码是:

<?php

        require_once("connectdb.php");


        $name = $_POST["name"];
        $email = $_POST["email"];
        $contact = $_POST["contact"];
        $country = $_POST["country"];
        $state = $_POST["state"];
        // $Areaofstudy = $_POST["Areaofstudy"];

        $course = $_POST["course"];

        $institutes = $_POST["checkbox"];

        if(isset($_POST['submit']))
        {
            for ($i=0; $i<sizeof($institutes); $i++)
                {
            // $query ="INSERT INTO `enquiries` (`name`, `email`, `contact`, `country`, `state`, `area_of_study`, `course`, `institute`) VALUES ('" . $name ."', '" . $email . "', '" . $contact .  "', '" . $country . "', '" . $state . "', '" . $Areaofstudy . "', '11', '" . $institutes . "')";

          $query = "INSERT INTO `enquiries` (`name`, `email`, `contact`, `country`, `state`, `area_of_study`, `course`, `institute`) VALUES ('" . $name ."', '" . $email . "', '" . $contact .  "', '" . $country . "', '" . $_POST['Areaofstudy'] . "', '" . $_POST['course'] . "', '16', '17')";

        $result = $dbhandle->query($query) or die($error) ;
    }
    }

     ?>

3 个答案:

答案 0 :(得分:0)

第25行是$result = $dbhandle->query($query) or die($error);,因为在你在该行中使用它之前没有定义$error,它会抛出一个“未定义的变量:错误”错误。

如果你正在使用PDO,你可能会这样做:

$result = $dbhandle->query($query) or die($dbhandle->errorCode());

或从$dbhandle->errorInfo()获取更详细的错误信息。

答案 1 :(得分:-1)

首先未声明变量 $ error 。你必须先申报。

$error = 'Error description here';

然后你就可以打电话了。

答案 2 :(得分:-1)

检查此行

$institutes[$i = $_POST["checkbox"];

<强>正确

$institutes[$i = $_POST["checkbox"]];