【问题标题】:Form data not populating in MySQL database [closed]表单数据未填充到 MySQL 数据库中[关闭]
【发布时间】:2014-11-25 17:17:38
【问题描述】:

这是表格:

<form id="secursendmsg" action="server.php" method="post">
        <div id="body_errors">
        </div>

        <textarea id="msg_body" name="msg_body'">
        </textarea>

        <script type="text/javascript">
          function showMe (box) {

            var chboxs = document.getElementsByName("read");
            var vis = "none";
            for(var i=0;i<chboxs.length;i++) {

              if(chboxs[i].checked){
                vis = "block";
                break;
              }
            }
            document.getElementById(box).style.display = vis;


          }
        </script>

        <p id="notifycheck" class="notify">
          <input type="checkbox" name="read" name="notify" onclick="showMe('notify')"/>

          <label for="id_notify">
            Enable Read Receipts
          </label>
        </p>
        <div id="notify" style="display:none">
          <div id="email_errors">
          </div>

          <div class="left">
            <p>
              <label for="email">
                Contact email (suggest Anonmail address):
              </label>

              <br>
              <input id="email" type="text" name="sender_email" size="35" rows="3" maxlength="100"/>
            </p>

          </div>
        </div>
        <p id="pwrapper">
          <div class="button1">
            <i class="fa fa-lock"></i> Submit
        </div>

        <div class="settings">

          <i class="fa fa-cog" style="font-size: 20px;">
          </i>
        </div>

      </div>
    </form>

我的 PHP 代码如下:

<?
if( $_POST )
{
  $con = mysql_connect("192.99.xxx.xxx","root","LAxxxxxd!");

  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("SecurSend", $con);

  $sent = $_POST['msg_body'];
  $email = $_POST['email'];


  $sent = mysql_real_escape_string($sent);
  $email = mysql_real_escape_string($email);




  $query = "
INSERT INTO `SecurSend`.`SecurMSG` (`email`,`sent`) VALUES         ('".$sent."','".$email."');";"

  mysql_query($query);

  echo "<h2>Test Complete!</h2>";

  mysql_close($con);
}
?>

无论如何,我从来没有得到回声,信息也没有填充。我无法理解为什么。表单和 PHP 不托管在服务器上,而是通过 Cordova 托管在应用程序内。

【问题讨论】:

  • ('".$sent."','".$email."');";"
  • $email = $_POST['sender_email'];
  • 在您打开&lt;?php标签error_reporting(E_ALL); ini_set('display_errors', 1);后立即将错误报告添加到文件顶部,也可以将or die(mysql_error())添加到mysql_query();这些是您在开发时应该使用的宝贵工具。
  • 我知道你在等我说......拜托,don't use mysql_* functions,它们不再维护,而是officially deprecated。改为了解prepared statements,并使用PDOMySQLi。您还想Prevent SQL Injection!
  • &lt;i class="fa fa-lock"&gt;&lt;/i&gt; Submit - 你是如何提交表格的?

标签: php html mysql forms cordova


【解决方案1】:

除了email文本名称问题,你的sql行搞砸了:

$query = "INSERT INTO SecurSend.SecurMSG(email,sent)VALUES('".$sent."','".$email."')";

【讨论】:

  • 谢谢,伙计们。现在语法显示正确,但是没有填充数据。感谢您发现电子邮件错误,我目前仅使用 msg_body 进行测试,但没有通过。
【解决方案2】:

改变这个

 $email = $_POST['email'];

$email = $_POST['sender_email'];

您已将表单中的电子邮件命名为sender_email,并在 php 中使用了email

您在$query 字符串中还有一个额外的双引号。删除该代码后将是:

$query = "INSERT INTO SecurSend.SecurMSG(email,sent)VALUES('".$sent."','".$email."')";

就您显示的代码而言,您并未提交表单。

所以改变

<div class="button1">
     <i class="fa fa-lock"></i> Submit
</div>

<button class="button1" type="submit">
    <i class="fa fa-lock"></i> Submit
</button>

【讨论】:

  • 我想知道 OP 是如何提交表单的。这个&lt;i class="fa fa-lock"&gt;&lt;/i&gt; Submit 很难说 OP 是否正在尝试使用不符合提交按钮的条件。可能是一个促成因素。
  • 是的,我确实抓住了它,但忘了把它扔在这里。我正在使用不同的按钮进行测试。
  • @Autonomous 您是否有机会阅读您的问题下留下的 cmets?特别是关于checking for errors
  • @Fred-ii-Nope,现在才看到。有点习惯 Stack Overflow 的界面。我会试试的。
  • @Autonomous 好的。请访问 meta.stackexchange.com/a/5235 了解您在其他问题中给出的答案。如果有任何人解决了问题,请返回给定的答案并勾选我在此处给您的链接中所示的复选标记。这会将问题标记为已解决。 干杯
猜你喜欢
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
相关资源
最近更新 更多