首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HTML表单不会通过PHP处理器将数据发送到MySQL数据库。

HTML表单不会通过PHP处理器将数据发送到MySQL数据库。
EN

Stack Overflow用户
提问于 2018-07-25 04:59:07
回答 1查看 380关注 0票数 2

上下文:

我正在使用以下堆栈: HTML/CSS,PHP,MySQL来构建一个web应用程序,用于存储和检索输入到表单中的数据。

问题:

当我在本地向表单(index.php的html部分)中输入数据时,实际上并没有将这些数据POSTing到我设置的mysql数据库中,并且抛出了一个错误,指出有一个未知的列。

我认为问题出在插入值部分。如果任何人有任何关于为什么它没有张贴的想法,我希望能得到任何帮助。耽误您时间,实在对不起!

Pics

代码

index.php

代码语言:javascript
复制
<?php

include("dbconfig.php");

try {

  /* connect with credentials held in dbconfig file */
  $conn = new PDO("mysql:host=$server;dbname=$db", $user, $pass);

  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Successfully Connected!";


  /* input sanitization */
  $title = htmlspecialchars($_POST['title']);
  $overview = htmlspecialchars($_POST['overview']);
  $threat_details = htmlspecialchars($_POST['threat_details']);
  $cust_name = htmlspecialchars($_POST['cust_name']);
  $fld_insight = htmlspecialchars($_POST['fld_insight']);
  $competitor = htmlspecialchars($_POST['competitor']);
  $id = htmlspecialchars($_POST['id']);
  $rev_damage = htmlspecialchars($_POST['rev_damage']);
  echo "data sanitized";

  /* submits data IF the submit button is pressed */
  if(isset($_POST['submit'])) {

      /* insert the values passed into the html form into mysql database */
    $sql = "INSERT INTO comp (title, overview, threat_details, cust_name, fld_insight, competitor, id, rev_damage) VALUES ('titleBinded', 'overviewBinded', 'threatDetailsBinded', 'customerNameBinded', 'fieldInsightBinded', 'competitorBinded', 'idBinded', 'revDamageBinded')";


    $sqlPrepared = $conn->prepare($sql);

    $sqlPrepared->bindParam(':titleBinded',$title);
    $sqlPrepared->bindParam(':overviewBinded',$overview); 
    $sqlPrepared->bindParam(':threatDetailsBinded',$threat_details);
    $sqlPrepared->bindParam(':customerNameBinded',$cust_name);
    $sqlPrepared->bindParam(':fieldInsightBinded',$fld_insight);
    $sqlPrepared->bindParam(':competitorBinded',$competitor);
    $sqlPrepared->bindParam(':idBinded',$id);
    $sqlPrepared->bindParam(':revDamageBinded',$rev_damage);


    $sqlPrepared->execute();
    echo "Successfully Inserted!";
  }


  /* commit the transaction */
  if (!$conn->commit()) {
    print("Commit failed\n");
    exit();
  }


  /* close connection */
  $conn->close();

}
catch(PDOException $e) {

  echo "Connection failed: " . $e->getMessage();
}

?>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">


<div class="form-style-2-heading">Create New Competitor</div>


<style>
html,body,h1,h2,h3,h4,h5 {font-family: "Raleway", sans-serif}
</style>
<body class="blue">

</head>


<body>



<!-- Top container -->
<div class="w3-bar w3-top w3-blue w3-large" style="z-index:4">
  <span class="w3-bar-item w3-center">Create New Competitor</span>
</div>

<div class="w3-main" style="margin-left:300px;margin-top:43px;">



<!-- SUBMIT PAGE CONTENT TO DATABASE  -->
<form method="POST">

<label id="info">
<span>Opportunity Title<span class="required">*</span></span>
<br>
<input type="text" class="input-field" name="opti"></label>
<br>
<br>

<label id="info">
<span>Opportunity Overview<span class="required">*</span></span>
<br>
<input type="text" class="input-field" name="opov"></label>
<br>
<br>

<label id="info">
<span>Competitive Threat Details<span class="required">*</span></span>
<br>
<input type="text" class="input-field" name="compdet"></label>
<br>
<br>

