Inserting data with PHP from a form to MySQL database - php

I have created a HTML form which will need to insert the data that was entered into the form straight into a table in mySQL.
newuser.php file
<?php
//including the connection page
include('./DB_Connect.php');
//get an instance
$db = new Connection();
//connect to database
$db->connect();
//fetch username and password
$usertype = $_POST['userType'];
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$username = $_POST['userName'];
$password = $_POST['password'];
$address1 = $_POST['add1'];
$address2 = $_POST['add2'];
//write the sql statement
$query = "INSERT INTO USERS (usertype, fname, lname, username, password, add1, add2)
VALUES ('$usertype', '$firstname', '$lastname', '$username', '$password', '$address1', '$address2')";
mysql_query($query,$db);
if (($query) == TRUE) {
echo "New record created successfully";
header("Location: login.php", true);
exit();
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
header("Location: register.php", true);
exit();
}
//close once finished to free up resources
$db->close();
?>
With the following html form:
<form action="newuser.php" method="POST" class="form" id="registerForm">
<label> Type of user: </label> <br>
<input type="radio" name="userType" id="itemSeeker">Item Seeker </input>
<input type="radio" name="userType" id="itemDonor">Item Donor </input>
<input type="radio" name="userType" id="peopleSeeker">People Seeker </input>
<input type="radio" name="userType" id="peopleDonor">People Donor </input>
<br>
<br>
<div class="form-inline">
<div class="form-group">
<input type="text" name="firstName" placeholder="First Name: " align="center" class="form-control" ></input>
</div>
<div class="form-group">
<input type="text" name="lastName" placeholder="Last Name: " align="center" class="form-control" ></input>
</div>
</div>
<br>
<input type="text" name="email" placeholder="Email Address: "align="center" class="form-control" ></input>
<br>
<div class="form-inline">
<div class="form-group">
<input type="text" name="userName" placeholder="Username: " align="center" class="form-control" ></input>
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Password: " align="center" class="form-control" ></input>
</div>
</div>
<br>
<!-- <label> Address 1: </label>-->
<input type="text" name="add1" placeholder="Address 1: " align="center" class="form-control" ></input>
<!-- <label> Address 2: </label>-->
<input type="text" name="add2" placeholder="Address 2: " align="center" class="form-control" ></input>
<br>
<button class="btn btn-primary" name="submitReg" type="submit">Submit</button><br>
<br>
<a href="login.php" >Already have an account?</a>
</form>
The USERS table
The above two blocks of code and the code that I'm working with.
My problem here is, when the form is submitted, the data isn't actually being entered into the table. Note that the first ever submission actually did work. All submissions after the first one don't seem to be entering anything into the database.
I'm not quite sure what's wrong with my code for it to not work. It does go to the 'login.php' page which means there aren't any faults and the query submitted correctly.
Could someone please tell me what I'm doing wrong, thank you.

right now you have alot more trouble than your insert problem. your code is totaly insecure. (mysql injections).
Dont use mysql_* functions use pdo instead with prepared statements!
you output spaces before you send a header you cant send header if you have output. your redirect wouldnt work. dont relay on a clientside redirect use may have it disabled so output a link where you want the user to go.
anotherthing your radio buttons have no value check html syntax
var_dump($_POST) and check if you submit everything. also check for isset or empty befor assign variables. do some sort of validation.
have a look at some php frameworks they provide much more flexibility and error checking
dont reinvent the wheel by writing everthing by your own in a 10 or more year behind procedural way

Related

php POST from html form, but can not get data

