Send Form Data to mysqli dtbs using php fails (xampp environment) - php

Here is a my html form code: The problem is that i can't figure out how to succesfuly submit the form to mysql dtbs using xampp. (Data aren't sent to dtbs).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My Form</title>
<meta name="description" content="An interactive form">
</head>
<body>
<form action="test.php" method="post" id="Personalinfo">
<label for="fname">Όνομα:</label>
<input type="text" id="fname" name="firstname" placeholder="Όνομα
Πελάτη..">
<input type="submit" value="Submit">
</body>
</html>
and now my php code:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Guests (firstname)
VALUES ('?')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Data is not sent in the mysql dtbs! i've been trying for 2 days solving this but nothing... please help!
Kind regards, Thanos

$sql = "INSERT INTO Guests (firstname) VALUES ('?')";
'?' is to substitute in an integer, string, double or blob value.
You placed the '?', but forgot to prepare it using bind_param. More importantly, you have to pass $firstname value into $stmt->bind_param("s", $firstname);
Updated Code
$firstname = $_POST['firstname'];
$sql = $conn->prepare("INSERT INTO Guests (firstname) VALUES (?)");
$sql->bind_param("s", $firstname);
if ($sql->execute() === TRUE) {
Read
Prepared Statements in MySQLi
how to insert into mysql using Prepared Statement with php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My Form</title>
<meta name="description" content="An interactive form">
</head>
<body>
<form action="test.php" method="post" id="Personalinfo">
<label for="fname">Όνομα:</label>
<input type="text" id="fname" name="firstname" placeholder="Όνομα
Πελάτη..">
<input type="submit" name="submitForm" value="Submit">
</body>
</html>
**test.php file**
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submitForm'])){
$firstname = $_POST['firstname'];
$sql = "INSERT INTO Guests (firstname)
VALUES ('{$firstname}')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}else{
echo "Are you sure you enter a firstname and the name of your html submit is submitForm";
}
$conn->close();
?>

Related

submit button is not working. i have a mysql database and i am trying to make a php form

when i hit the submit button, nothing happens. perhaps the database is not connected. i am trying to make a form using php and html. i am using xampp, i wrote the code in notepad++ and i saved form.php in htdocs. i don't know what is wrong. maybe the names i used for the variables.
this is the html code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="C:\xampp\htdocs\form.php">
Nume de utilizator : <input type="text" name="nume_de_utilizator" placeholder="Enter Your Name" >
Email : <input type="text" name="email" placeholder="Enter Your Email">
Parola: <input type="password" name="parola">
<input type="submit" value="submit" >
</form>
</body>
</html>
this is form.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "autentificare";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$nume_de_utilizator = mysqli_real_escape_string($conn, $_POST['nume_de_utilizator']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$parola = mysqli_real_escape_string($conn, $_POST['parola']);
// Attempt insert query execution
$sql = "INSERT INTO utilizatori (nume_de_utilizator, email, parola) VALUES ('$nume_de_utilizator', '$email', '$parola')";
if(mysqli_query($conn, $sql))
printf("%d Row inserted.\n", mysqli_affected_rows($con));
else
{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);}
// Close connection
mysqli_close($conn);
?>
and this is the "autentificare", the database
my database
Try to see the "online link"
eg: "http://localhost:8080/form.php"
Do a simple echo msg - file to check and after replace
action="C:\xampp\htdocs\form.php" with action=http_link

PHP only adding Numbers to sql in column of VARCHAR

PHP only adding Numbers to MySQL in column of VARCHAR instead of texts
when using query directly in MySQL it works...but if I use $_POST from HTML, IT fails
I don't know the reason how it is getting failed. what is the problem here ?
<?php
$link=mysqli_connect("localhost","root","","home_ac");
if(mysqli_connect_error()) {
die("error in database");
}
$name =$_POST["name"];
$query = "INSERT INTO `test`(`number`, `name`) VALUES (NULL,$name)";
if(mysqli_query($link, $query)){
echo "done";
}
else {
echo "failed";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post">
<input type="text" placeholder="enter a name" name="name">
<input type="submit" value="add">
</form>
</body>
</html>
You need quotes around text
$query = "INSERT INTO `test`(`number`, `name`) VALUES (NULL,'$name')";
Please, think about prepared query. It solve quotes problem and protect from SQL injection.
You have to use PHP Prepared Statements or PHP Data Objects (PDO).
For example, using PDO:
<html>
<head>
<meta charset="utf-8">
<title> Example PDO Insert </title>
</head>
<body>
<form method="post" action="" name="myForm" id="myForm">
<input type="text" placeholder="Enter Your Name" name="name" required="required">
<input type="submit" name="submit" value="add">
</form>
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "home_ac";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ( isset($_POST['submit']) && !empty($_POST['name']) ) {
# code...
$sql = "INSERT INTO test (number,name) VALUES (NULL,'$name')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

Display data from database in HTML page [duplicate]

This question already has an answer here:
Show data pulled from database, based on html form input and display in html page
(1 answer)
Closed 6 years ago.
I would like to display data from my database on page load, but I don't know how and I didn't found any functional way. Inserting works fine.
Here is my HTML code for data inserting:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label for="snapname">Name:</label>
<input type="text" name="snapname">
</p>
<p>
<label for="age">Age:</label>
<input type="text" name="age">
</p>
<input type="submit" value="odeslat">
</form>
</body>
</html>
PHP for connect and insert data to database:
<?php
$servername = "localhost";
$username = "admin";
$password = "***";
$dbname = "db1";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Could not connect to server: " . $conn->connect_error);
}
$first_name = mysqli_real_escape_string($conn, $_POST['snapname']);
$last_name = mysqli_real_escape_string($conn, $_POST['age']);
$Jmeno = $_POST['snapname'];
$Vek = $_POST['age'];
$sql = "INSERT INTO snapy (ID, username, age, date)
VALUES (0, '$Jmeno', '$Vek', CURRENT_TIMESTAMP)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Can someone help me with displaying data in HTML file?
You need to run a SELECT-request. Since you are using MySQLi you want to use something like:
$sql = "SELECT * FROM snapy";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["username"]. " " . $row["age"]. " yo<br>";
}
} else {
echo "0 results";
}
Found here: http://www.w3schools.com/php/php_mysql_select.asp

