本声明中的错误是什么
<?php
session_start();
$host = "localhost";
$uname = "root";
$pass = "";
$database = "mcndb";
$cons = mysqli_connect($host, $uname, $pass, $database);
mysqli_select_db($cons, $database);
if (!$cons) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['username'])) {
date_default_timezone_set("Asia/Bangkok");
$today = date("Y/m/d h:i:s:A");
$username = mysqli_real_escape_string($cons, $_POST['username']);
$password = mysqli_real_escape_string($cons, $_POST['password']);
$firstname = mysqli_real_escape_string($cons, $_POST['firstname']);
$middlename = mysqli_real_escape_string($cons, $_POST['middlename']);
$lastname = mysqli_real_escape_string($cons, $_POST['lastname']);
// $age=mysqli_real_escape_string($cons,$_POST['age']);
$gender = mysqli_real_escape_string($cons, $_POST['gender']);
$email = mysqli_real_escape_string($cons, $_POST['usremail']);
$companyname = mysqli_real_escape_string($cons, $_POST['companyname']);
$position = mysqli_real_escape_string($cons, $_POST['position']);
$contactnumber = mysqli_real_escape_string($cons, $_POST['contactnumber']);
$addresss = mysqli_real_escape_string($cons, $_POST['address']);
$sql = "INSERT INTO tbltry (username,password,email,firstname,middlename,lastname,gender, company_name,position,contact_number,address_of_company,dateofregister)
VALUES ('$username', '$password', '$email','$firstname', '$middlename','$lastname','$gender', '$companyname','$position','$contactnumber','$addresss','$today')";
if ($cons->query($sql) == TRUE) {
echo '<script>';
echo 'alert("Successfully created an account")';
echo '</script>';
} else {
echo '<script>';
echo "Error: " . $sql . "<br>" . $cons->error;
echo 'alert("Account already exist")';
echo '</script>';
}
}
?>
这个问题是,当我尝试在数据库中向唯一列注册一个dulicate值时,警报中的错误消息不显示,即使在sql中它也没有显示任何类型的错误它只是说Uncaught SyntaxError:意外的标识符,所以我的代码错误警告不显示错误?
答案 0 :(得分:1)
所以我的代码有什么问题
此:
<script>
Error: some text<br>some more text
alert("Account already exist")
</script>
(可能还有其他一些事情,但这似乎是你目前看到的错误发生的地方。)
第一行JavaScript代码实际上不是JavaScript代码。这只是文字。所以Error:
(以及它之后的所有内容)就JavaScript而言是一个意外的标识符。
我怀疑你打算把那个文本放在脚本块之外:
echo "Error: " . $sql . "<br>" . $cons->error;
echo '<script>';
echo 'alert("Account already exist")';
echo '</script>';