*
Im trying to make a Register page. But the POST in php can not get any data from form in html page. If I ran the code. The raw in data base is created but doesn't have any data inside. PLease Help!
*
My insert.php
$firstname=mysqli_real_escape_string($_POST ['firstname']);
$lastname=mysqli_real_escape_string($_POST ['lastname']);
$pw=md5(mysqli_real_escape_string($_POST ['pw']));
$pw2=md5(mysqli_real_escape_string($_POST ['pw2']));
$email= mysqli_real_escape_string($_POST ['email']);
$mobile=mysqli_real_escape_string($_POST ['mobile']);
$address=mysqli_real_escape_string($_POST ['address']);
$postcode=mysqli_real_escape_string($_POST ['postcode']);
$sql = "INSERT INTO user (user_firstname, user_lastname, user_email)
VALUES ('$firstname', '$lastname', '$email')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
echo $sql;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
My form in html:
<form action="insert.php" method="POST">
<div class="login-img"><img src="images/register-ico.png"></div>
<div class="login-form">
<label><i></i><p>Email Address<span class="required"> *</span></p></label>
<input class="login-input" name='email' id="email" type="text" placeholder="harry#boei.co.nz">
<label><i></i><p>First Name<span class="required"> *</span></p></label>
<input class="login-input" name='firstname' id="firstname" type="text">
<label><i></i><p>Last Name</p></label>
<input class="login-input" name ='lastname'id="lastname" type="tel">
<label><i></i><p>Password<span class="required"> *</span></p></label>
<input class="login-input" name='pw'id="password" type="email">
<label><i></i><p>Re-enter Password<span class="required"> *</span></p></label>
<input class="login-input" 'name'='pw2' id="password2" type="text">
<label><i></i><p>Mobile</p></label>
<input class="login-input" name='mobile'id="mobile" type="text">
<label><i></i><p>Delivery Address</p></label>
<input class="login-input" name='address'id="address" type="text">
<label><i></i><p>Postcode</p></label>
<input class="login-input" name='postcode'id="postcode" type="text">
<div class="step-required"><span class="required"> * required field</span></div>
<p><input type="submit" name="submit" value="submit"></p>
</div>
<div class="register-info">
<input name="reg" type="reg" value="reg">
<div class="register-text"><p>By Registering, you agree to accept our <br>Terms and Conditions and Privacy.</p></div>
</div>
</form>
what is your PHP version? HTTP_RAW_POST_DATA DEPRECATED in PHP 5.6.0 and REMOVED as of PHP 7.0.0 and it's just getting raw POST data.
HTTP_RAW_POST_DATA it should be $_POST.
You are typing 'Email' incorrectly in post parameters.
declare mysql connection
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
and it should be something like
$firstname = $mysqli->real_escape_string($_POST["firstname"]);
and if you want a procedure type
string mysqli_real_escape_string ( mysqli $link , string $escapestr )
$firstname = mysqli_real_escape_string($SQLConnection, $_POST["firstname"]);

PHP Script will echo statements but will not insert data into table from modal

I am building a simple client management system that allows users to input client data into a form populated in a modal. The idea is that when the user completes the form in the modal by clicking Create New Client, new client information will be stored into the predefined mysql table named client. I am using a nearly identical php script throughout the site to create new users which works fine.
The primary difference with this script is that is called from a form that is enclosed within a modal that is enclosed within divs. Could that possibly be inhibiting my scripting from following through on the query?
There are no error messages other than my own defined if else calls. The data will echo on Submit but information will not actually insert into the data in mysql. I'm still new to anything beyond html and css, so any help will be greatly appreciated and reciprocated as I continue to get a handle on this journey. Reference code below
newclient.php
<?php
session_start();
include 'dbh.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
$comname = $_POST['comname'];
$primcon = $_POST['primcon'];
$addr = $_POST['addr'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phn = $_POST['phn'];
$websi = $_POST['websi'];
$email = $_POST['email'];
$activ = $_POST['activ'];
$sql = "INSERT INTO client (comname, primcon, addr, city, state, zip, phn,
websi, email, activ )
VALUES ('$comname', '$primcon', '$addr', '$city', '$state', '$zip', '$phn',
'$websi', '$email', '$activ')";
$result = mysqli_query($conn, $sql);
echo $comname;
echo $primcon;
echo $addr;
echo $city;
echo $state;
echo $zip;
echo $phn;
echo $websi;
echo $email;
echo $activ;
//header("Location: login.html");
admin.html - where users actually create new clients (modal content only)
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>New Client Registration</h2>
</div>
<div class="modal-body">
<form align="center"
action="http://localhost:1234/housenotes/newclient.php" method="POST">
<input type="text" placeholder="Company Name" name="comname"
required>
<input type="text" placeholder="Primary Contact" name="primcon"
required>
<input type="text" placeholder="Street Address" name="addr"
required> <br>
<input type="text" placeholder="City" name="city" required>
<input type="text" placeholder="State" name="state" required>
<input type="text" placeholder="Zip" name="zip" required> <br>
<input type="text" placeholder="Phone Number" name="phn"
required>
<input type="text" placeholder="Website" name="websi" required>
<input type="text" placeholder="Email" name="email" required>
<br>
<input type="radio" name="activ" value="Potential" checked>
Potential
<input type="radio" name="activ" value="Engaged"> Engaged
<input type="radio" name="activ" value="Active"> Active<br>
<button type="submit" class="button" style="width:15%;">Create
New Client</button>
</form>
</div>
<div class="modal-footer">
<h3>housenotes</h3>
</div>
Again, any help would be greatly appreciated! Thanks in advance!