<label id="info">
<span>Customer Name<span class="required">*</span></span>
<br>
<input type="text" class="input-field" name="custnme"></label>
<br>
<br>

<label id="info">
<span>Field Insight<span class="required">*</span></span>
<br>
<input type="text" class="input-field" name="fldinsght"></label>
<br>
<br>

<label id="info">
<span>Name of Company Competitor<span class="required">*</span></span>
<br>
<input type="text" class="input-field" name="cmpname"</label>
<br>
<br>

<label id="info">
<span>Enter Deal ID<span class="required">*</span></span>
<br>
<input type="text" class="input-field" name="id"</label>
<br>
<br>

<label id="info">
<span>Approx. Revenue Implications<span class="required">*</span></span>
<br>
<input type="text" class="input-field" name="rev"</label>
<br>
<br>

<b>Technologies/Domains Included</b>
<br>
<br>

<label id="container">Tech1
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Tech2
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Tech3
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Tech4
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Tech5
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Tech6
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Tech7
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>
<br>

<b>Geo/Theatre</b>
<br>
<br>

<label id="container">Geo1
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo2
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo3
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo4
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo5
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo6
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo7
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo8
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo9
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo10
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo11
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo12
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo13
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo14
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo15
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>

<label id="container">Geo16
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<br>
<br>

<label><span>&nbsp;</span><input type="submit" value="Submit" name="submit" /></label>

</form>
</div>
</html>

create.sql

代码语言:javascript
复制
drop database if exists comp;
create schema comp;
use comp;
create table comp (
    titleBinded VARCHAR(50),
    overviewBinded VARCHAR(255),
    threatDetailsBinded VARCHAR(255),
    customerNameBinded VARCHAR(30),
    fieldInsightBinded VARCHAR(255),
    competitorBinded VARCHAR(30),
    idBinded INT,
    revDamageBinded INT,
    PRIMARY KEY (idBinded)
);
create table technologies (
    name VARCHAR(225), 
    PRIMARY KEY (name)
);
create table geography (
    area VARCHAR(255),
    PRIMARY KEY (area)
);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-25 05:04:57

由于create table语句包含列名称,因此必须在查询中使用这些名称:

代码语言:javascript
复制
create table comp (
    titleBinded VARCHAR(50),
    overviewBinded VARCHAR(255),
    threatDetailsBinded VARCHAR(255),
    customerNameBinded VARCHAR(30),
    fieldInsightBinded VARCHAR(255),
    competitorBinded VARCHAR(30),
    idBinded INT,
    revDamageBinded INT,
    PRIMARY KEY (idBinded)
);

查询应如下所示:

代码语言:javascript
复制
INSERT INTO comp (titleBinded, overviewBinded, threatDetailsBinded, customerNameBinded ...

等。

现在,您的绑定语句中有一个: -

代码语言:javascript
复制
$sqlPrepared->bindParam(':titleBinded',$title);
$sqlPrepared->bindParam(':overviewBinded',$overview); 
$sqlPrepared->bindParam(':threatDetailsBinded',$threat_details); ...

等。

因此,您也应该在查询中使用这些语句,不带引号

代码语言:javascript
复制
VALUES (:titleBinded, :overviewBinded, :threatDetailsBinded, :customerNameBinded, :fieldInsightBinded, :competitorBinded, :idBinded, :revDamageBinded)

因为要绑定变量,所以可以去掉使用htmlspecialchars()的行,因为这不会清理数据,准备好的语句中的绑定会处理这些数据。

简而言之,

您必须有一个具有name属性的form元素。在这里,您可以在发布时绑定该变量,以便在查询中使用:

代码语言:javascript
复制
<input type="text" name="this_input_name" />

如果使用POST方法,则可以在绑定中使用该变量,如下所示:

代码语言:javascript
复制
$sqlPrepared->bindParam(':thisBoundName', $_POST['this_input_name']);

然后,您的查询将如下所示:

代码语言:javascript
复制
INSERT INTO `tablename` (`this_column_name`) VALUES (:thisBoundName)

注意如何使用命名输入this_input_name绑定参数:thisBoundName,以及如何将参数用作要插入到列中的值(在创建表时创建)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51507443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档