Trouble adding users to database using PHP - php

I am trying to store values to my database. This is the code I use to insert the code from my website:
<?php
$ident = $_POST['ident'];
$dato = $_POST['dato'];
$kundensnavn = $_POST['kundensnavn'];
$gsm = $_POST['gsm'];
$fodselsdato = $_POST['fodselsdato'];
$prisplan = $_POST['prisplan'];
$operator = $_POST['operator'];
$portering = $_POST['portering'];
$epost = $_POST['epost'];
// Database connection
$conn = new mysqli('localhost','my_username','my_password','id14293554_rw2');
if($conn->connect_error){
echo "$conn->connect_error";
die("Connection Failed : ". $conn->connect_error);
} else {
$stmt = $conn->prepare("INSERT INTO sales_table(ident_column, date_column, name_column, gsm_column, birthdate_column, pp_column, carrier_column, transfer_column, email_column) values(?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssss", $ident, $dato, $kundensnavn, $gsm, $fodselsdato, $prisplan, $operator, $portering, $epost);
$execval = $stmt->execute();
echo $execval;
echo "Done!";
$stmt->close();
$conn->close();
}
?>
This is my index.html if needed:
<!DOCTYPE html>
<html lang="no" >
<head>
<meta charset="UTF-8">
<title>Test</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<!-- partial:index.partial.html -->
<div class="container">
<form id="contact" action="insert.php" method="post">
<h3><center>Reg</center></h3>
<fieldset>
<input type="text" placeholder="Ident" name="ident" required autofocus>
</fieldset>
<fieldset>
<input type="text" placeholder="Dato" name="dato" required>
</fieldset>
<fieldset>
<input type="text" placeholder="Kundens navn" name="kundensnavn" required>
</fieldset>
<fieldset>
<input type="text" placeholder="GSM" name="gsm" required>
</fieldset>
<fieldset>
<input type="text" placeholder="Fødselsdato" name="fodselsdato" required>
</fieldset>
<fieldset>
<input type="text" placeholder="Prisplan" name="prisplan" required>
</fieldset>
<fieldset>
<input type="text" placeholder="Operatør" name="operator" required>
</fieldset>
<fieldset>
<input type="text" placeholder="Portering" name="portering" required>
</fieldset>
<fieldset>
<input type="email" placeholder="Epost" name="epost" required>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Save</button>
</fieldset>
</form>
</div>
<!-- partial -->
</body>
</html>
When I press my "Submit" button, this scripts run, and gives me "Done!", so no errors. But, when I check in my table, there is nothing there, even if I type in all the info needed:
Image here
Any tips why it won't work?
EDIT:
This is my db table structure:

Your table contain Integer column gsm_column
But you passing string value here:
$stmt->bind_param("sssssssss", $ident, $dato, $kundensnavn, $gsm, $fodselsdato, $prisplan, $operator, $portering, $epost); <---------
Try to change to this:
$stmt->bind_param("sssisssss", $ident, $dato, $kundensnavn, $gsm, $fodselsdato, $prisplan, $operator, $portering, $epost);
Also i would change the last section
if ($stmt->execute()) {
// it worked
echo "Done";
} else {
// it didn't
echo "Not inserted";
//Print error
echo $stmt->error;
}
$stmt->close();
$conn->close();

Related

Insert value of multiple checkbox in sqlite database using php pdo

