this is the php code and the one below is the HTML file. i keep getting the
unidentified index error and i dont know why. please help!
$firstname = $_POST["fname"];
$lastname = $_POST['lname'];
$sql = "INSERT INTO user(fname,lname)
VALUES ('$firstname','$lastname')";
<form action="connect.php" method="post">
First name: <input type="password" name="fname">
Last name: <input type="text" name="lname"><br><br>
<input type="submit" value="Sign up!" id="btnsignup" />
</form>
Use the isset() function to evaluate if the key is set.
example:
$firstname = isset($_POST["fname"]) ? $_POST["fname"] : '';
$lastname = isset($_POST['lname']) ? $_POST['lname'] : '';
Related
I have a form that I am using to update a mysql database using PHP and html. I set my data type in mysql as double (3,1). The problem is when I submit the data to the database I get a 0 where I had put nothing on the form input fields; What I want is these columns to be null.
I don't know how I can achieve this.
$sName = mysql_real_escape_string($_REQUEST['sName']);
$datetime1 = mysql_real_escape_string( $_REQUEST['datetime1']);
$rain = mysql_real_escape_string( $_REQUEST['rain']);
$max = mysql_real_escape_string( $_REQUEST['max']);
$min = mysql_real_escape_string( $_REQUEST['min']);
$sql = "INSERT INTO rain(sName, datetime, rain, min, max) VALUES ('$sName', '$datetime1', '$rain','$max','$min')";
part of my html code
<p>
<label for="max">Maximum Temp:</label>
<input type="number" min="10.0" max="55.0" name="max" id="max" value="">
</p>
<p>
<label for="min">Minimum Temp:</label>
<input type="number" min="-3.0" max="40.0" name="min" id="min"value="">
</p>
<input type="submit" value="Submit">
<input type="reset" value="Clear">
If I understand, you can use this :
<form method="post" action="traitement.php">
<label for="data">Data :</label>
<input type="text" name="data">
<input type="submit" value="Send">
</form>
PHP :
if(empty($_POST['data'])) {
//insert null in your database
}
else {
//insert data
}
how to create batch insert
view
two input number check and tow input price check and tow input date check
<input type='text' name="number_check[]">
<input type='text' name="price_check[]">
<input type='text' name="date_check[]">
<input type='text' name="number_check[]">
<input type='text' name="price_check[]">
<input type='text' name="date_check[]">
controller
I don't know what to write ?????
Yii::$app->db->createCommand()->batchInsert('sale_check',[
'number_check',
'price_check',
'date_check',
'user_id',
'sale_id',
],$data)->execute() ;
Looking to you sample where you have just two set of same fields you shoul populate the $data array and perform execute() of db command
$post = Yii::$app->request->post();
for ($i=0; $i<2; $i++){
$data[$i][0] = $post[number_check][$i];
$data[$i][1] = $post[price_check][$i];
$data[$i][2] = $post[date_check][$i];
$data[$i][3] = Your_value_for_user_id;
$data[$i][4] = Your_value_for_sale;
}
Yii::$app->db->createCommand()->batchInsert('sale_check',[
'number_check',
'price_check',
'date_check',
],
$data
)->execute();
i want to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page!!
i've tried to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page but doesnt work ! and when i refresh the page it store the last data that i've tried before!!
this is index.php :
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
this is the users.php file :
<?php
class Users extends DB {
function insertUser($name,$age,$email){
$query = "INSERT INTO users (name, age, email )
VALUES ('$name', '$age', '$email')";
$res=$this->insert($query);
//return $this->select("SELECT * FROM `users`");
}
}
i've tried to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page but doesnt work ! and when i refresh the page it store the last data that i've tried before!!
Please help!!
it show this error and store the data at the same time !
Notice: Undefined index: name in C:\xampp\htdocs\gestion de paie\index.php on line 10
Notice: Undefined index: age in C:\xampp\htdocs\gestion de paie\index.php on line 10
Notice: Undefined index: email in C:\xampp\htdocs\gestion de paie\index.php on line 10
You can put a condition before you create user, in index file
index.php
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
if(isset($_POST['name']) && isset($_POST['age']) && isset($_POST['email']){
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
}
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
Also don't put user inputs inside a query without sanitize, i suggest always to controll user inputs before you enter in you sql query:
users.php :
<?php
class Users extends DB {
function insertUser($name,$age,$email){
// Sanitize your data always don't trust user
$name = mysql_real_escape_string($name);
$age = mysql_real_escape_string($age);
$email = mysql_real_escape_string($email);
$query = "INSERT INTO users (name, age, email )
VALUES ('$name', '$age', '$email')";
$res=$this->insert($query); //return $this->select("SELECT * FROM `users`");
Hope to be the needed question :)
Instead of isset, you should use !empty (look here), as isset doesn't work for arrays. isset($_POST["anything"]) always returns true, while !empty($_POST["anything"]) only returns true if $_POST["anything"] exists.
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
if (!empty($_POST['name']) && !empty($_POST['age']) && !empty($_POST['email']) {
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
}
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
Also, you should use Prepared statements.
I'm a beginner to ExpressJS, I want to be able to add records to the "site" table. However, when I run the following code, it says:
Error: ER_BAD_FIELD_ERROR: Unknown column 'BeastMode' in 'field list'.
"BeastMode" is an entry I made to the shortName field.
A little context: I'm not supposed to use ORM. I have to use raw sql queries to add to MYSQL database.I'm using 'mysql'package for Nodejs to connect to the database.
var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES "+
"("+
req.body.shortName+", "+
req.body.addressLine1+", "+
req.body.addressLine2+", "+
req.body.city+", "+
req.body.state+", " +
req.body.zipcode+", " +
req.body.phoneNumber+" );"
console.log(req.body);
dbconnector.query(squery,function(err,rows,fields){
if(!err){
res.send("Record Added Successfully: "+req.body);
}else{
res.send("Error: "+ err);
}
});
});
Also, here is my dbconnect.js file:
var mysql = require('mysql');
dbconnect = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database:"rsacs"
});
module.exports = dbconnect
Here is my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<% include head %>
</head>
<body class="container">
<header>
<% include header %>
</header>
<main>
<div>
<h1><%=title%></h1>
<form method="post" action="/site/create" >
<div class="form-group">
<label for="shortName">Shortname</label>
<input type="text" class="form-control" placeholder="Shortname" name="shortName"><br>
<label for="Address Line 1"> Address Line 1:</label>
<input type="text" class="form-control" placeholder="Address Line 1" name="addressLine1"><br>
<label for="Address Line 2"> Address Line 2:</label>
<input type="text" class="form-control" placeholder="Address Line 2" name="addressLine2"><br>
<label for="City">City:</label>
<input type="text" class="form-control" placeholder="City" name="city"><br>
<label for="State">State:</label>
<input type="text" class="form-control" placeholder="State" name="state"><br>
<label for="Zipcode">Zipcode:</label>
<input type="text" class="form-control" placeholder="Zipcode" name="zipcode"><br>
<label for="PhoneNumber">Phone Number:</label>
<input type="text" class="form-control" placeholder="PhoneNumber" name="phoneNumber"><br>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</main>
<footer>
<% include footer %>
</footer>
</body>
</html>
Here is my Site table structure
To echo #AnshumanJaiswal's solution, you're probably encountering an escape character problem.
The solution I'm going to propose, though, is different. The mysql nodejs driver supports prepared queries. As such, the most robust way to sort your query is:
var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES (?,?,?,?,?,?,?);
var objs = [req.body.shortName,req.body.addressLine1,req.body.addressLine2,req.body.city,req.body.state,req.body.zipcode,req.body.phoneNumber]
sql = mysql.format(squery, objs);
// now you have a properly-escaped SQL query which you can execute as usual:
connection.query(squery, objs, function (error, results, fields) {if (error) throw error;});
Let me know if this doesn't sort your problem.
the values are string and you are not passing them as string.
There are two possible ways:
Solution 1.
add `` to your string values like:
var squery = "INSERT INTO SITE (shortName,addressLine1,addressLine2,city,state,zipcode,phoneNumber) VALUES "+
"('"+
req.body.shortName+"', '"+
req.body.addressLine1+"', '"+
req.body.addressLine2+"', '"+
req.body.city+"', '"+
req.body.state+"', '" +
req.body.zipcode+"', " +
req.body.phoneNumber+" );"
...
Solution 2.
make an object from body data as:
var data = {
shortName: req.body.shortName,
addressLine1: req.body.addressLine1,
addressLine1: req.body.addressLine2,
city: req.body.city,
state: req.body.state,
zipcode: req.body.zipcode,
phoneNumber: req.body.phoneNumber
};
var squery = "INSERT INTO SITE SET ?";
dbconnector.query(squery, data, function(err,rows,fields){
if(!err){
console.log(rows);
res.send("Record Added Successfully.");
}else{
res.send("Error: "+ err);
}
});
I am learning PHP.using XAMPPlocal server to test. Need help below
when I enter the details and click register in the form, I just see a blank page with the action.php link.
Code for action.php is below:
include 'config.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST['register'])){
require 'config.php';
$first_name = !empty($_POST['fname']) ? trim($_POST['fname']) : null;
$last_name = !empty($_POST['lname']) ? trim($_POST['lname']) : null;
$user_name = !empty($_POST['uname']) ? trim($_POST['uname']) : null;
$pass = !empty($_POST['upass']) ? trim($_POST['upass']) : null;
$user_email = !empty($_POST['umail']) ? trim($_POST['umail']) : null;
$sql = "INSERT INTO ams_users (id, firstname, lastname, username, password, email)
VALUES(?,?,?,?,?)";
$stmt = $pdo->prepare($sql);
$result = $stmt->execute(0,$first_name, $last_name, $user_name, $pass, $user_email);
if($result){
echo 'Thank you for registering';
}else{
echo 'sorry';
}
}enter code here
code for register.php form below:
<div class="reg-form">
<h1>Register</h1>
<form action="action.php" method="POST">
<p><label>First Name* :</label>
<input id="firstname" type="text" name="fname" /></p>
<p><label>Last Name* :</label>
<input id="lasttname" type="text" name="lname" /></p>
<p><label>User Name* : </label>
<input id="username" type="text" name="uname" /></p>
<p><label>Password: </label>
<input id="password" type="password" name="upass" /></p>
<p><label>E-Mail: </label>
<input id="e-mail" type="e-mail" name="umail"/></p>
<input type="submit" name="submit" value="register" />
</form>
</div>
MySQL table has 5 entries: id (autoincrement, firstname, lastname,username,password, and email.
please help where I am going wrong.
execute() takes an array of parameters not directly like you did.
You have less question marks then parameter
If you make id AUTOINCREMENTED then you can omit it from your query
So it becomes:
INSERT INTO ams_users (firstname, lastname, username, password, email)
VALUES(?,?,?,?,?)
Then you execute:
$result = $stmt->execute(array($first_name,
$last_name,
$user_name,
$pass,
$user_email)
);
Or:
INSERT INTO ams_users (id,firstname, lastname, username, password, email)
VALUES('',?,?,?,?,?)
The problem is you should have gotten the error, so make sure it is turned on:
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);