我是PHP的初学者,我正在尝试网站注册等等。我一直在收到错误"意外的'赶上' (T_CATCH)在第67和34行的C:\ wamp \ www \ ass \ signup.php中;每当我点击我的注册表单上提交时(它以不同的html格式)。以下是我在html表单上点击提交时调用的php文件的代码。如果这里有很多菜鸟错误,请帮助和非常抱歉。正如我所说,我是初学者。
<?php
include('connect.php');
$username =$_POST['username'];
$firstname = $_Post['firstName'];
$lastname = $_Post['lastName'];
$emailadd = $_POST['emailAdd'];
$gender = $_POST['gender'];
$userpassword = $_Post['userPassword'];
$dayofbirth = $_POST['dayOfBirth'];
$monthofbirth = $_POST['monthOfBirth'];
$yearofbirth = $_POST['yearOfBirth'];
$profiledesc = $_POST['profileDesc'];
$date = $_POST['yearOfBirth'] . '-' . $_POST['monthOfBirth'] . '-' . $_POST['dayOfBirth'];
try {
$host = "localhost";
$username = "root";
$password = "";
$database = "users";
$dsn = "mysql:host=$host;dbname=$database";
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO userProfile("
. "username, firstName, lastName, emailAdd, gender, userPassword, birthday, profileDesc"
. " ) VALUES ("
. "'" . $lastname . "',"
. "'" . $firstname . "',"
. "'" . $emailadd . "',"
. "'" . $gender ."';"
. "'" . $userpassword ."';"
. "'" . $date ."';"
. "'" . $profiledesc ."';";
$conn->query($sql);
$sql = "SELECT * FROM users";
$users = $conn-query($sql);
echo '<table>';
echo '<tr>';
echo '<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Gender</th>';
echo '<tr>';
foreach ($users as $users) {
echo '<tr>';
echo ' <td>' . $users['firstName'] . '</td>';
echo ' <td>' . $users['lastName'] . '</td>';
echo ' <td>' . $users['emailAdd'] . '</td>';
echo ' <td>' . $users['gender'] . '</td>';
echo ' </tr> ';
}
echo '</table>';
$conn = null;
catch (PDOException $e) {
$conn = null;
exit("Connection failed: " . $e->getMessage());
}
?>
答案 0 :(得分:2)
您缺少尝试阻止
的结束括号$conn = null;
}// this brace
catch (PDOException $e) {
$conn = null;
exit("Connection failed: " . $e->getMessage());
}
您的INSERT
命令(缺少括号,额外的半冒号)中也存在语法错误,并且可以使用SQL注入。我不想向您介绍安全性,但您正在使用PDO,因此您至少可以准备好查询:
$sql = "INSERT INTO userProfile
(username, firstName, lastName, emailAdd, gender, userPassword, birthday, profileDesc)
VALUES (:lastname, :firstname, :emailadd, :gender, :userpassword, :date, :profiledesc)";
$conn->prepare($sql);
$conn->execute(array(
':lastname' => $lastname ,
':firstname' => $firstname,
':emailadd' => $emailadd,
':gender' => $gender,
':userpassword' => $userpassword,
':date' => $date,
':profiledesc' => $profiledesc
));
答案 1 :(得分:1)
格式化代码可帮助您解决问题。我想这是语法错误,但是,如果你还是找不到它为什么会这样。 catch
阻止要求try
关闭,就像
try { smth } catch {
但是,您的try
区块中没有右括号。
可能需要在$conn = null;
我猜
答案 2 :(得分:0)
您需要先关闭try
阻止,然后再致电catch
:
$conn = null;
} catch (PDOException $e) {