如何将表单数据正确提交到SQL数据库? [英] How to submit form data to the SQL database correctly?

查看:151
本文介绍了如何将表单数据正确提交到SQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我提交数据的表单

<p align="center" style="color:#F00;">
  <?php echo $msg; ?>
</p>
<form class="form-horizontal style-form" name="form1" method="post" action="Create-jobs.php" onSubmit="return validate();">
  <p style="color:#F00"></p>
  <div class="form-group">
    <label class="col-sm-2 col-sm-2 control-label" style="padding-left:40px;">Job Title</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="job_title" placeholder="&lt;Enter Job name&gt;">
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-2 col-sm-2 control-label" style="padding-left:40px;">Job Description</label>
    <div class="col-sm-10">
      <textarea class="form-control" name="job_des" placeholder="&lt;Enter a detailed description&gt;" cols="17" rows="10"></textarea>
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-2 col-sm-2 control-label" style="padding-left:40px;">Job Requirements</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="job_reqs" placeholder="&lt;Enter requirements seperated by a comma&gt;">
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-2 col-sm-2 control-label" style="padding-left:40px;">Is the job active?(y/n)</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" name="job_reqs" placeholder="y/n">
    </div>
  </div>

  <div style="margin-left:15px; margin-bottom:15px;">
    <input type="submit" name="submit" value="Create Job" class="btn btn-theme"></div>
</form>

这是我用来提交表单数据的php代码。

And this is the php code I use to submit the form data.

define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'loginsystem');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if(isset($_POST['submit']))
{ 
    $jobtitle = $_POST['job_title'];
    $jobdes = $_POST['job_des'];
    $jobreqs = $_POST['job_reqs'];
    $jobactive = "";

    if(job_active == "y") {
        $jobactive = '1';
    } else {
        if(job_active == "n") {
            $jobactive = '0';
        } else {
            $msg = "error in job_active.";
        }
    }   

    $sql = "INSERT INTO jobs (jobID, job_title, job_des, job_reqs, job_active, Posting_date) VALUES (NULL, '$jobtitle', '$jobdes', '$jobreqs', '$jobactive', current_timestamp()')";

    mysqli_query($con,$sql);
    $msg = "it worked successfully!";
} else {
    $msg = "error submitting form.";
}

我正确输入了数据,并且代码通过了我所有的检查。($ msg =已成功触发工作。)但数据库本身未更新。我检查数据库本身是否有故障,但我读取数据库数据的其他页面正常运行。任何人都可以告诉我我的代码有什么问题?

I enter the data correctly and the code executes passing all my checks.($msg = "it worked successfully is being triggered.) but the database itself doesn't update. I checked to see if the database itself is faulty but my other pages that read database data function normally. Can anyone tell me what's wrong with my code?

推荐答案

在SQL语句的末尾,您留下了一个单引号,请尝试首先对其进行修复,如果不可以,可以对其进行修复。帮助,然后打印SQL并尝试将其手动插入MySQL,它应该显示出什么问题。

In the end of the SQL statement you left a single quotation mark. Try to fix it first and if it will not help, then print the SQL and try to insert it manually to MySQL, it should show what is wrong.

$sql = "INSERT INTO jobs (jobID, job_title, job_des, job_reqs, job_active, Posting_date) VALUES (NULL, '$jobtitle', '$jobdes', '$jobreqs', '$jobactive', current_timestamp())";

这篇关于如何将表单数据正确提交到SQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