php - 对 name、id、onclick、onsubmit 感到困惑 - 我在数据库中提交数据时做错了什么?

标签 php mysql forms

我的代码如下。请帮我纠正代码。我对语法感到困惑。另外,我不知道如果存在任何数据库问题,到底应该在哪里放置错误消息警报以及如何关闭数据库。请帮忙

<form id="ContactForm" name="form1" method="post" onsubmit="submitdata()">
  <div>
    <div class="formwrap"><span>Your Name:</span><input name="fname" type="text" class="input" ></div>
    <div class="formwrap"><span>Your E-mail:</span><input type="text" name="femail" class="input" ></div>
    <div class="formwrap"><span>Your Mobile:</span><input type="text" name="fmobile" class="input" ></div>
    <div class="formwrap"><span>Your City:</span><input type="text" name="fcity" class="input" ></div>
    <div class="textarea_box"><span>Your Message:</span><textarea name="fmessage" cols="1" rows="1"></textarea></div>
    <a class="button" onClick="document.getElementById('ContactForm').submit()"><span class="shadow"></span>Send</a>
    <a class="button" onClick="document.getElementById('ContactForm').reset()"><span class="shadow"></span>Clear</a>
  </div>
</form>


<?php
function submitdata(){
  include "config/connection.php";
  $fdname = $_POST['fname']);
  $fdemail = $_POST['femail']);
  $fdmobile = $_POST['fmobile']);
  $fdcity = $_POST['fcity']);
  $fdmessage = $_POST['fmessage']);

  $sql="INSERT INTO users (name, email, mobile, city, message)
  VALUES ('$fdname', '$fdemail', '$fdmobile', '$fdcity', '$fdmessage')";
}
?>

谢谢

最佳答案

我更改了您的 html 表单上的操作,并创建了 php 脚本,该脚本将处理您的数据并将其存储到数据库中,请查看并进一步阅读准备好的语句。

<form id="ContactForm" name="form1" action="insert.php" method="post">
  <div>
    <div class="formwrap"><span>Your Name:</span><input name="fname" type="text" class="input" ></div>
    <div class="formwrap"><span>Your E-mail:</span><input type="text" name="femail" class="input" ></div>
    <div class="formwrap"><span>Your Mobile:</span><input type="text" name="fmobile" class="input" ></div>
    <div class="formwrap"><span>Your City:</span><input type="text" name="fcity" class="input" ></div>
    <div class="textarea_box"><span>Your Message:</span><textarea name="fmessage" cols="1" rows="1"></textarea></div>
    <a class="button" onClick="document.getElementById('ContactForm').submit()"><span class="shadow"></span>Send</a>
    <a class="button" onClick="document.getElementById('ContactForm').reset()"><span class="shadow"></span>Clear</a>
  </div>
</form>

插入.php

<?php
    if($_POST){
        $db = new mysqli("address","id","password","DBname");
        if (mysqli_connect_errno())
        {
            echo "Connection Failed: " . mysqli_connect_errno();
            exit();
        }

        $stmt = $db->prepare("INSERT INTO users (name, email, mobile, city, message) VALUES (?, ?, ?, ?, ?)");
        $stmt -> bind_param('sssss', $fname, $femail, $fmobile, $fcity, $fmessage);

        $fname = $_POST['fname'];
        $femail = $_POST['femail'];
        $fmobile = $_POST['fmobile'];
        $fcity = $_POST['fcity'];
        $fmessage = $_POST['fmessage'];

        $stmt -> execute();
        $stmt -> close();
        $db -> close();
        }
?>

关于php - 对 name、id、onclick、onsubmit 感到困惑 - 我在数据库中提交数据时做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25962168/

相关文章:

php - 用 PHP 在 CSV 文件中写一个逗号

python - 数据库错误 python manage.pysyncdb

php - 数据库未从 php 更新

javascript - 在 HTML5 范围输入上禁用跟踪

css - 如何在使用 CSS 进行选择后显示输入字段

php shell_exec() 在后台运行脚本所需的帮助

PHP 函数不返回任何值

php - Erlang 和 PHP 之间的加密

javascript - 想要通过在 codeigniter 中将数据导出到 Excel 工作表来设置表格边框

javascript - 没有刷新页面的 HTML 提交表单并显示警告框