storing data to mysql database using php - php

my code wont insert into the student table I dont know whats wrong could you guys possibly help? I tried to print and error message in each line to find out where the error was but I got no errors so I'm confused
<?PHP
if(isset($_POST['submit'])) {
$link = mysql_connect('localhost','root','');
if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}
$sql = "INSERT INTO student (FirstName, LastName, UserName,Password, confirmP ,phoneNum,Email) VALUES ('$_POST[FN]','$_POST[LN]','$_POST[userName]','$_POST[password]','$_POST[confirmPass]','$_POST[number]','$_POST[email]') ";
mysql_query($sql,$link);
mysql_close($link);
}
?>
and here's the html form:
<form method="post" action="Register.php">
<div class="contact-to">
<P> <input name="FN" type="text" class="text" value="First name" >
<input name="LN" type="text" class="text" value="Last name" style="margin-left: 10px">
<input name="userName" type="text" class="text" value="username" style="margin-left: 10px">
</div>
<div class="contact-to">
<input name="password" type="text" class="text" value="Password" style="margin-left: 10px">
<input name="confirmPass" type="text" class="text" value="Confirm password" style="margin-left: 10px">
<input name="number" type="text" class="text" value="Phone Number" >
<input name="email" type="text" class="text" value="email" >
</div>
<div> <input value="submit" type="submit" class="submit"> </div>
</form>

If you're trying to validate $_POST['submit'] after user clicks the button, you should include attribute name inside your input submit tag like this:
<input value="submit" type="submit" name="submit" class="submit">
And then you can check $_POST:
if(isset($_POST['submit'])) {
// your code
}
PHP will always return false in your condition without a attribute name...
Also, consider using mysqli with prepared statements, or PDO with prepared statements, they're much safer.

Change your sql insert query code to this:
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
function debug($data){
echo '<pre>';
print_r($data);
echo '</pre>';
}
function getConnected($host,$user,$pass,$db) {
$mysqli = new mysqli($host, $user, $pass, $db);
if($mysqli->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $mysqli;
}
if(isset($_POST['submit'])) {
debug('Connecting database...');
$link = getConnected('localhost','root','', 'stackoverflow');
debug('Database connected...');
$sql = "
INSERT INTO `student` (`FirstName`, `LastName`, `UserName`, `Password`, `confirmP`, `phoneNum`, `Email`)
VALUES (
'{$_POST['FN']}',
'{$_POST['LN']}',
'{$_POST['userName']}',
'{$_POST['password']}',
'{$_POST['confirmPass']}',
'{$_POST['number']}',
'{$_POST['email']}'
)";
if (!mysqli_query($link, $sql)) {
debug("Errormessage: ". mysqli_error($link)."\n");
}else{
debug('Query executed successfully...');
}
mysqli_close($link);
}
?>
Also change your form submit button code to this:
<div> <input name="submit" value="submit" type="submit" class="submit"> </div>

Related

How to fix MySQL and PHP in WAMP not inserting data?

I’m using WAMP as my php server, I’m using PHP-AdminPage for my databse (usin MySql), Everything is working, my database is connected . except for when I want to display the data, rows are created but they’re EMPTY! even tho I didn’t leave them empty. I don’t understand, I’m so confused.
PHP Code:
<html>
<body>
<?php
$conn = new mysqli('localhost','root','');
//check connection
if ($conn-> connect_error) {
die("Connection failed: ".$conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn,"catcafe");
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";
if($conn->query($sql) === true) {
echo "New record created";
}
else{
echo "Error, check values".$sql."<br>".$conn->error;
}
mysqli_close($conn);
?>
</body>
</html>
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";
MySQL keeps telling me that the error is here! but I dont know what it is, the columns have the exact same name as the ones in the database.
MY FORM /HTML CODE:
<form action="InsertReservation.php">
<label for="fname">First Name </label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
<br>
<label for="lname">Last Name </label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
<br>
<label for="Email">Email </label><br>
<input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
<br>
<label for="Mobile">Mobile Number </label><br>
<input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
<br>
<label for="Time">Time </label><br>
<input type="time" id="Time" name="Time" min="8:00" required style="width:600px; font-size: 30px;">
<br>
<input type="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')">
<button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
</form>
MySQL Code
SELECT * FROM `customer` ORDER BY `customer`.`FName` ASC
I want it to insert the data, but it keeps inserting empty data.
Validate your input data all the time.
Use prepared statments for user input
Time is a mysql reserved word so use backticks `` around it.
$conn = new mysqli('localhost', 'root', '');
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn, "catcafe");
//insert query
$sql = "INSERT INTO customer(FName,LName,Email,PhNum,`Time`) VALUES(?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss", $_POST['firstname'], $_POST['lastname'], $_POST['Email'], $_POST['Mobile'], $_POST['Time']);
if ($stmt->execute()) {
echo "New record created";
} else {
echo "Error, check values " . $stmt->error;
}
mysqli_close($conn);
?>
Hello Please use this code on a single file it's working fine.
Thanks
$conn = new mysqli('localhost', 'root', '');
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn, "catcafe");
if($conn){
//insert query
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){
$sql = "INSERT INTO customer(FName,LName,Email,PhNum,Time)
VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['Email']."','".$_POST['Mobile']."','".$_POST['Time']."')";
if($conn->query($sql) === true) {
echo "New record created";
}
else{
echo "Error, check values".$sql."<br>".$conn->error;
}
}
mysqli_close($conn);
}
<form method="post">
<label for="fname">First Name </label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
<br>
<label for="lname">Last Name </label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
<br>
<label for="Email">Email </label><br>
<input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
<br>
<label for="Mobile">Mobile Number </label><br>
<input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
<br>
<label for="Time">Time </label><br>
<input type="time" id="Time" name="Time" min="8:00" style="width:600px; font-size: 30px;">
<br>
<input type="submit" name="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')">
<button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
</form>

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"]);