Hi I have multiple check boxes name make and female I want that if a person select make and don't select female it input nothing or false in database now if I don't select example: female it return nothing in female row but it also get a error
Undefined index: Female in /storage/sdcard1/www/3/signup/index.php on line 11
but I don't want this error and I want the if female not select it insert false and don't show error I don't want the selected check box value to be true because it is wrote as on in database
Code index.php
<?php
if(isset($_POST['submit'])){
$conn = new PDO("sqlite: sign.db");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$Email = $_POST['Email'];
$First = $_POST['First'];
$Last = $_POST['Last'];
$Password = $_POST['Password'];
$Male = $_POST['Male'];
$Female =$_POST['Female'];
$Dateofb = $_POST['Dateofb'];
require_once 'imp.php';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css" >
<script src="index.js" ></script>
<title>Survey</title>
</head>
<body>
<form action="" method="post" >
<div class="container" >
<div class="form" >
<input type="email" class="first" id="Email" name="Email" placeholder="Email" required="required">
<input type="text" class="second" id="First" name="First" placeholder="First name" required="required">
<input type="text" class="last" id="Last" name="Last" placeholder="Last name" required="required">
<input type="password" class="pass" name="Password" id="Password" placeholder="Password" required="required">
<div class="day" >
<p class="bd" >Birthday Date:</p>
<input type="date" class="date" id="Dateofb" name="Dateofb" >
</div>
<div>
<div class="malee" >
<input type="checkbox" class="male" id="Male" name="Male">
<p class="mal" >Male</p>
</div>
<div class="femalee" >
<input type="checkbox" class="female" id="Female" name="Female" >
<p class="fem" >Female</p>
</div>
</div>
<div >
<input class="submit" id="submit" type="submit" name="submit" >
</div>
<div class="acc" >
already have account <a href="#" >Login</a>
</div></div>
</div>
</form>
</body>
</html>
Code imp.php
<?php
$sql = "INSERT INTO signup (`Email`, `First`, `Last`, `Password`, `Male`, `Female`, `Dateofb`) VALUES ('$Email', '$First', '$Last', '$Password', '$Male', '$Female', '$Dateofb');";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
$conn->connection = null;
?>
The problem is that only checked boxes are submitted.
You need to check if a box's name is set in $_POST and IF NOT:
manually assign a value you wish to use instead
I.e. use this short hand if/else
$Female = (isset ($_POST['Female'])) ? 1 : 0;

Submit button in the html form is displaying the php script instead of sending form data to the db

I have this problem with the submit button in the html form where by when I click it instead of sending the form details to the database it displays the php script in a new window,I've tried everything I could still not working,please help
html code:
<!DOCTYPE html>
<html>
<head>
<title>sign up</title>
<link href="sign in.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div class="signbox">
<h1>SIGN UP HERE</h1>
<form method="" action="connect.php" method="POST">
<p>First name:</p>
<input type="text" name="firstname" placeholder="enter your first name">
<p>Email address:</p>
<input type="text" name="email" placeholder="enter your email address">
<p>Password:</p>
<input type="password" name="password" placeholder="enter your password">
<p>Confirm password:</p>
<input type="password" name="password2" placeholder="confirm your password">
<input type="submit" value="sign in">
already have an account?login here.
</form>
</div>
</body>
</html>
PHP code:
<?php
$firstname = $_POST['firstname'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
//database connection.
$conn = new mysqli('loacalhost','root','','','first_db');
if($conn->connect_error){
die('connection failed : '.$conn->connect_error);
}else{
$stmt = $conn->prepare("insert into users3(firstname, email, password, password2)
values(?, ?, ?, ?)");
$stmt->bind_param("ssss",$firstname, $email, $password, $password2);
$stmt->execute();
echo "registration complete...";
$stmt->close();
$conn->close();
}
?>
In your form tag there are 2 method attributes, should be corrected as;
<form action="connect.php" method="POST">
And also in the php code, mysqli connection should be;
$conn = new mysqli('localhost','root','','first_db');
in which the four parameter are;
$conn= new mysqli("server name","username","password","database name");

Filling data from a form into a database using php

I want to insert data from a form into a database.
I've searched for a long time but can't figure out what I am doing wrong. Any help will be appreciated
HTML
<!DOCTYPE html>
<html>
<head>
<title>PHP insertion</title>
<link href="css/insert.css" rel="stylesheet">
</head>
<body>
<div class="maindiv">
<!--HTML Form -->
<div class="form_div">
<div class="title">
<h2>Book Information</h2>
</div>
<form action="new 12.php" method="post">
<!-- Method can be set as POST for hiding values in URL-->
<h3>Enter the Details</h3>
<label>Access number</label>
<input class="input" name="access" type="text" value=""><br>
<label>Title</label>
<input class="input" name="title" type="text" value=""><br>
<label>Author</label>
<input class="input" name="author" type="text" value=""><br>
<label>Edition</label>
<input class="input" name="edition" type="text" value=""><br>
<label>Publisher</label>
<input class="input" name="publisher" type="text" value=""><br>
<input class="submit" name="submit" type="submit" value="Submit"><br>
</form>
</div>
</div>
</body>
</html>
PHP
<?php
$link = mysqli_connect("localhost", "root", "admin", "library");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "INSERT INTO library (access,title,author,edition,publisher) VALUES ('$access','$title','$author','$edition','$publisher')";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
$access is undefined. You need to refer to $_POST['access'] to get the value of your POSTed form. Same for all other fields.
First of all, I strongly recommend to make a PDO conection like:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
Second: I would put id to all of the inputs
Third: You´re missing the $_POST["value"]So you are not sending information
The html should look like:
<!DOCTYPE html>
<html>
<head>
<title>PHP insertion
</title>
<link href="css/insert.css" rel="stylesheet">
</head>
<body> <div class="maindiv">
<!--HTML Form -->
<div class="form_div">
<div class="title">
<h2>Book Information</h2>
</div>
<form action="new 12.php" method="post">
<!-- Method can be set as POST for hiding values in URL-->
<h3>Enter the Details</h3>
<label>Access number</label> <input class="input" name="access" id="access" type="text" value=""><br>
<label>Title</label> <input class="input" name="title" id="title" type="text" value=""><br>
<label>Author</label> <input class="input" name="author" id="author" type="text" value=""><br>
<label>Edition</label> <input class="input" name="edition" id="edition" type="text" value=""><br>
<label>Publisher</label> <input class="input" name="publisher" id="publiser" type="text" value=""><br>
<input class="submit" name="submit" type="submit" value="Submit"><br>
</form>
</div>
</div>
</body>
</html>
And the php like this:
<?php
try {
$conn = new PDO("mysqli:server = yoursever; Database = yourdatabase", "user", "pass");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
print("Error connecting to Server.");
die(print_r($e));
}
$access= $_POST['access'];
$title= $_POST['title'];
$author= $_POST['author'];
$edition= $_POST['edition'];
$publisher= $_POST['publisher'];
$sql = "INSERT INTO library (access, title, author, edition, publisher) VALUES (?,?,?,?,?)";
$stmti= $conn->prepare($sql);
$stmti->execute([$access, $title, $author, $edition, $publisher]);
if ($stmti->error){
echo "ERROR";
}
else{
echo "Records inserted successfully.";
}
$conn->close();
?>
This is one safe way to insert info in your server to prevent SQL inyection, please read How does the SQL injection from the "Bobby Tables" XKCD comic work? to understand better how to prevent inyection into your server

HTML Form + PHP + MYSQL 500 Error

I am receiving an internal 500 error when trying to insert data from HTML form into MySQL database via PHP. I am hosting a site with an Apache 2 webserver on an Ubuntu server (droplet using DigitalOcean). PHP and MySQL are both installed on the server. HTML below and PHP below. I have tested with a PHP echo with the variable $name, which was successful, but I am thrown the error whenever I try to connect to mysql. I know 500 is an internal error, but am not sure where to go from here.
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title>who this</title>
<link rel="stylesheet" type="text/css" href="STYLE.css">
</head>
<body>
<h1 id="header2">me</ h1>
<div id="back">
back
</div>
<div id="me-intro">
<p>tell me about yourself</p>
</div>
<div id="joke">
<form action="process.php" method="POST">
<div id="one">
<p>name: <input type="text" name="name" id='first'></p>
</div>
<div id="two">
<p>gender: <input type="text" name="gender" id='second'></p>
</div>
<div id="three">
<p>age: <input type="text" name = "age" id='third'></p>
</div>
<div id="four">
<p>occupation: <input type="text" name="occupation" id='fourth'></p>
</div>
<div id="five">
<p>education: <input type="text" name="education" id='fifth'></p>
</div>
<div id="six">
<p>car you drive: <input type="text" name="car" id='sixth'></p>
</div>
<div id="seven">
<p>phone number: <input type="text" name="phone" id="seventh"></p>
</div>
<div id="eight">
<p>address: <input type="text" name="address" id="eighth"></p>
</div>
<div id="nine">
<p>social security number: <input type="text" name="ssn" id="ninth">
</div>
<div id="ten">
<input type="submit">
</div>
</form>
</div>
</body>
</html>
process.php below
<?php
$conn = new mysqli("localhost", "root", "password_here", "database_here");
$name = $_POST["name"];
$gender = $_POST["gender"];
$age = $_POST["age"];
$occupation = $_POST["occupation"];
$education = $_POST["education"];
$car = $_POST["car"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$ssn = $_POST["ssn"];
if($conn -> connect_error) {
die("err: dis not working: " . $conn->connect_error);
}
$sql = "INSERT INTO table_here (name, gender, age, occupation, education, car, phone, address, ssn) VALUES ('$name', '$gender', '$age', '$occupation', '$education', '$car', '$phone', '$address', '$ssn')";
if($conn->query($sql) === TRUE){
echo "success";
} else {
echo "error: did not submit form to mysql";
}
$conn->close();
?>

Information is not saved in mysql database after submitting in form

This is the code for saving the information into mysql database from a FORM.
In the HTML section the form is being handled i.e. retrieving required data from user.
In the PHP section storing data has been handled.
But the problem is it doesn't store data.
I'm using XAMPP server.
<html>
<head>
<title>signup</title>
<link rel="stylesheet" href="css/insert.css" />
</head>
<body>
<div class="maindiv">
<!--HTML form -->
<div class="form_div">
<div class="title"><h2>Insert Data In Database Using PHP.</h2> </div>
<form action="signup.php" method="post"> <!-- method can be set POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<br />
<input class="input" type="text" name="name" value="" />
<br />
<label>Email:</label><br />
<input class="input" type="text" name="mail" value="" />
<br />
<label>Phone:</label><br />
<input class="input" type="text" name="phone" value="" />
<br />
<label>Password:</label><br />
<input class="input" type="text" name="pass" value="" />
<br />
<label>Address:</label><br />
<textarea rows="5" cols="25" name="add"></textarea>
<br />
<input class="submit" type="submit" name="submit" value="Insert" />
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");
//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){
//Fetching variables of the form which travels in URL
$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
//Closing Connection with Server
mysql_close($connection);
?>
</form>
</div>
</div>
</body>
I don't understand what can be the problem.
Thanks all. I got the problem.
Actually the sequence of the column in my database was not matching with the query in php code.
I have solved this by changing the variable sequence in the query which is maintained in the database.
$query = mysql_query("INSERT INTO user (`name`, `mail`, `pass`, `address`, `phone`)VALUES('".$name."', '".$mail."', '".$pass."', '".$address."', '".$phone."')");
Here is the code and it will work for your..
I have passed connection link in your mysql_query. and used PHP_SELF for current page.
<html>
<head>
<title>signup</title>
<link rel="stylesheet" href="css/insert.css" />
</head>
<body>
<div class="maindiv">
<!--HTML form -->
<div class="form_div">
<div class="title"><h2>Insert Data In Database Using PHP.</h2> </div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- method can be set POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<br />
<input class="input" type="text" name="name" value="" />
<br />
<label>Email:</label><br />
<input class="input" type="text" name="mail" value="" />
<br />
<label>Phone:</label><br />
<input class="input" type="text" name="phone" value="" />
<br />
<label>Password:</label><br />
<input class="input" type="text" name="pass" value="" />
<br />
<label>Address:</label><br />
<textarea rows="5" cols="25" name="add"></textarea>
<br />
<input class="submit" type="submit" name="submit" value="Insert" />
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");
//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){
//Fetching variables of the form which travels in URL
$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')",$connection);
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
//Closing Connection with Server
mysql_close($connection);
?>
</form>
</div>
</div>
</body>

Categories