Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果我重复输入,如何禁用对数据库的写入,并在alertbox中显示警告?_Php_Javascript - Fatal编程技术网

Php 如果我重复输入,如何禁用对数据库的写入,并在alertbox中显示警告?

Php 如果我重复输入,如何禁用对数据库的写入,并在alertbox中显示警告?,php,javascript,Php,Javascript,我对php有点陌生,所以我需要帮助。在一个页面上,我有一个带有firstname和lastname字段的小表单。在提交时,我将它发送到external.php脚本,在那个里我想检查数据库中是否已经有同名的人。若数据库中已经有输入姓名的人,我想取消对数据库的写入,并返回html页面。我已经这样做了,但是我想显示JS消息框“具有此名称的人已经存在于数据库中!”,如果对base的写入被取消,我想我的输入字段显示上次写入的值。我读了很多文学作品,我迷路了。无论如何,谢谢你的帮助:) 这是我的表格: &l

我对php有点陌生,所以我需要帮助。在一个页面上,我有一个带有firstname和lastname字段的小表单。在提交时,我将它发送到external.php脚本,在那个里我想检查数据库中是否已经有同名的人。若数据库中已经有输入姓名的人,我想取消对数据库的写入,并返回html页面。我已经这样做了,但是我想显示JS消息框“具有此名称的人已经存在于数据库中!”,如果对base的写入被取消,我想我的输入字段显示上次写入的值。我读了很多文学作品,我迷路了。无论如何,谢谢你的帮助:)

这是我的表格:

<form name="form" action="write.php" method="post" onSubmit="return Validate()">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
<input type="submit">
</form>

名字:
姓氏:
我的php代码:

<?php
$con=mysqli_connect("localhost","root","","phptest");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['firstname'])) { 
$firstname = $_POST['firstname'];
}
if (isset($_POST['lastname'])) { 
$lastname = $_POST['lastname'];
}
$duplicate=mysqli_query($con,"SELECT * FROM persons WHERE FirstName='$firstname'");
if(mysqli_num_rows($duplicate)>0)
{
die('Name already exists' );//I want that this message shows in html file, as alert
}
else{
$sql="INSERT INTO Persons (FirstName, LastName)
VALUES('$firstname','$lastname')";
$result=mysqli_query($con,$sql);
if (!$result)
{
die('Error: ' . mysqli_error());
}
}
header( 'Location://localhost/ivanda.php' ) ;
mysqli_close($con);

?>
这行:

if(mysqli_num_rows($duplicate)>0)
{
die('Name already exists' );//I want that this message shows in html file, as alert
}
将其更改为以下代码:

if(mysqli_num_rows($duplicate)>0)
{
echo "<script>alert('Name already exist!!!'); </script>";
echo "<script>window.location.assign('ivanda.php'); </script>"; //add this line instead of header


}
if(mysqli\u num\u行($duplicate)>0)
{
echo“警报('名称已存在!!!');”;
echo“window.location.assign('ivanda.php');”;//添加此行而不是标题
}
-------------------------编辑----------------------------------

尝试这样做:

    $duplicate=mysqli_query($con,"SELECT * FROM Persons WHERE FirstName='$firstname' limit 1");
    if(mysqli_num_rows($duplicate)>0)
    {

    $row = mysqli_fetch_assoc($duplicate);

    echo "<script>alert('Name already exist!!!'); </script>";
    echo "<script>window.location.assign('ivanda.php?firsName=".$row['FirstName']."&lastName=".$row['LastName']."'); </script>"; //add this line instead of header

    }
    else{
    $sql="INSERT INTO Persons (FirstName, LastName)
    VALUES('$firstname','$lastname')";
    $result=mysqli_query($con,$sql);
    if (!$result)
    {
    die('Error: ' . mysqli_error());
    }
    }
$duplicate=mysqli_query($con,“从FirstName='$FirstName'限制1的人员中选择*);
如果(mysqli_num_行($duplicate)>0)
{
$row=mysqli\u fetch\u assoc($duplicate);
echo“警报('名称已存在!!!');”;
echo“window.location.assign('ivanda.php?firsName=“.$row['FirstName']”和lastName=“.$row['lastName']””);;;;//添加此行而不是标题
}
否则{
$sql=“插入到个人中(FirstName,LastName)
值(“$firstname”,“$lastname”)”;
$result=mysqli\u查询($con,$sql);
如果(!$result)
{
die('Error:'.mysqli_Error());
}
}
这样,您就可以通过url上的GET发送var…然后在您的表单中,您应该有如下内容:

<form name="form" action="write.php" method="post" onSubmit="return Validate()">
Firstname: <input type="text" name="firstname" value="<?php if(isset($_GET['firsName'])){echo $_GET['firsName'];} ?>">
Lastname: <input type="text" name="lastname" value="<?php if(isset($_GET['lastName'])) {echo $_GET['lastName'];} ?>">
<input type="submit">
</form>


Firstname:我已经试过了,它可以工作,但是我不能“重定向到html页面:/,如果我重定向到:header('Location://localhost/ivanda.php' ) ; 然后我无法阅读此消息,JS方法历史记录也是如此。back()这有助于我获得所需的内容,但部分原因是,当我被重定向回表单时,我最后输入的值不存在。我正在处理一个有20个输入字段的项目,所以如果我的输入无效,我想显示消息(您演示了这一点),然后让我返回表单,但所有最后输入的值仍然在它们的输入字段中。这样我就不用再从Begging输入20个输入字段了。。。这对我有帮助,但没有解决这个问题:/