【问题标题】:Can't insert data into MYSQL from form using php无法使用php从表单将数据插入MYSQL
【发布时间】:2023-03-16 19:36:02
【问题描述】:

我想使用 PHP 将数据从 HTML 表单插入 MYSQL。当我在表单中输入一个有效的字符串并提交它时,按照代码重定向到 ma​​in.php 顺便说一句,输入的字符串没有插入进入MYSQL表。

下面是我的代码

HTML


<form class="form" action="" method="post" >
    <input type="text" placeholder="User Id" id="userid" name="userid" title="Enter your Roll number">
    <button onclick="validate()" type="submit" id="login-button" name="formSubmit" value="submit">Login</button>

</form>

这是表单验证。

JS


function validate()
{
    var id=document.getElementById('userid').value;
    if(id=="")
    {
        document.getElementById('msg').innerHTML = "Please enter User Id";
        setTimeout(function(){
        window.location.replace('index.php');
        }, 2000);
    }
    else
    {
        if(id.length=="10")
        {
            document.getElementById('msg').innerHTML = "Login Success";
            setTimeout(function(){
            window.location.replace('main.php');
            }, 2000);
        }
        else
        {   
            document.getElementById('msg').innerHTML = "Please enter valid User Id";
            setTimeout(function(){
            window.location.replace('index.php');
            }, 2000);
        }
    }
}

PHP


mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("gcet_elib") or die(mysql_error());

if(isset($_POST['formSubmit']))
{

    $userid = $_POST['userid'];

    $query = "INSERT INTO users ( uid ) VALUES ('$userid')";

    $result = mysql_query($query);

    if($result){
        //$msg = "User Created Successfully.";

        echo '<script language="javascript">';
        echo 'alert("Login Successfull")';
        echo '</script>';

    }

else
{
    echo '<script language="javascript">';
    echo 'alert("Cant submit this form")';
    echo '</script>';
}

}



所以请帮我找出代码中的错误。

【问题讨论】:

  • 你的 validate() 函数在哪里
  • 您的数据库连接在哪里? - 另外,mysql_query 在语句中需要数据库“链接”,请参阅mysql_query - 还有!请注意 mysql 已弃用,您应该考虑使用 mysqli 或 PDO
  • 检查你的数据库连接是否成功。
  • 按钮标签不要提交值,而是使用 .. 并确保您有一个有效的数据库连接.. .
  • @Akshay:你在服务器上试过他的代码吗??

标签: php html mysql


【解决方案1】:

将您的 html 替换为以下内容:

<form class="form" action="main.php" method="post">
  <input type="text" placeholder="User Id" id="userid" name="userid" title="Enter your Roll number">
  <button onclick="return validate()" type="submit" id="login-button" name="formSubmit" value="submit">Login</button>

</form>

用以下内容替换你的javascript:

function validate() {
  var id = document.getElementById('userid').value;
  if (id == "") {
    document.getElementById('msg').innerHTML = "Please enter User Id";
    return false;
  } else {
    if (id.length == "10") {
      document.getElementById('msg').innerHTML = "Login Success";
      return true;
    } else {
      document.getElementById('msg').innerHTML = "Please enter valid User Id";
      return false;

    }
  }
}

【讨论】:

  • 我试过了,顺便说一句,它不起作用,也没有重定向到 main.php。感谢您的帮助。
  • &lt;button onclick="return validate();之后加一个分号,并将代码if (id.length == "10") {替换为if (id.length == 10) {
  • 为什么 OP 应该“试试这个”?一个好的答案总是会解释所做的事情以及这样做的原因,不仅对 OP,而且对未来的 SO 访问者。
【解决方案2】:

当我运行此代码时,它会发出类似“无法提交此表单”的警报。

这可能是因为您的表单要求未满足,而 validate() 函数中定义了该要求。检查您的 javascript validate() 函数。这可能是错误的。因此,将其发布为编辑,或者您可能没有将所需的信息传递给 validate() 为 true 所需的输入字段。

mysql 或 php 现在不是问题,因为您的表单甚至没有提交。

您的脚本可能有误 检查一下,看看我放的 cmets 并给出一个想法。

