在php上插入不起作用

时间:2015-06-03 05:33:47

标签: php mysql pdo sql-insert

场景:我正在尝试使用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

1 个答案:

答案 0 :(得分:0)

对于绑定变量:

替换为

 $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);
}