Hope you guys can help me, I've been trying to figure out what seems to be my error but to no avail, here is my code:
**This is my index.php**
<?php
include('config.php');
if($_POST['submit']=='Borrow')
{
$mysqli->query("INSERT INTO `borrowersprofile`(`lastname`, `firstname`, `middlename`) VALUES(
'".$_POST['lastname']."',
'".$_POST['firstname']."',
'".$_POST['middlename']."',
NOW())");
header("Location: index.php");
exit;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form method="post" action="">
<label for="lastname">Lastname:</label>
<input name="lastname" type="text" id="lastname" style="width:120px;"/>
<label for="firstname">Firstname:</label>
<input name="firstname" type="text" id="firstname" style="width:120px;"/>
<label for="middlename">M.I:</label>
<input name="middlename" type="text" id="middlename" style="width:35px;"/><br />
<input type="submit" name="submit" value="Borrow" />
</form>
</body>
</html>
and here is the config.php
<?php
$db_username = 'root';
$db_password = '';
$db_name = 'bsystem';
$db_host = 'localhost';
$mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
?>
I've been trying to check and even tried to remake the database but its still not adding data, btw, this is a school project so security doesn't really matter, hope you guys can help me out! Thanks.
As pointed out by #user328
1) Assuming the ID or primary key of your table is AUTO_INCREMENT, therefore you do not need to mention ID column in your query. Sql inserts it automatically for you.
2) You need to mention the column name where to save the value returned by now().
if($_POST['submit']=='Borrow')
{
$mysqli->query("INSERT INTO `borrowersprofile`(`lastname`, `firstname`, `middlename`, `DATE`) VALUES(
'".$_POST['lastname']."',
'".$_POST['firstname']."',
'".$_POST['middlename']."',
NOW())");
header("Location: index.php");
exit;
}
Try to create this table in your database and then use the script below.
sql table:
CREATE TABLE `borrowersprofile` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`lastname` VARCHAR(100) NOT NULL,
`firstname` VARCHAR(100) NOT NULL,
`middlename` VARCHAR(100) NOT NULL,
`datetime` DATETIME NOT NULL
)ENGINE=InnoDB;
index.php
<?php
require_once('./config.php');
if(!empty($_POST['lastname']) AND !empty($_POST['firstname']) AND !empty($_POST['middlename'])){
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
if ($stmtint = $mysqli->prepare("INSERT INTO `borrowersprofile`( `lastname`, `firstname`, `middlename`, `datetime`) VALUES(?, ?, ?, NOW())") {
$stmtint->bind_param("sss", $lastname, $firstname, $middlename);
if ($stmtint->execute()) {
$stmtint->close();
echo "User saved successfully!";
}else{
die("Error Message:".$mysqli->error);
}
header("Location: index.php");
exit;
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form method="post" action="">
<label for="lastname">Lastname:</label>
<input name="lastname" type="text" id="lastname" style="width:120px;"/>
<label for="firstname">Firstname:</label>
<input name="firstname" type="text" id="firstname" style="width:120px;"/>
<label for="middlename">M.I:</label>
<input name="middlename" type="text" id="middlename" style="width:35px;"/><br />
<input type="submit" name="submit" value="borrow" />
</form>
</body>
config.php
<?php
$db_username = 'root';
$db_password = '';
$db_name = 'bsystem';
$db_host = 'localhost';
$mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
?>
Related
I am trying to create PHP form data insert in SQL but getting error.
Even when I write code same to same but I'm still getting an error.
<?php
$un = $_POST['uname'];
$em = $_POST['email1'];
//with or what out these bellow variables
$host = "localhost";
$username = "admin";
$password = "admin";
$database = "test1";
$db = mysqli_connect('$host','$username','$password','$database');
$query = "INSERT INTO users ('username','password') VALUES ('$un','$em')";
$rs = mysqli_query($db,$query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Registration</title>
</head>
<body>
<form action="server1.php" method="post">
<label>Name</label>
<input type="text" name="uname" required="required">
<label>Email</label>
<input type="email" name="email1" required="required">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
The error was "" just inverted comma's, now its works perfectly.
<?php
$un = $_POST['uname'];
$em = $_POST['email1'];
$host = "localhost";
$username = "admin";
$password = "admin";
$database = "test1";
$con = mysqli_connect ("$host","$username","$password","$database");
$query = "insert into users (username,email) values ('$un','$em')";
$run = mysqli_query ($con,$query);
if ($run=TRUE){
echo 'Data Submitted Successfuly';
}
else {
echo 'Error';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Registration</title>
</head>
<body>
<form action="server1.php" method="post">
<label>Name</label>
<input type="text" name="uname" required="required">
<label>Email</label>
<input type="email" name="email1" required="required">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
You can try this way.
$db = mysqli_connect($host, $username, $password) or die ('Unable to connect');
mysqli_select_db($db, $database) or die('Unable to select DB');
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!
I have created a form in html and I would like that the dataentered in the form is sent to a mysql database in XAMMP. I created the database, the table and the connectivity.php file that manages the connection to the database and data entry, but when I try I get the following error:
"Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\example\connectivity.php:8 Stack trace: #0 {main} thrown in C:\xampp\htdocs\example\connectivity.php on line 8"
I post all the code that I wrote. Does anyone know where I'm wrong?
here is the form index.html
<!DOCTYPE HTML>
<html>
<head>
<title>Contact Us</title>
<link rel="stylesheet" type="text/css" href="style.css">
<?php include("connectivity.php"); ?>
</head>
<body>
<div id="contact">
<h3>Contact Us For Any Query</h3>
<form method="POST" action="connectivity.php">
Name
<br>
<input type="text" name="name">
<br> Email
<br>
<input type="text" name="email">
<br> Message
<br>
<textarea rows="10" cols="50" maxlength="100" name="message"></textarea>
<br>
<input type="submit" value="Send Message">
</form>
</div>
</body>
</html>
Then the code used to define database and the table:
CREATE DATABASE practice;
USE practice;
CREATE TABLE contact
(
contactID INT(9) NOT NULL auto_increment,
contactName VARCHAR(40) NOT NULL,
contactEmail VARCHAR(40) NOT NULL,
message VARCHAR(250) NOT NULL,
PRIMARY KEY(contactID)
);
Finally the connectivity.php file:
<?php
//connecting to the database
define('DB_HOST', 'localhost');
define('DB_NAME', 'practice');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
//inserting Record to the database
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$query = "INSERT INTO contact(contactName,contactEmail,message)VALUES('$name','$email','$message')";
$result = mysql_query($query);
if($result)
{
echo "Successfully updated database";
}
else
{
die('Error: '.mysql_error($con));
}
mysql_close($con);
?>
P.S: I installed the latest version of XAMMP (5.6.15)
$con=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,##TABLE NAME##) or die("Failed to connect to MySQL: " . mysql_error());
$query = "INSERT INTO contact(contactName,contactEmail,message)VALUES('$name','$email','$message')";
$result = mysqli_query($con,$query);
If you are using one of the latest version of xampp therefore you have to use PDO or MySQLi .
Your have to change your codes to something like this.
Your connectivity page
<?php
$db = new PDO('mysql:host=localhost;dbname=practice;charset=utf8',
'root',
'',
array(PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
?>
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$stmt = $db->prepare("INSERT INTO `contact` (contactName,contactEmail,message)
VALUES (:name, :email, :message)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':message', $message);
$stmt->execute();
echo 'added';
}
?>
Your home page
<!DOCTYPE HTML>
<html>
<head>
<title>Contact Us</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="contact">
<h3>Contact Us For Any Query</h3>
<form method="POST" action="connectivity.php">
Name
<br>
<input type="text" name="name">
<br> Email
<br>
<input type="text" name="email">
<br> Message
<br>
<textarea rows="10" cols="50" maxlength="100" name="message"></textarea>
<br>
<input type="submit" value="Send Message">
</form>
</div>
</body>
</html>
Hope this helps
Firts you see your phpinfo:
<?php
phpinfo();
?>
Then see in here , php_mysql is enabled or disabled?
If there not php_mysql, change php.ini file:
Uncomment extension=php_mysql.dll
I'm new to PHP and still trying to get my head round it. this form says that the data has been sent to the database but when I look the database is empty, no errors are showing up? is there a problem with my code.
Note: I understand that this form is not protected from SQL Injection.
HTML
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Page Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="main">
<h2>PHP Page 3 Form</h2><hr/>
<span id="error">
</span>
<form action="page4_insertdata.php" method="post">
<label>Company Name :<span>*</span></label><br />
<input name="company_name" type="text" placeholder="Joes Cleaner" required>
<br />
<label>Ref :<span>*</span></label><br />
<input name="ref" type="text" placeholder="H123" required>
<br />
<label>Website :<span>*</span></label><br />
<input name="website" type="text" placeholder="www.google.com" required>
<br />
<label>Email :<span>*</span></label><br />
<input name="email" type="email" placeholder="Joescleaners#gmail.com" required>
<br />
<label>Telephone :<span>*</span></label><br />
<input name="tel" type="text" placeholder="07123456789" required>
<br />
<label>Message :<span>*</span></label><br />
<input name="message" id="message" type="text" size="500" required>
<br />
<input type="reset" value="Reset" />
<input name="submit" type="submit" value="Submit" />
</form>
</div>
</div>
</body>
</html>
PHP
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>PHP Multi Page Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="main">
<h2>PHP Multi Page Form</h2><hr/>
<?php
$servername = "localhost";
$db_database = 'form';
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "DB Connected successfully. ";
$company_name = $_POST['company_name'];
$ref = $_POST['ref'];
$website = $_POST['website'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$sql = "INSERT INTO detail (company_name,ref,website,email,tel,message)
VALUES ('$company_name','$ref','$website','$email','$tel','$message')";
if($sql){
echo " Database Sent.";
}
else {
echo "ERROR to insert into database";
};
?>
</div>
</div>
</body>
</html>
Change the following code:
if($sql){
echo " Database Sent.";
}
else {
echo "ERROR to insert into database";
};
To:
$result = $conn->query($sql);
if($result){
echo " Database Sent.";
}
else {
echo "ERROR to insert into database";
};
This way you are actually performing the query and checking on failure of query...
To make your query a bit safer, try the following:
$sql = "
INSERT INTO detail (
company_name,
ref,
website,
email,
tel,
message
)
VALUES (
'" . mysqli_real_escape_string($company_name) . "',
'" . mysqli_real_escape_string($ref) . "',
'" . mysqli_real_escape_string($website) . "',
'" . mysqli_real_escape_string($email) . "',
'" . mysqli_real_escape_string($tel) . "',
'" . mysqli_real_escape_string($message) . "'
)";
Better yet, use binding of params by replace the $sql instantiation and query execution ($conn->query()) with the following:
$stmt = $conn->prepare("INSERT INTO detail (company_name,ref,website,email,tel,message) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param('ssssss', $company_name, $ref, $website, $email, $tel, $message);
$stmt->execute();
You can read up on binding parameters with mysqli by visiting PHP: mysqli_stmt::bind_param - Manual
Complete code:
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>PHP Multi Page Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="main">
<h2>PHP Multi Page Form</h2><hr/>
<?php
$servername = "localhost";
$db_database = 'form';
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $db_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "DB Connected successfully. ";
$stmt = $conn->prepare("INSERT INTO detail (company_name,ref,website,email,tel,message) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param('ssssss',
$_REQUEST['company_name'],
$_REQUEST['ref'],
$_REQUEST['website'],
$_REQUEST['email'],
$_REQUEST['tel'],
$_REQUEST['message']
);
if($stmt->execute()) {
echo " Database Sent.";
} else {
echo "ERROR to insert into database: " . $stmt->error;
};
?>
</div>
</div>
</body>
</html>
You arent actually sending a query, you are setting the variable $sql = "INSERT....."
which is always true.
you need to do:
$result = $mysqli->query($sql);
if ($result......)
I'm trying to insert somethig to db and after i write this code i get just a blank page in php. Cam you help me ? if i delete all // i get blank page if it is as now i get imput containers
<?php
function create()
{
if(isset($_POST["submit"]))
{
$db = new mysqli('localhost', 'root', 'root', 'idoctor_db');
$username = $db->real_escape_string($_POST['username']);
$password = $db->real_escape_string($_POST['password']);
$password_conf = $db->real_escape_string($_POST['password_conf']);
$nick = $db->real_escape_string($_POST['nick']);
//create_new_user($username, $password, $password_conf, $nick);
//$db->query
//('
//INSERT INTO `idoctor_db`.`users` (`ID` ,`Login` ,`Password` ,`Name` ,`Level`)
//VALUES ('5 ' , 'kev5', 'roo5', 'kevkev5', ' 3 ' );
//');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="css/login.css" />
</head>
<body>
<div class="container">
<form action="<?php create(); ?>" method="POST">
<input type="text" name="username" placeholder="Username..." />
<input type="password" name="password" placeholder="Password" />
<input type="password" name="password_conf" placeholder="Confirm password" />
<input type="text" name="nick" placeholder="Nick" />
<input type="submit" value="Create" name="submit"/>
</form>
</div>
</body>
</html>
If there are no errors in running this query, you will see a blank page.
All the work is happening in the background, look at your mysql table to confirm if the insert worked.
If you want to check the outcome of the query... just do the following
$outcome = $db->query
('
INSERT INTO `idoctor_db`.`users` (
`ID` ,
`Login` ,
`Password` ,
`Name` ,
`Level`
)
VALUES
(' 4 ', ' kev4 ', ' root ', ' kevkev4 ', ' 3 ');
');
if($outcome) {
echo 'success';
} else {
echo 'failed';
}
After your edit... I see another problem in your code:
<form action="<?php create(); ?>" method="POST">
<!-- ^ This is not how you submit form data to php...
action is supposed to be a URL.. however if left blank
it will take you to the same page
Try this instead:
<?php
if(isset($_POST["submit"]))
{
$db = new mysqli('localhost', 'root', 'root', 'idoctor_db');
$username = $db->real_escape_string($_POST['username']);
$password = $db->real_escape_string($_POST['password']);
$password_conf = $db->real_escape_string($_POST['password_conf']);
$nick = $db->real_escape_string($_POST['nick']);
//create_new_user($username, $password, $password_conf, $nick);
//$db->query
//('
//INSERT INTO `idoctor_db`.`users` (`ID` ,`Login` ,`Password` ,`Name` ,`Level`)
//VALUES ('5 ' , 'kev5', 'roo5', 'kevkev5', ' 3 ' );
//');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="css/login.css" />
</head>
<body>
<div class="container">
<form method="POST">
<input type="text" name="username" placeholder="Username..." />
<input type="password" name="password" placeholder="Password" />
<input type="password" name="password_conf" placeholder="Confirm password" />
<input type="text" name="nick" placeholder="Nick" />
<input type="submit" value="Create" name="submit"/>
</form>
</div>
</body>
</html>