function validate()
        {
            var id=document.getElementById('userid').value;
            //if id is null this will rise
            if(id=="")
            {
                document.getElementById('msg').innerHTML = "Please enter User Id";
                setTimeout(function(){
                window.location.replace('index.php');
                }, 2000);
            }
            //if the id is not null this means a string is passed
            else
            {
                //if the id is exactly 10 characters long
                if(id.length=="10")
                {
                    document.getElementById('msg').innerHTML = "Login Success";
                    setTimeout(function(){
                    window.location.replace('main.php');
                    }, 2000);
                }
                //if the id is not null and character length is not equal to 10
                else
                {   
                    document.getElementById('msg').innerHTML = "Please enter valid User Id";
                    setTimeout(function(){
                    window.location.replace('index.php');
                    }, 2000);
                }
            }
        }

在这种情况下,如果 id 不为 null 或 null,它会一直将window.location.replace('index.php'); 重定向到 index.php,这意味着即使一个字符串被传递给它,它也会刷新。因此,请根据您的喜好更正这些条件语句以使其工作

【讨论】:

  • 当我输入一个有效的字符串时,页面将导航到 main.php,正如 JS btw 中所说,该值不会插入到 MYSQL 表中
  • 是的,如果字符串无效,我想将页面重定向到 index.php,如果字符串有效,它会重定向到 main.php。
【解决方案3】:

修改你的 php 代码,如下所示:

mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("stackov") or die(mysql_error());

if(isset($_POST['formSubmit']))
      {

        $userid = $_POST['userid'];

        $query = "INSERT INTO users ( id ) VALUES ('$userid')";

        $result = mysql_query($query);

        if($result){
            //$msg = "User Created Successfully.";

            echo '<script language="javascript">';
            echo 'alert("Login Successfull")';
            echo '</script>';

        }
        else{
          echo '<script language="javascript">';
            echo 'alert("Cant submit this form")';
            echo '</script>';
      }
      }

我刚刚更改了else块的位置,所以消息无法提交表单不会弹出。

【讨论】:

  • 我的实际问题是数据没有被插入到 MYSQL 表中,顺便说一句,我只是用来发现错误。感谢您的帮助。
  • @Vijay 你应该在你的问题中澄清这一点。
【解决方案4】:

如果您在加载时看到该错误,这是正常的,因为您没有设置 $_POST['formSubmit'] 以便 if 循环重定向到 else 并显示错误。

在加载页面上,您没有设置 post 变量,但 php 部分将执行,它会找到 isset($_POST['formSubmit'])false,它会转到 else 语句。

进行以下更改。

  1. 在 JS 中,进行所有验证并让 JS 提交表单。
  2. 在if(isset($_POST['formSubmit'])里面制作完整的php代码,这样php代码只有在提交完成后才会执行。

【讨论】:

  • 感谢您的帮助。您能否清楚地解释一下如何更正代码。
  • @Vijay :我们不清楚你想从这些代码中做什么。
  • 您好,感谢您报告我的问题中的困惑。我现在更改了它,您可以再试一次吗。感谢您的帮助。
【解决方案5】:

如果您希望页面加载时没有错误,请将 PHP 文件更改为:

<?php
  mysql_connect("localhost", "root", "root") or die(mysql_error());
  mysql_select_db("stackov") or die(mysql_error());

  if(isset($_POST['formSubmit']))
  {

    $userid = $_POST['userid'];

    $query = "INSERT INTO users ( uid ) VALUES ('$userid')";

    $result = mysql_query($query);

    if($result){
        //$msg = "User Created Successfully.";

        echo '<script language="javascript">';
        echo 'alert("Login Successfull")';
        echo '</script>';

    }
    else{
        echo '<script language="javascript">';
        echo 'alert("Cant submit this form")';
        echo '</script>';
    }
  }
?>

在检查 isset($_POST) 上移动 else 语句

【讨论】:

  • 对不起,我的问题以前不清楚,我现在改了,你能再检查一下吗。谢谢你的帮助。
  • 我知道你的意思。你试过我对 PHP 文件的更改吗?无论如何,它可能会对您有所帮助。
  • 对不起,我的实际问题是数据没有被插入到 MYSQL 表中,顺便说一句,不是我刚刚用来发现错误的警报消息。感谢您的尝试。
【解决方案6】:

您只是重定向和更改窗口位置。你需要提交表格。您需要提供action="main.php" 的操作,并且在验证中,如果您的字段不为空或根据您的验证不正确,只需使用此$( "#formid" ).submit(); jquery 函数。您的表单将被提交,您将能够访问 main 中的帖子值.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多