i have two 3 files 1 for the database and the others is for add the data and the index but my adddata.php doesn't catch the value and just return null
connection.php
<?php
session_start();
//general configs
$server = "Myserver";
$usuario = "MyuserName";
$senha = "Mypassword";
$banco = "MYdbname";
//Connection
try {
$pdo = new PDO("mysql:host=$server;dbname=$banco", $usuario, $senha);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
adddata.php
<?php
require("db/connection.php");
if ($_SERVER['REQUEST_METHOD'] == "POST"){
try {
$sql = $pdo->prepare("INSERT INTO clients (id,first_name,email,datinha) VALUES (default, first_name = :namess ,email = :emails , datinha = :datinha)");
$sql->bindParam(":namess",$_POST["nome"]);
$sql->bindParam(":emails",$_POST["mail"]);
$sql->bindParam(":datinha",$_POST["datinha"]);
$sql->execute();
echo "Submitted" . PHP_EOL;
header("Location: /crud/index.php?adicionou");
}catch(PDOException $ex){
echo "Something went wrong" . $ex->getMessage();
}
}
index.php
<div >
<form action="adddata.php" method="post">
<input type="text" name="nome">
<input type="email" name="mail">
<input type="date" name="datinha">
<button type="submit">Send</button>
</form>
</div>
PS: I'm using mysql and php 8.1
CodePudding user response:
Forms are submitted by submit buttons. You don't have one, your regular link is just a link.
Replace <a href="adddata.php">Send</a>
with <button>Send</button>
.
Add an action
attribute to the <form>
if you need to specify a URL to submit to that isn't the same as the current page's.
CodePudding user response:
Try this
<form method="post" action="adddata.php">
<input type="text" name="nome">
<input type="email" name="mail">
<input type="date" name="datinha">
<input type="submit" value="Send">
</form>
The SQL looks a bit off, try this
$sql = $pdo->prepare("INSERT INTO clients (id, first_name, email, datinha) VALUES (default, :namess, :emails, :datinha)");