Insert data to Mysql database Using Bootstrap

I have a simple form I used bootstrap to design the form:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Form</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.css" rel="stylesheet">
<link href="css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="css/animate.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div class="ibox-title">
<h5>Table of All</h5>
</div>
<div class="ibox-content">
<form method="post" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">FirstName</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="fname"></div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Contact Number</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="Contact"></div>
</div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-2">
<button class="btn btn-white" type="submit">Cancel</button>
List
<button class="btn btn-primary" name="insert" type="submit">Save</button>
</div>
</div>
</form>
</div>
<!-- Mainly scripts -->
<script src="js/jquery-2.1.1.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<!-- Custom and plugin javascript -->
<script src="js/inspinia.js"></script>
<script src="js/plugins/pace/pace.min.js"></script>
</body>
</html>
and here is my php code for inserting it to database:
<?php
$servername = "localhost";
$username = "root";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO test (firstname, contact)
VALUES ('John', 'Doe')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
how I take values from fname and contact textbox to add it in php code.
<?php
if(isset($_POST['insert']){
$servername = "localhost";
$username = "root";
$dbname = "test";
$password = "root_password_if_applicable";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else{
$fname=isset($_POST['fname'])?$_POST['fname']:null;
$contact=isset($_POST['Contact'])?$_POST['Contact']:null;
$sql = "INSERT INTO table (firstname, contact) VALUES ('$fname', '$contact')";
if (mysqli_query($conn, $sql)) {
echo $fname." added to table successfully";
} else {
echo "Error: ". mysqli_error($conn);
}
mysqli_close($conn);
}
?>
Change table to the table name
Note: You have set the cancel button as type="submit". You might want to change it to type="reset" if you want it clear form.
PHP:
<?php
if(isset($_POST['inser'])){
$servername = "localhost";
$username = "root";
$password = "yourpassword";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
$fname = $_POST['fname'];
$contact = $_POST['Contact'];
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO test (firstname, contact)
VALUES ($fname, $contact)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
Hope it will work :)

unable to use PHP 5.5 to add data to MySQL 5.7 in webmatrix 3

Please help, this is driving me mad! I have, I thought, a simple registration form that I am trying to send data with PHP to MySQL in Webmatrix. (PHP 5.5 to add data to MySQL 5.7 in webmatrix 3) however, I get the following error in Chrome:
The localhost page isn’t working
localhost is currently unable to handle this request.
500
Here's the PHP:
<?php
$db_user = 'root';
$db_pass = '';
$db_name = 'MySQL10';
$db_host = 'localhost';
$fname = $_POST('fname')
$lname = $_POST('lname')
$email = $_POST('email')
//Create Connection
$conn = new mysqli ( $db_host, $db_user, $db_pass, $db_name);
//Check connection
if ($conn->connect_error) {
die("Connection failed:" . $conn->connect_error);
}
$sql = "INSERT INTO table_1 (fname_1, lname_1, email_1)
VALUES ('$fname', '$lname', '$email')";
$conn->close();
?>
Here's Mark up:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Page</title>
</head>
<body>
<div class="registration">
<form name="test" method="post" action="demo.php" autocomplete="on">
<p>First name:<input type="text" name="fname" value=""></p>
<p>Last name:<input type="text" name="lname" value=""></p>
<p>Email Address:<input type="email" name="email" value=""></p>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
thank you all. I added the [] and the ; and having checked the input using: if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "" . $conn->error; } i was kindly reminded that I had not set a default value for the ID in the database!

Categories