My form won't post inserted data into my database, i know this is a very basic problem but I am only just starting to learn to code
connect_to_mysql.php:
<?php
$db_host="localhost";
$db_username="ajamesbird";
$db_pass="";
$db_name="test";
$db_connect = mysql_connect("$db_host","$db_username","$db_pass")or die
("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
login.php
<html>
<?php include "C:\Users\andrew\Documents\Websites\Seller\storescripts\connect_to_mysql.php";?>
<?php
if(isset($_POST['loginform'])){
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$sql = ("INSERT INTO users (id, access_level, username, firstname,
lastname, email, password, dob, date_added, activated)
VALUES ('NULL','NULL','$username','$firstname','$lastname','$email', '$password', '$dob', now(), '0')") or die (mysql_error());
if(!mysql_query($db_connect, $sql)){
die('Error inserting into database');
}
}
?>
<head>
<link href="style/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="login.php" enctype="multipart/form-data" name="loginform" id="loginform" method="post">
<input name="username" type="text" id="username" size="63" class="form-control" value="Username" required/>
<input name="firstname" type="text" id="firstname" size="63" class="form-control" value="First name" required/>
<input name="lastname" type="text" id="lastname" size="63" class="form-control" value="Last name" required/>
<input name="email" type="email" id="email" size="63" class="form-control" value="Email" required/>
<input name="password" type="password" id="password" size="63" class="form-control" value="Password" required/>
<input name="dob" type="text" id="dob" size="63" class="form-control" value="Date of Birth" required/>
<input type="submit" name="button" id="button" size="64" value="Sign Up" />
</form>
</body>
</html>
Thank you in advance
Try to move name="loginform" from and put it in hidden input
<html>
<?php include "C:\Users\andrew\Documents\Websites\Seller\storescripts\connect_to_mysql.php";?>
<?php
if(isset($_POST['loginform'])){
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$sql = ("INSERT INTO users (id, access_level, username, firstname,
lastname, email, password, dob, date_added, activated)
VALUES ('NULL','NULL','$username','$firstname','$lastname','$email', '$password', '$dob', now(), '0')") or die (mysql_error());
if(!mysql_query($db_connect, $sql)){
die('Error inserting into database');
}
}
?>
<head>
<link href="style/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="login.php" enctype="multipart/form-data" method="post">
<input name="username" type="text" id="username" size="63" class="form-control" value="Username" required/>
<input name="firstname" type="text" id="firstname" size="63" class="form-control" value="First name" required/>
<input name="lastname" type="text" id="lastname" size="63" class="form-control" value="Last name" required/>
<input name="email" type="email" id="email" size="63" class="form-control" value="Email" required/>
<input name="password" type="password" id="password" size="63" class="form-control" value="Password" required/>
<input name="dob" type="text" id="dob" size="63" class="form-control" value="Date of Birth" required/>
<input type="submit" name="button" id="button" size="64" value="Sign Up" />
<input type="hidden" name="loginform">
</form>
</body>
</html>
Related
I am trying to add data into 2 tables. the data arrives into my table called companies with no issues. however the data does not arrive into the table users. I do not get an error message and the page loads the admin page afterwards.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<title>Create a Company</title>
<link rel="stylesheet" type="text/css" href="/css.css"/>
</head>
<body>
<h2 class="header"> Create a Company </h2>
<form action="processcompany.php" method="post">
<input class="entry" placeholder="Account No" name="accountno" type="text"><br>
<input class="entry" placeholder="Company Name" name="companyname" type="text" required="required"><br>
<input class="entry" placeholder="GST/VAT/ABN/TAX No" name="taxno" type="text" required="required"><br>
<input class="entry" placeholder="Address Line 1" name="address1" type="text" value=""><br>
<input class="entry" placeholder="Address Line 2" name="address2" type="text" value=""><br>
<input class="entry" placeholder="Suburb/County" name="suburb" type="text" value=""><br>
<input class="entry" placeholder="State" name="state" type="text" value=""><br>
<input class="entry" placeholder="Post/Zip Code" name="postcode" type="text" value=""><br>
<input class="entry" placeholder="Country" name="country" type="text" value=""><br>
<input class="entry" placeholder="Primary Contact" name="primarycontact" type="text" value=""><br>
<input class="entry" placeholder="Primary Email" name="primaryemail" type="text" value=""><br>
<input class="entry" placeholder="Subscription Type" name="subscriptiontype" type="text" value=""><br>
<input class="entry" placeholder="Subscription Status" name="subscriptionstatus" type="hidden" value="Active"><br>
<input class="entry" placeholder="Subscription End Date" name="subscriptionenddate" type="text" value=""><br><br><br>
<input class="entry" placeholder="login Email Address" name="loginname" type="text" value=""><br>
<input class="entry" placeholder="First and Last Name" name="counttypename" type="text" value=""><br>
<input class="entry" placeholder="User Type" name="usertype" type="hidden" value="Company Administrator"><br>
<input class="entry" placeholder="User Status" name="status" type="hidden" value="Active"><br>
<input class="entry" placeholder="Password" name="password" type="text" value=""><br>
<input class="button" type="submit">
</form>
</body>
</html>
this is my script file
<?php
include 'db.php';
$sql = "INSERT INTO `companies`
( `accountno`, `companyname` ,
`taxno` , `address1`, `address2`, `suburb` ,
`state` , `postcode`, `country`, `primarycontact` ,
`primaryemail`, `subscriptiontype` ,
`subscriptionstatus`, `subscriptionenddate`,
`datecreated` )
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";
$stmt = $conn->prepare($sql);
if ( ! $stmt ) {
echo $stmt->error;
exit;
}
$stmt->bind_param('isssssssssssss',
$_POST['accountno'],
$_POST['companyname'],
$_POST['taxno'],
$_POST['address1'],
$_POST['address2'],
$_POST['suburb'],
$_POST['state'],
$_POST['postcode'],
$_POST['country'],
$_POST['primarycontact'],
$_POST['primaryemail'],
$_POST['subscriptiontype'],
$_POST['subscriptionstatus'],
$_POST['subscriptionenddate']
);
$stmt->execute();
if ( ! $stmt ) {
echo $stmt->error;
exit;
}
$sqla = "INSERT INTO `users`
( `accountno`, `loginname` ,
`password` , `countteamname`, `status`, `usertype` ,
`datecreated` )
VALUES (?,?,?,?,?,?,NOW())";
$stmta = $conn->prepare($sqla);
if ( ! $stmta ) {
echo $stmta->error;
exit;
}
$stmta->bind_param('isssss',
$_POST['accountno'],
$_POST['loginname'],
$_POST['password'],
$_POST['countteamname'],
$_POST['status'],
$_POST['usertype']
);
$stmta->execute();
if ( ! $stmta ) {
echo $stmta->error;
exit;
}
mysqli_close($conn);
header('location: admin.php');
?>
my db values are
userid, accountno, loginname, password, countteamname, status, counttype, piid, datecreated
counttype and piid are not being added at this moment to the table. userid is an auto increment number by mysql.
once i get this uploaded, I will work on securing the password using hash.
I have been trying to figure this out on my own for hours. I hope you can help.
By the looks of it your only adding to the one table with the query
what about concatenating two queries
$sql = "QUERY;";
$sql .= "QUERY;";
use the .= to concatenate both queries.
I'm a newbie in HTML and PHP, so please do help. The values entered in the form are not getting stored in the database. I've checked for the database connection and it is connecting. I have no idea what's wrong with the code.
<?php
include("con.php");
$msg="";
if(isset($_POST["sub_btn"])) {
$id_length=6;
$id=crypt(uniqid(rand(),1));
$id=strip_tags(stripslashes($id));
$id=str_replace(".","",$id);
$id=strrev(str_replace("/","",$id));
$id=substr($id,0,$id_length);
$userid=$id;
$fname=$_POST["fname"];
$lname=$_POST["lname"];
$street=$_POST["street"];
$city=$_POST["city"];
$pin=$_POST["pin"];
$mail=$_POST["mail"];
$phone=$_POST["phone"];
$password=$_POST["pwd"];
$passconf=$_POST["pwdc"];
$mail_check="SELECT mail FROM userdata WHERE mail='$mail'";
$res=mysqli_query($db,$mail_check);
$row=mysqli_fetch_array($res,MYSQLI_ASSOC);
if(mysqli_num_rows($res)==1) {
$msg= "This email is already registered. Please login or use another email ID.";
}
else if (empty($fname) ||empty($lname) ||empty($street) ||empty($city) ||empty($pin) ||empty($mail) ||empty($phone) ||empty($password) ||empty($passconf)) {
//Checks for any blank field.
$msg="Cannot leave the field blank!";
}
elseif($password!=$passconf) {
//Checks for matching password.
$msg= "Passwords don't match!";
}
else {
$query=mysqli_query($db,"INSERT INTO userdata(userid, fname, lname, street, city, pin, mail, phone, password) VALUES('$userid','$fname','$lname','$street','$city','$pin','$mail','$phone','$password')");
if($query) {
$msg= "Thank you! You are now registered.";
//Or give another link to redirect.
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Signup</title>
<link rel="stylesheet" type="text/css" href="signupcss.css">
<link rel="icon" href="pageicon.png">
</head>
<body style="background:#212934;">
<div class="search_box" style="background:#85A25B;">
<form name="signup" action="index.php" method="post">
<h1 style="text-align:center;">SIGNUP</h1>
<h6 style="margin-left:36px;" >The <span style="color:#D62F0B">*</span> indicates required field.</h6>
<hr class="fieldlen_long" style="text-align:center;">
<ul class="form_style">
<br><list><label>FULL NAME<span class="required"> *</span></label>
<input type="text" name="fname" class="fieldlen_split" placeholder="First Name" />
<input type="text" name="lname" class="fieldlen_split" placeholder="Last Name" /></list><br>
<list><label>ADDRESS</label></list>
<list><input type="text" name="street" class="fieldlen_long" placeholder="Street" /></list>
<list><input type="text" name="city" class="fieldlen_split" placeholder="City" />
<input type="text" name="pin" maxlength="6" placeholder="Pincode" /></list>
<br>
<list><label>EMAIL ID <span class="required"> *</span></label>
<input type="email" name="mail" class="fieldlen_mail" placeholder="Email ID" /></list>
<br>
<list><label>PHONE<span class="required"> *</span></label><input type="text" name="phone" maxlength="10" placeholder="Phone" /></list>
<br>
<list><label>PASSWORD<span class="required"> *</span></label><input type="password" name="pwd" class="field-divided" placeholder="Password"/> <input type="password" name="pwdc" class="field-divided" placeholder="Confirm Password"/></list>
<br>
<div class="submitbutton">
<list><input type="submit" name="sub_btn" value="SUBMIT" /></list></div>
</ul>
</form>
</div>
I'm having trouble with data not saving to a table, I've got the following code in a file called Register.php, I have enabled error_reporting to see any errors, but none are showing. My database is called Registration and the table is called users and is being hosted on GoDaddy
<?php
require 'Connections/Connections.php';
?>
<?php
if(isset($_Post['Register'])) {
session_start();
$FName = $_POST['First_Name'];
$LName = $_POST['Last_Name'];
$Username = $_POST['Username'];
$Email = $_POST['Email'];
$PW = $_POST['Password'];
$sql = $con->query("INSERT INTO users (Fname, Lname, Username, Email, Password)Values('{$Fname}', '{$LName}', '{$Username}', '{$Email}', '{$PW}')");
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<link href="Style/Master.css" rel="stylesheet" type="text/css" />
<title>Register</title>
</head>
<body>
<form action="" method="post" name="RegisterForm" id="RegisterForm">
<div class="FormElement">
<input name="First_Name" type="text" required="required" class="TField" id="First_Name" placeholder="First Name">
</div>
<div class="FormElement">
<input name="Last_Name" type="text" required="required" class="TField" id="Last_Name" placeholder="Last Name">
</div>
<div class="FormElement">
<input name="Username" type="text" required="required" class="TField" id="Username" placeholder="Username">
</div>
<div class="FormElement">
<input name="Email" type="text" required="required" class="TField" id="Email" placeholder="Email">
</div>
<div class="FormElement">
<input name="Password" type="text" required="required" class="TField" id="Password" placeholder="Password">
</div>
<div class="FormElement">
<input name="Register" type="submit" class="button" id="Register" placeholder="Register">
</div>
</body>
</html>
This is the code in the Connections.php just without my real username and password
<?php
$con = mysql_connect("localhost", "USERNAME", "PASSWORD", "Registration");
?>
Any help would be very much appreciated!
You need to edit the Connection.php
mysql_connect("localhost", "USERNAME", "PASSWORD");
mysql_select_db("Registration"); //your data base name
And also you have to edit
mysql_query("INSERT INTO users (Fname, Lname, Username, Email, Password)Values('$Fname', '$LName', '$Username', '$Email', '$PW')") or die(mysql_error());
Try this
mysql_query("INSERT INTO users (Fname, Lname, Username, Email, Password)Values('$Fname', '$LName', '$Username', '$Email', '$PW')") or die(mysql_error());
Hey my PHP code for my register form isn't working with some of the var's but everything looks fine to me.
Username, password and email works fine, I also have another page that adds data to the database and that works perfect with the same code as this.
Thanks in advance.
<?php if (isset($_POST['submit'])){
$username = $_POST['username'];
$sql = "SELECT * FROM members WHERE username='$username'";
echo $sql;
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$num = mysqli_num_rows($result);
if ($num >= 1){
$_SESSION['userError']='Username Already Taken';
}else{
$username = $_POST['username'];
$password = $_POST['password'];
$password = hash('sha256', $password);
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$address = $_POST['address'];
$contact = $_POST['contact'];
$sql2 = "INSERT INTO members(username, password, fname, lname, email, address, contact) VALUES ('$username', '$password', '$fname', '$lname', '$email', '$address', '$contact')";
mysqli_query($con, $sql) or die(mysqli_error($con));
echo $sql2;
$_SESSION['message']="Account Added Successfully"; }} ?>
And here is the form that it links too.
<form class="pure-form reg-form" action="" method="post">
<fieldset class="pure-group" style="float:left">
<input type="text" class="pure-input-1" name="username" value="" placeholder="Username" autofocus/>
<input type="password" class="pure-input-1" name="password" value="" pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{4,}" placeholder="Password"/>
<input type="text" class="pure-input-1" name"fname" value="" placeholder="First Name"/>
<input type="text" class="pure-input-1" name"lname" value="" placeholder="Last Name"/>
</fieldset>
<fieldset class="pure-group" style="float:right">
<input type="email" class="pure-input-1" name="email" value="" placeholder="E-mail"/>
<input type="text" class="pure-input-1" name"address" value="" placeholder="Address"/>
<input type="text" class="pure-input-1" name"contact" value="" placeholder="Contact"/>
<input type="submit" class="pure-button pure-input-1 pure-button-primary" name="submit" value="Submit">
</fieldset>
</form>
Check Your HTML you are missing = from some of the input tags
Your Code :
<form class="pure-form reg-form" action="" method="post">
<fieldset class="pure-group" style="float:left">
<input type="text" class="pure-input-1" name="username" value="" placeholder="Username" autofocus/>
<input type="password" class="pure-input-1" name="password" value="" pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{4,}" placeholder="Password"/>
<input type="text" class="pure-input-1" name"fname" value="" placeholder="First Name"/>//missing =
<input type="text" class="pure-input-1" name"lname" value="" placeholder="Last Name"/>//missing =
</fieldset>
<fieldset class="pure-group" style="float:right">
<input type="email" class="pure-input-1" name="email" value="" placeholder="E-mail"/>
<input type="text" class="pure-input-1" name"address" value="" placeholder="Address"/> //missing =
<input type="text" class="pure-input-1" name"contact" value="" placeholder="Contact"/> //missing =
<input type="submit" class="pure-button pure-input-1 pure-button-primary" name="submit" value="Submit">
</fieldset>
</form>
And Also check your PHP code your are making query in sql2 and executing sql
change :
$sql2 = "INSERT INTO members(username, password, fname, lname, email, address, contact) VALUES ('$username', '$password', '$fname', '$lname', '$email', '$address', '$contact')";
mysqli_query($con, $sql) or die(mysqli_error($con));
to :
$sql2 = "INSERT INTO members(username, password, fname, lname, email, address, contact) VALUES ('$username', '$password', '$fname', '$lname', '$email', '$address', '$contact')";
mysqli_query($con, $sql2) or die(mysqli_error($con));
you write query but not execute it.
line 22
mysqli_query($con, $sql) or die(mysqli_error($con));
change $sql to $sql2
I have a user registration MySql database that I want users to enter data into using a form. This table has a column that I've prepopulated with unique codes. The idea is that each user gets a unique code when they register.
First, is there a better way to handle this than the setup I have?
Second, if there isn't a better way, how do I update just one row of data for each form submission?
Here's what I'm currently doing:
Register Form
<form action="confirm.php" method="post" onsubmit="return Validate();">
<p>First Name: <input type="text" name="firstname" /></p>
<p>Last Name: <input type="text" name="lastname" /></p>
<p>Address: <input type="text" name="address" /></p>
<p>City: <input type="text" name="city" /></p>
<p>State: <input type="text" name="state" /></p>
<p>Zip Code: <input type="text" name="zipcode" /></p>
<p>Phone Number: <input type="text" name="phone" /></p>
<p>Email: <input type="text" name="email" /></p>
<p><input type="submit" /></p>
</form>
Confirm.php
<form action="register.php" method="post" onsubmit="return Validate();">
<p>First Name: <input type="text" name="firstname" id="firstname" size="36" value="<?php echo $_POST['firstname']; ?>"> </p>
<p>Last Name: <input type="text" name="lastname" id="lastname" size="36" value="<?php echo $_POST['lastname']; ?>"> </p>
<p>Address: <input type="text" name="address" id="address" size="36" value="<?php echo $_POST['address']; ?>"> </p>
<p>City: <input type="text" name="city" id="city" size="36" value="<?php echo $_POST['city']; ?>"> </p>
<p>State: <input type="text" name="state" id="state" size="36" value="<?php echo $_POST['state']; ?>"> </p>
<p>Zip Code: <input type="text" name="zipcode" id="zipcode" size="36" value="<?php echo $_POST['zipcode']; ?>"> </p>
<p>Phone Number: <input type="text" name="phone" id="phone" size="36" value="<?php echo $_POST['phone']; ?>"> </p>
<p>Email: <input type="text" name="email" id="email" size="36" value="<?php echo $_POST['email']; ?>"> </p>
<input type="hidden" name="redirect_values" value="true">
<input type="submit" name="confirm" value="Confirm Information">
<input type="button" name="return" value="Change Information" onClick="javascript: window.history.back(-1)";>
</form>
Register.php
<?php ob_start();
$url = 'registercomplete.php';
require("register_dbinfo.php");
$con=mysqli_connect($host,$username,$password,$database);
if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: ".mysqli_connect_error();}
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$city = mysqli_real_escape_string($con, $_POST['city']);
$state = mysqli_real_escape_string($con, $_POST['state']);
$zipcode = mysqli_real_escape_string($con, $_POST['zipcode']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$sql="INSERT INTO users (id, firstname, lastname, address, city, state, zipcode, phone, email)VALUES ('','$firstname', '$lastname', '$address', '$city', '$state', '$zipcode', '$phone', '$email')";
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));}
mysqli_close($con);
while (ob_get_status())
{ob_end_clean();}
header( "Location: $url" );
?>
Database columns:
id|promo|tickets|firstname|lastname|address|city|state|zipcode|phone|email
I'm not sure if I've understood your question.
If id is INT, make the attribute UNIQUE with A_I (Auto Increment).
If you need a unique string for promo, you may want to use an hashing function (like SHA-256) in order to avoid string collisions.
In addition to your question I suggest you to not concatenate escaped string since this technique will not prevent SQL injection. You should use prepared statements and stored procedures.