value from formnot entering into database in php

code is not coming into signup_ad.php
signup_ad.php:
<? php
mysql_connect("localhost","root","") or die("not connected");
mysql_select_db("sample") or die("no db found");
if(isset($_POST)
{
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$query= "insert into signup(fname,lname) values('$fname','$lname')";
$result=mysql_query($query);
if (!$result) {
$message='invalid query';
die($message); }
}
?>
value is not entering into db..and also not giving any error. Do i need to include my signup_ad.php file ?
signup.php:
<form action="signup_ad.php" method="post" >
<label>
First Name
</label>
<input type="text" required autocomplete="off" name="fname" />
<label>
Last Name
</label>
<input type="text" required autocomplete="off" name="lname"/>
<button type="submit" class="button button-block" name="btn-signup" value="submit" action="login.php" />Get Started</button>
You forget a quotation before semicolon:
$query= "insert into signup(fname,lname) values('$fname','$lname')";

form didnt working with mysql

this is the html
<form action="addadd.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>
First Name<br>
<label for="firstname"></label>
<input type="text" name="firstname" id="firstname" />
</p>
<p>
Last Name<br>
<label for="lastname"></label>
<input type="text" name="lastname" id="lastname" />
</p>
<p>
Mobile<br>
<label for="mobile"></label>
<input type="text" name="mobile" id="mobile" />
</p>
<p>
Email<br>
<label for="email"></label>
<input type="text" name="email" id="email" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
<input type="reset" name="button3" id="button3" value="Reset" />
</p>
</form>
This is the php
database connection
<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("connection to database failed".mysql_error());
}
$dataselect = mysql_select_db("qoot",$con);
if(!$dataselect)
{
die("Database namelist not selected".mysql_error());
}
?>
<?php
$unm = $_SESSION['name'];
$fname=$_POST['firstname'];
$lname=$_POST['lastname'];
$ema=$_POST['email'];
$mob=$_POST['mobile'];
?>
<?php
$qry=mysql_query("INSERT INTO address( firstname, lastname, mobile, email)VALUES('$fname',$lname','$ema','$mob')", $con);
?>
Now the problem is that this is inserting nothing in to my database.
What else can i try in order to check where things go wrong?
updated database connection details
Place your tablename address in backticks
Using mysqli_
<?php
/start session
session_start();
//establish connection
$con = mysqli_connect("localhost","root","","qoot");
if(!$con)
{
die("connection to database failed".mysqli_error($con));
}
//read the values from form
$unm = $_SESSION['name'];
$fname=$_POST['firstname'];
$lname=$_POST['lastname'];
$ema=$_POST['email'];
$mob=$_POST['mobile'];
?>
<?php
//insert to database
$qry=mysqli_query($con,"INSERT INTO `address` ( firstname, lastname, mobile, email)VALUES('$fname',$lname','$ema','$mob')") or die(mysqli_error($con));
?>
P.S I'd say you are at risk of mysql injection, check here How can I prevent SQL injection in PHP?. You should really use prepared statements to avoid any risk.

Adding a record from html form to oracle database

I have a HTML form that I want to add a record to an Oracle database when somebody hits submit. The table is hooking up somewhat, the problem is that when somebody submits their information they come up as NULL values in the database table.
HTML:
<form name="myForm" action="/Add_File.php" onsubmit="return validateForm()" method="post"><!--Form-->
<fieldset>
<label class ="label1" for "name">First Name: </label>
<input type="text" name="fname"><br />
<label class ="label1" for "name">Surname: </label><input type="text" name="sname"><br/>
<label for="email">E-mail Address: </label><input type="text" name="email"><br />
<label for "address">Address: </label> <input type="text" name="address"><br />
<label class="label" for "Password">Select a Password: </label> <input type="password" name="pass"><br />
<label class="label" for "Password">Retype-Password:</label> <input type="password" name="pass2"><br />
</fieldset>
<fieldset><input class="button" type="submit" onclick="message()" value="Submit"/>
<input class="button" type="reset" value="reset form" onclick="myFunction()"/>
</fieldset>
</form>
PHP code:
$dbuser = "scott";
$dbpassword = "tiger";
$db = "orabis";
$conn = oci_connect($dbuser,$dbpassword,$db);
if (!$conn){
echo "Connection error";
exit;
}
$fname=$_POST['First_Name'];
$sname=$_POST['Surname'];
$email=$_POST['Email_Address'];
$address=$_POST['Address'];
$selpass=$_POST['Select_A_Password'];
$confirm=$_POST['Retype_Password'];
$sql = "INSERT INTO Become_A_Member_110385461(First_Name,Surname,Email_Address,Address,Select_A_Password,Retype_Password)
VALUES ('".$fname."','".$sname."', '".$email."', '".$address."','".$selpass."', '".$confirm."')";
$stmt = oci_parse($conn, $sql);
if (!$stmt) {
echo "Error in preparing the statement";
exit;
}
oci_execute($stmt, OCI_DEFAULT);
print "Record Inserted";
oci_commit($conn);
oci_close($conn);
change like this
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$email=$_POST['email'];
$address=$_POST['address'];
$selpass=$_POST['pass'];
$confirm=$_POST['pass2'];

Categories