尝试使用mysql和php插入数据
如果查看下面的查询,我无法插入此查询,因为它返回的错误是: 未定义变量:尝试使用mysql和php插入数据,php,mysql,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"]; $conta
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) ;
}
}
?>
变量$error不是首先声明的。你必须先申报
$error = 'Error description here';
然后你可以叫它。检查这行
$institutes[$i = $_POST["checkbox"];
正确
$institutes[$i = $_POST["checkbox"]];
第25行是$result=$dbhandle->query($query)或die($error)由于在该行中使用之前未定义$error
,因此它将抛出“undefined variable:error”错误
如果您正在使用PDO,您可能会:
$result=$dbhandle->query($query)或die($dbhandle->errorCode())代码>
或者从$dbhandle->errorInfo()
获取更详细的错误信息,检查代码$institutes[$i=$\u POST[“checkbox”];除了Aman Kumar提到的行中的错误外,您还应该清理这些POST VAR。这是一种sql注入危险,您在哪里声明了$error变量?$institutes[$i]=…而不是您如何编写它。请查看后面的。。。