【问题标题】:Insert on php not working在php上插入不起作用
【发布时间】:2015-08-17 03:50:30
【问题描述】:

场景:我正在尝试使用 php 从表单中添加一些字段到 sql 表中,但它不起作用,我不知道为什么,我已经搜索了整个网络和论坛,但似乎没有正在工作,有人可以帮帮我吗?

表格:

<form id="myform" class="fs-form fs-form-full" method="post" action="core/insert-contato.php" autocomplete="off">
<ol class="fs-fields">
    <li>
        <label class="fs-field-label fs-anim-upper" for="q1">Nome</label>
        <input class="fs-anim-lower" id="q1" name="name" type="text" placeholder="Insira seu nome aqui" required/>
    </li>
    <li>
        <label class="fs-field-label fs-anim-upper" for="q2" data-info="Preencha com atenção, senão não poderei te responder!">Endereço de email</label>
        <input class="fs-anim-lower" id="q2" name="email" type="email" placeholder="seuemail@email.com" required/>
    </li>
    <li data-input-trigger>
        <label class="fs-field-label fs-anim-upper" style="color: #fff"for="q3" data-info="Isso me ajuda muito na organização dos pedidos, acredite">Por qual tipo de serviço você se interessa?</label>
        <div class="fs-radio-group fs-radio-custom clearfix fs-anim-lower">
            <span><input id="q3b" name="service" type="radio" value="1"/><label for="q3b" class="radio-conversion" style="color: #fff;">Website</label></span>
            <span><input id="q3c" name="service" type="radio" value="2"/><label for="q3c" class="radio-social" style="color: #fff;">SEO</label></span>
            <span><input id="q3a" name="service" type="radio" value="3"/><label for="q3a" style="color: #fff;" class="radio-mobile">Design</label></span>
        </div>
    </li>
    <li>
        <label class="fs-field-label fs-anim-upper" for="q4">Descreva de maneira breve o que você deseja:</label>
        <textarea class="fs-anim-lower" id="q4" name="description" placeholder="Uma breve descrição" style="background-color:#101010; height: 30%"></textarea>
    </li>
    <li>
        <label class="fs-field-label fs-anim-upper" for="q5">Orçamento disponível:</label>
        <input class="fs-mark fs-anim-lower" id="q5" name="orcamento" type="number" step="100" min="100"/>
    </li>
</ol><!-- /fs-fields -->
<button class="fs-submit" type="submit">Enviar</button>

insert-contato.php
<?php 
    include_once 'dbconect.php';    

    $name = $_POST['name'];
    $email = $_POST['email'];
    $description = $_POST['description'];
    $service = $_POST['service'];
    $orcamento = $_POST['orcamento'];

    switch ($service){
        case '1':
            $service = 'website';
            break();
        case '2':
            $service = 'seo';
            break();
        case '3':
            $service = 'design';
            break();
        }


    $stmt = $dbh->prepare("INSERT INTO Contact (name, email, description, service, orcamento) VALUES ( ?, ?, ?, ?, ?)");
        try{
            $stmt->bindParam(1, $name);
            $stmt->bindParam(2, $email);
            $stmt->bindParam(3, $description);
            $stmt->bindParam(4, $service);
            $stmt->bindParam(5, $orcamento);

            $stmt->execute();
        }
        catch(PDOException $e){
            echo $e->getMessage();
        }
    header('Location: ../');    
?>

dbconect.php

  <?php
         try {
            $dbh = new PDO('mysql:host=127.0.0.1;dbname=info', 'root', 'pass');
            }
         catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
            }
     ?>

表:

id int pk
name varchar(50
email varchar(50)
description varchar(255)
service varchar(11)
orcamento smallint
data datetime

项目目录:

contato
├── core
│   ├── dbconect.php
│   ├──insert-contato.php
│   └──select-contato.php
├── README.m
└── index.php

【问题讨论】:

  • 你能解释一下, but it's not working 是什么意思吗?您是否收到错误消息,如果是,请发布消息!还是没有插入任何东西?或或..
  • 您遇到的错误是什么?
  • '搜索了整个网络' - 整个网络?真的吗?
  • 没有错误返回一个空白页
  • break() 不是函数。只是break

标签: php mysql pdo sql-insert


【解决方案1】:

对于绑定变量:

替换为

 $stmt = $dbh->prepare("INSERT INTO Contact (name, email, description, service, orcamento) VALUES (:name,:email,:description,:service,:orcamento)");
try{

      $stmt->bindParam(:name, $name, PDO::PARAM_STR);
      $stmt->bindParam(:email, $email, PDO::PARAM_STR);
      $stmt->bindParam(:description, $description, PDO::PARAM_STR);
      $stmt->bindParam(:service, $service, PDO::PARAM_STR);
      $stmt->bindParam(:orcamento, $orcamento, PDO::PARAM_STR);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多