我不明白为什么这么简单的事情是如此困难。
现在,当我点击提交时,我收到错误:
注意:未定义的变量:第6行的C:\ xampp \ htdocs \ DataHandling.php中的conn
致命错误:在第6行的C:\ xampp \ htdocs \ DataHandling.php中调用null上的成员函数prepare()
我的表单有效,代码:
<html>
<head>
<title>Gym Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="DataHandling.php" method="post">
<span>Gym Membership Registration</span><br><br>
<Span>Title: </Span><input type ="text" Value =" " name ="Title" /><br>
<Span>First Name: </Span><input type ="text" Value =" " name ="Fname" /><br>
<Span>Last Name: </Span><input type ="text" Value =" " name ="Lname" /><br><br>
<Span>Gender: </Span><select name ="Gender">
<option value ="Junior">Male</option>
<option value ="Adult">Female</option>
<option value ="Senior">Private</option>
</select><br>
<Span>DOB: </Span><input type ="date" name ="DOB" /><br><br>
<Span>MembershipExpiry: </Span> <input type ="date" name ="MemX" /><br>
<Span>MembershipType: </Span><select name = "MemType">
<option value ="Junior">Junior</option>
<option value ="Adult">Adult</option>
<option value ="Senior">Senior</option>
</select><br><br>
<Span>Email Address: </Span><input type ="email" name ="Email" /><br><br>
<input type="Submit" name="submit" value ="Submit Form">
然后我收到一条消息告诉我与数据库的连接已确认,conn.php:
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
$dbName = 'gym';
try
{
//Attempt connection passing in predefined connection variables.
$conn = new PDO("mysql:host=$hostname;dbname=$dbName", $username, $password);
echo ("Connected to Database Successfully. Welcome ".$username);
}
catch(PDOException $e)
{
//Use exception E to return PDO/MySQL specific error messages
echo $sql . "<br>" . $e->getMessage();
}
</body>
</form>
</html>
?>
但是,从表格输入数据到准备好的数据库时,我有一段非常可怕的时间。
我尝试了Sqli,现在尝试了别的东西。
<?php
//Prepare HTML insert statement binding parameters
$stmt = $conn->prepare("INSERT INTO records (Title,Fname,Lname,Gender,DOB,MemX,MemType,Email)
VALUES ('$title', '$fname', '$lname', '$gender', '$dob', '$memx', '$memtype', '$email')");
$stmt ->bindParam(':Title', $title);
$stmt ->bindParam(':Fname', $fname);
$stmt ->bindParam(':Lname', $lname);
$stmt ->bindParam(':Gender', $gender);
$stmt ->bindParam(':DOB', $dob);
$stmt ->bindParam(':MemX', $memx);
$stmt ->bindParam(':MemType', $memtype);
$stmt ->bindParam(':Email', $email);
//Attempt row insertion by executing prepared statement
try
{
//Insert a row
$title = $_POST['Title'];
$fname = $_POST['Fname'];
$lname = $_POST['Lname'];
$gender = $_POST['Gender'];
$dob = $_POST['DOB'];
$memx = $_POST['MemX'];
$memtype = $_POST['MemType'];
$email = $_POST['Email'];
$stmt->execute();
}
catch (PDOException $e)
{
echo $e->getMessage();
}
//Close Connection
$conn = null;
?>
答案 0 :(得分:1)
这不是PDO准备好的陈述的工作原理。这是一个例子:
$s = $conn->prepare('SELECT * FROM table WHERE column=:value');
$s->bindParam(':value', $value);
有关详细信息,请参阅the manual。
答案 1 :(得分:1)
如果你将conn.php包含在DataHandling.php之上,那么在你设置之后什么都不会有效
在conn.php结束时 $conn = null
。
conn.php
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
$dbName = 'gym';
$conn = null;
try
{
//Attempt connection passing in predefined connection variables.
$conn = new PDO("mysql:host=$hostname;dbname=$dbName", $username, $password);
}
catch(PDOException $e)
{
//Use exception E to return PDO/MySQL specific error messages
echo $sql . "<br>" . $e->getMessage();
}
?>
DataHandling.php 你准备好的陈述也是错误的。
<?php
require_once 'conn.php';
//Prepare HTML insert statement binding parameters
$stmt = $conn->prepare("INSERT INTO records (Title,Fname,Lname,Gender,DOB,MemX,MemType,Email)
VALUES (:Title, :Fname, :Lname, :Gender, :DOB, :MemX, :MemType, :Email)");
$title = $_POST['Title'];
$fname = $_POST['Fname'];
$lname = $_POST['Lname'];
$gender = $_POST['Gender'];
$dob = $_POST['DOB'];
$memx = $_POST['MemX'];
$memtype = $_POST['MemType'];
$email = $_POST['Email'];
//Attempt row insertion by executing prepared statement
try
{
//Insert a row
$stmt->bindParam(':Title', $title);
$stmt->bindParam(':Fname', $fname);
$stmt->bindParam(':Lname', $lname);
$stmt->bindParam(':Gender', $gender);
$stmt->bindParam(':DOB', $dob);
$stmt->bindParam(':MemX', $memx);
$stmt->bindParam(':MemType', $memtype);
$stmt->bindParam(':Email', $email);
$stmt->execute();
}
catch (PDOException $e)
{
echo $e->getMessage();
}
//Close Connection
$conn = null;
?>