Am I Connecting to Database Correctly?

Asked

Viewed 44 times

0

hello I have this table named Payment:

fname
email
adr
city
cname
ccnum
expmonth
expyear
cvv
id

and I have this payment form if the person wants to help my project!

<form id="my-form"
     action="envia.php"
       method="POST">        <label for="fname"><i class="fa fa-user"></i> Nome</label>
               <input type="text" id="fname" name="firstname" placeholder="seu nome e sobrenome">
              <label for="email"><i class="fa fa-envelope"></i> Email</label>
                 <input type="text" id="email" name="email" placeholder="[email protected]">
                 <label for="adr"><i class="fa fa-address-card-o"></i> Address</label>
             <input type="text" id="adr" name="address" placeholder="542 W. 15th Street">
               <label for="city"><i class="fa fa-institution"></i> City</label>
            <input type="text" id="city" name="city" placeholder="New York">

            
    <label for="cname">Name on Card</label>
             <input type="text" id="cname" name="cardname" placeholder="John More Doe">
             <label for="ccnum">Numero do Cartão:</label>
               <input type="number" id="ccnum" name="cardnumber" placeholder="1111-2222-3333-4444" >
               <label for="expmonth">Mês de Expiração:</label>
            <input type="number" id="expmonth" name="expmonth" placeholder="Setembro">
      <label for="expyear">Exp Year</label>
                     <input type="number" id="expyear" name="expyear" placeholder="2018" pattern="[0-9]+$">
    <label for="cvv">CVV</label>
                     <input type="number" id="cvv" name="cvv" placeholder="352">
     <button id="my-form-button" >Submit</button>

in send.php I have this code:

<?php
session_start();
include_once("conexao.php");
$firstname = $_POST["firstname"];
$email =$_POST["email"];
 $address = $_POST["address"];
$city = $_POST["city"];
$cardname = $_POST["cardname"];
$cardnumber = $_POST["cardnumber"];
$expmonth = $_POST["expmonth"];
$expyear = $_POST["expyear"];
$cvv = $_POST["cvv"];
$result_usuario = "INSERT INTO Payment (fname, email, adr, city, cname, ccnum , expmonth , expyear , cvv , id) VALUES ('$firstname', '$email', '$firstname','$address', '$city','$cardnumber','$cardname','$expmonth','$expyear','$cvv',NOW())";
$resultado_usuario = mysqli_query($conn, $result_usuario);
//sucesso
if(mysqli_insert_id($conn)){
  $_SESSION['msg'] = "<p style='color:green;'>Usuário cadastrado com sucesso</p>";
  header("Location: cadastrado.php");
//erro
}else{
  $_SESSION['msg'] = "<p style='color:red;'>Usuário não foi cadastrado com sucesso</p>";
  header("Location: error.php");
}

and in connection.php:

 <?php $servidor = "localhost"; $usuario = "meu_usuario"; $senha =
 "minha_senha"; $dbname = "nome_do_meu_banco"; $conn = mysqli_connect($servidor, $usuario, $senha,
 $dbname);

only that he’s not recording anything that’s wrong?

  • Give an echo in the $result_usuario variable and try to run the query in the database

  • did debug? identified an error? with @Felipepachecopaulucio said, tried to pick up the query and see if it is ok?

1 answer

0


Here is the file of your form with some adjustments, change the button to type="Submit" and make sure that the extension of your file is with . php

    <form id="my-form" action="envia.php" method="POST">
        <label for="fname"><i class="fa fa-user"></i> Nome</label>
        <input type="text" id="fname" name="firstname" placeholder="seu nome e sobrenome">
        <label for="email"><i class="fa fa-envelope"></i> Email</label>
        <input type="text" id="email" name="email" placeholder="[email protected]">
        <label for="adr"><i class="fa fa-address-card-o"></i> Address</label>
        <input type="text" id="adr" name="address" placeholder="542 W. 15th Street">
        <label for="city"><i class="fa fa-institution"></i> City</label>
        <input type="text" id="city" name="city" placeholder="New York">


        <label for="cname">Name on Card</label>
        <input type="text" id="cname" name="cardname" placeholder="John More Doe">
        <label for="ccnum">Numero do Cartão:</label>
        <input type="number" id="ccnum" name="cardnumber" placeholder="1111-2222-3333-4444">
        <label for="expmonth">Mês de Expiração:</label>
        <input type="number" id="expmonth" name="expmonth" placeholder="Setembro">
        <label for="expear">Exp Year</label>
        <input type="number" id="expyear" name="expyear" placeholder="2018" pattern="[0-9]+$">
        <label for="cvv">CVV</label>
        <input type="number" id="cvv" name="cvv" placeholder="352">
        <button id="my-form-button" type="submit">Submit</button>
    </form>

I changed your.php connection to Pdo because mysqli is deprecated

<?php 

$usuario = "root"; 
$senha =""; 

try {
    $conn = new PDO('mysql:host=localhost;dbname=namebanco', $usuario, $senha);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  } catch(PDOException $e) {
      echo 'ERROR: ' . $e->getMessage();
  }

And finally your send.php file was as follows

<?php
session_start();
include_once("conexao.php");
//Certifique os tipos dos seus parametros caso no banco esteja como integer o campo converta com intval() ou strval() ex: strval($_POST["firstname"]) ou intval($_POST["cvv"])

$firstname  = $_POST["firstname"];
$email      = $_POST["email"];
$address    = $_POST["address"];
$city       = $_POST["city"];
$cardname   = $_POST["cardname"];
$cardnumber = $_POST["cardnumber"];
$expmonth   = $_POST["expmonth"];
$expyear    = $_POST["expyear"];
$cvv        = $_POST["cvv"];

$query = "INSERT INTO Payment (fname, email, adr, city, cname, ccnum , expmonth , expyear , cvv) VALUES (?,?,?,?,?,?,?,?,?)";

$stmt = $conn->prepare($query);
$stmt->bindParam(1, $firstname);
$stmt->bindParam(2, $email);
$stmt->bindParam(3, $address);
$stmt->bindParam(4, $city);
$stmt->bindParam(5, $cardname);
$stmt->bindParam(6, $cardnumber);
$stmt->bindParam(7, $expmonth);
$stmt->bindParam(8, $expyear);
$stmt->bindParam(9, $cvv);

//executando o insert
$stmt->execute();
//pegando id inserido
$temp = $conn->lastInsertId();;

if(!empty($temp)){
  $_SESSION['msg'] = "<p style='color:green;'>Usuário cadastrado com sucesso</p>";
  header("Location: cadastrado.php");
}else{
  $_SESSION['msg'] = "<p style='color:red;'>Usuário não foi cadastrado com sucesso</p>";
  header("Location: error.php");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.