Website database issue... [PHP, MySQLi, HTML]

So I am working on this under construction page for my own small business's website. I want to have an email field so that users write their email so that i can later tell them when the page is finished. But I am having trouble with the database. Apparently the connection is established but the data is not being written to the table in the DB. This is my php code, it is embedded on the top of my index.html:
<?php require 'connections/connections.php'; ?>
<?php
if(isset($_POST['submit'])) {
$Fname = $_POST['first_name'];
$Lname = $_POST['last_name'];
$Email = $_POST['email'];
$sql = $con->query("INSER INTO subscriptions (Fname, Lname, Email)Values('{$Fname}', '{$Lname}', '{$Email}')");
header ('Location: confirm.html');
}
?>
This is my connections.php file, required by the above code:
<?php
$con = mysqli_connect("localhost", "webmaster_velez", "Cristianpromw3", "nexus_subscriptions");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
This is the HTML that the user interacts with to send their email address:
<div id="subscribe">
<form id="SubscriptionForm" name="RegisterForm" method="post">
<div class="FormElement">
<input name="first_name" type="text" required="required" class="TField" id="first_name" form="SubscriptionForm" placeholder="First Name">
</div>
<div class="FormElement">
<input name="last_name" type="text" required="required" class="TField" id="last_name" form="SubscriptionForm" placeholder="Last Name">
</div>
<div class="FormElement">
<input name="email" type="email" required="required" class="TField" id="email" form="SubscriptionForm" placeholder="Email">
</div>
<div class="FormElement">
<input name="submit" type="submit" class="button" id="submit" form="SubscriptionForm" value="Submit">
</div>
</form>
</div>
Any help, corrections, and/or advice on this would be much appreciated and accepted. Thanks in advance.
PS. I am new to PHP and database/server dynamics.
From reading the code you've written, I would assume that the main problem you are facing is this typo - "INSER INTO" should be "INSERT INTO".
You'll also need to implement some try error blocks to catch if any errors are happening and protect yourself from SQL vulnerabilities/injections.

jquery mobile form does not submit

I would like to get some user data using a php form and store it in a mysql database , trouble is, the page simply refreshes when I submit the form.
Here is my php form:
<form id="companyform" name="companyform" method="post" action="index3.php" data-ajax="false">
<b>To enlist your business fill in the form below:</b>
<p>
<label for="name">Company Name:</label>
<input type="text" name="companyname" id="companyname" data-mini="true"/>
</p>
<p>
<label for="name">Company Address:</label>
<input type="text" name="companynaddress" id="companynaddress" data-mini="true"/>
</p>
<p>
<label for="textfield">Tel No.:</label>
<input type="text" name="tel" id="tel" data-mini="true"/>
</p>
<p>
<label for="textfield">Fax No.:</label>
<input type="text" name="fax" id="fax" data-mini="true"/>
</p>
<p>
<label for="textfield">Email:</label>
<input type="text" name="email" id="email" data-mini="true"/>
</p>
<p>
<label for="textfield">Website Address:</label>
<input type="text" name="website" id="website" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Name:</label>
<input type="text" name="contactname" id="contactname" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Number:</label>
<input type="text" name="contactnumber" id="contactnumber" data-mini="true"/>
</p>
<p>
<label for="textfield">Contact Person Email:</label>
<input type="text" name="contactemail" id="contactemail" data-mini="true"/>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Submit" />
</p>
</form>
and here is my database connection code:
<?php
if (array_key_exists('submit', $_POST)) {
$con = mysql_connect("host", "user", "pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("botswanasearchdb", $con);
$companyname = $_POST['companyname'];
$companyaddress = $_POST['companyaddress'];
$tel = $_POST['tel'];
$fax = $_POST['fax'];
$emailid = $_POST['emailid'];
$website = $_POST['website'];
$contactname = $_POST['contactname'];
$contactnumber = $_POST['contactnumber'];
$contactemail = $_POST['contactemail'];
// prepare the SQL query
$sql = "INSERT INTO businessuser (companyname, companyaddress, tel, fax, emailid, website, contactname, contactnumber, contactemail) VALUES ('$companyname', '$companyaddress', '$tel', '$fax', '$emailid', '$website', '$contactname', '$contactnumber', '$contactemail')";
mysql_close($con);
}
?>
This is your form tag:
<form id="companyform" name="companyform" method="post" action="index3.php" data-ajax="false">
So you are submitting the form to index3.php (the action attribute). According to your comment index3.php contains your form and that is why the form refreshes when you submit it. You are basically reloading your form on form submit.
You need to submit the form to your php script that contains the php code you posted.
Edit: If everything is on the same page, you can do something like:
if (array_key_exists('submit', $_POST))
{
// your code
// show thank you message
}
else
{
// show form
}
Another edit: As you are using the deprecated mysql_* functions and not escaping the data, you have an sql injection whole and a ' character in your data will break your query. You should switch to PDO / mysqli and prepared statements. And always add error handling.

unsubscribe html form using php and my sql

I have an html form where people can subscribe to a mailing list. The form includes form validation and when the form is submitted, the data is stored in a database using My SQL.
Here is the code on the index.html page where the form is
<form id="subscribe-form" action="send.php" method="post">
<p id="status"></p>
<div>
<label for="title">Title:</label>
<select class="uniform" name="title" id="title">
<option>Please Choose</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select>
</div>
<div>
<label for="firstName">First name:</label>
<input type="text" id="firstName" name="firstName" />
</div>
<div>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname" />
</div>
<div>
<label for="email">Email:</label>
<input type="text" id="email" name="email" />
</div>
<div>
<label for="phone">Contact Number:</label>
<input type="text" id="phone" name="phone" />
</div>
<div>
<label for="title">How did you hear about us?</label>
<select class="uniform" name="refer" id="refer">
<option>Please Choose</option>
<option>Google</option>
<option>Yahoo</option>
<option>Word of Mouth</option>
<option>Others</option>
</select>
</div>
<div>
<input type="checkbox" name="news_updates" value="1" />
I'd like to hear about the latest news and events updates</div>
<div>
<input class="button" type="submit" value=""/>
</div>
</form>
Here is the code for send.php
<?php
include ('connection.php');
$sql="INSERT INTO form_data (title,firstName, surname, email, phone, refer, news_updates)
VALUES
('$_POST[title]', '$_POST[firstName]','$_POST[surname]','$_POST[email]','$_POST[phone]','$_POST[refer]','$_POST[news_updates]')";
if (!mysql_query($sql, $connected))
{
die('Error: ' . mysql_error());
}
mysql_close($connected);
?>
I would like to make another html (unsubscribe.html) page where people can unsubscribe by entering their email address so that their email address would match the corresponding email that is in the database already and remove it from the My Sql database .
I found this tutorial which was kind of helpful -
http://www.phpsuperblog.com/php/delete-records-from-mysql-database-with-html-form-and-php/
and this is the form on my unsubscribe.html page.
<form id="unsubscribe_form" action="delete.php" method="post">
<div>
<label for="email_remove">Email:</label>
<input type="text" id="email_remove" name="email_remove" />
</div>
<div>
<input name="delete" type="submit" id="delete" value="" class="unsubscribe_btn">
</div>
</form>
but when I enter method="post" in the unsubscribe form. The data from the form on the subscribe / index.html does not get stored in My Sql, instead they come up as blank.
So I am guessing I can't have two "post" method maybe??
If someone could guide me in the right direction that would be much appreciate. Thanks.
I guess you are at your learning stage. So, I will suggest you to have a check for POST method being called on the page which receives the post.
Example: in your subscribe.php
you should have :
<input class = "button" type = "submit" value = "Subscribe" name = "subscribe" />
in send.php
you must do:
if(!isset($_POST['subscribe'])
{
header('location: subscribe.html');
}
You must use isset for your pages.
If you could display your delete.php, perhaps I can edit this post and assist you further but, so far... A check is required and you can use as many forms as many you like (even on one page) but, make sure they all have different id/names.
Your delete.php script should be:
<?php
require ('connection.php'); // User require for important functions so that if not found, it throws fatal error
$email = $_POST['email_remove'];
// Check for isset POST
$query = "DELETE from form_data WHERE email = '".$email."'";
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";}
?>
your delete.php seems OK to me.
Can add the following to Line 2
echo "";
print_r($_POST);
and post array in comments?

Categories