insert data into database using oops and mysqli - php

Here is my db connection file. (db_configenter code here.php)
<?php
$con = mysqli_connect("localhost","root","","oop");
if(mysqli_connect_errno())
{
echo "failed to connect to mysql:" . mysqli_connect_error();
}
?>
Here is my user.class.php code
<?php
include "db_config.php";
class User
{
public function registration($fname,$lname,$username,$email,$password)
{
$sql = $con->query("INSERT INTO users_registration(fname,lname,username,email,password)
VALUES('$fname','$lname','$username','$email','$password',)");
return $sql;
}
}
?>
And finally my registrtaion.php file is here
<?php
include "class.user.php";
$user = new User();
if (isset($_POST['submit']))
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$user->registration($fname,$lname,$username,$email,$password);
echo "registration success";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div class="wrapper"><!--wrapper start here-->
<form action="" method="POST">
<input type="text" name="fname" placeholder="First name"/><br>
<input type="text" name="lname" placeholder="Lastst name"/><br>
<input type="text" name="username" placeholder="User name"/><br>
<input type="text" name="email" placeholder="Email id"/><br>
<input type="text" name="password" placeholder="Password"/><br>
<input type="text" name="cpassword" placeholder="Confirm Password"/><br>
<input type="button" value="submit" name="submit" class="btn"/>
</form>
</div><!--wrapper ends here-->
</body>
</html>
i am not able to insert data into database, when i submit button nothing happend.Plz help me to solve the prob.i am new on php.
Also want to add an image in the same field.
Thanks in advance.

You have an unwanted , in your query.
$sql = $con->query("INSERT INTO users_registration(fname,lname,username,email,password)
VALUES('$fname','$lname','$username','$email','$password',)");
^
Also, to submit an HTML form, the button type should be submit. Change
<input type="button" value="submit" name="submit" class="btn"/>
to
<input type="submit" value="submit" name="submit" class="btn"/>

<?php
class dbins
{
function insert_data($name,$age)``
{
$runn = mysqli_connect("localhost","root","","oop");
$ins = "INSERT into ajax(name,age) values('$name','$age')";
$run = mysqli_qyery($runn,$ins);
return $run;
}
}
?>
<form name="insert" method="POST">
<table align="center">
<thead>
<h1 align="center">Simple Insert Use OOP</h1>
<tr>
<th>Name</th>
<td><input type="text" name="name" required="" placeholder="Enter Your Name"></td>
</tr>
<tr>
<th>Age</th>
<td><input type="text" name="age" required="" placeholder="Enter Your Age"></td>
</tr>
<tr align="center">
<td><input type="submit" name="insert" value="Insert" ></td>
</tr>
</thead>
</table>
</form>
<?php
$con = new dbins();
if(isset($_POST['insert']))
{
$name = $_POST['name'];
$age = $_POST['age'];
$con->insert_data($name,$age);
}
?>

Related

Cannot find the page/object after click submit the form

I followed a online tutorial to connect my php code with MySQL. After I click the submit button, it said it cannot find the object (page). I did not see any code in my code? Any idea for me how to debug this code?
<?php
$user = 'root';
$pass = 'xxxxxx';
$db = 'testDB';
$db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");
echo"Great work!";
$id ="";
$fname="";
$midname="";
$lname="";
function getPosts()
{
$posts =array();
$posts[0]=$_POST['Contact_ID'];
$posts[1]=$_POST['first_name'];
$posts[2]=$_POST['middle_name'];
$posts[3]=$_POST['last_name'];
return $posts;
}
//search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query="select * from Contact where contact_ID =$data[0]";
$search_Result=mysql_query($db, $search_Query);
if($search_Result)
{
if(mysql_num_rows($search_Result))
{
while($row=mysql_fetch_array($search_Result))
{
$id =$row['contact_ID'];
$fname =$row['first_name'];
$midname =$row['middle_name'];
$lname =$row['last_name_name'];
}
}
else
{
echo"No data for this Id";
}
}
else
echo"Result error";
}
?>
<!DOCTYPE Html>
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<form action="php_insert_update_delete_search.php" method="post">
<input type="number", name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text", name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text", name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text", name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
</body>
</html>
remove php_insert_update_delete_search.php from the action of your form. The form is being redirected to php_insert_update_delete_search.php page when you click search button
<form action="" method="post">
<input type="number" name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text" name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text" name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text" name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
See You Have applied an action in your form tag . This action means that when ever a user will click on any button related to that form he or she will be redirected to that page (mentioned in action ) . So u need to make a page php_insert_update_delete_search.php and has to apply logic there for inserting the data .
If your wanting the form to go to another page to run code to process what you need
<form action="pageyouneed.php" method="post">
if you want to use the current page to parse your code:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
$_SERVER["PHP_SELF"]; // Will return current document to parse code.

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>

How to connect page between signin,signup and profile in php

i'm still newbie in php, i want to connect this three page. I want to know how to tansfer data from signup to pfofile and also the sign in page. I tried to use tag but not working.
page=signup
<body>
<?php
// define variables and set to empty values
$email = $username = $name = $contact = $birthday = $gender = $address = $password = $payment ="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = test_input($_POST["email"]);
$username = test_input($_POST["username"]);
$name = test_input($_POST["name"]);
$contact = test_input($_POST["contact"]);
$birthday = test_input($_POST["birthday"]);
$gender = test_input($_POST["gender"]);
$address = test_input($_POST["address"]);
$password = test_input($_POST["password"]);
$payment = test_input($_POST["payment"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="login">
<form id="registeration" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<fieldset>
<legend><h1>Registeration</h1></legend>
<p>Email:<br/>
<input type="email" name="email"></p>
<p>Username:<br/>
<input type="text" name="username" />
</p>
<p>Your Name:<br/>
<input type="text" name="name"/></p>
<p>Contact Number:<br/>
<input type="text" name="contact"/></p>
<p>Birthday:<br/>
<input type="date" name="birthday">
<p>Gender:<br/>
<input type="radio" name="gender" value="male"/>Male
<input type="radio" name="gender" value="female"/>Female
</p>
<p>Address: <br/>
<textarea name="address" cols="50"></textarea></p>
<p>Password:<br/>
<input name="password" type="password" size="50"/></p>
</p>
<h2>Choose payment method:<br/></h2>
<p><input type="radio" name="payment" value="cod"/>CASH ON DELIVERY</p>
<p><input type="radio" name="payment" value="Online Banking"/>ONLINE BANKING : CIMB Clicks<br/>
<p><input type="radio" name="payment" value="Online Banking"/>ONLINE BANKING : Maybank<br/>
<p><input type="radio" name="payment" value="Online Banking"/>ONLINE BANKING : BSN<br/>
<p><input type="radio" name="payment" value="atm"/>ATM</p>
<p><button type="submit" value="submit">Submit</button><span> <span><span><span><span>
<button type="reset" value="cancel">Cancel</button></p>
</fieldset>
</form>
</div>
echo $email;
echo "<br>";
echo $username;
echo "<br>";
echo $name;
echo "<br>";
echo $contact;
echo "<br>";
echo $birthday;
echo "<br>";
echo $gender;
echo "<br>";
echo $address;
echo "<br>";
echo $password;
echo "<br>";
echo $payment;
?>
</body>
page=signin
<body>
<div id="login">
<fieldset>
<legend><h1>Login</h1></legend>
<form id="signin" method="POST">
<p>Username:<br/>
<input type="text" name="username"/></p>
<p>Password:<br/>
<input type="password" name="password"/></p>
<p><button type="submit" value="submit">Submit</button><span>
<button type="reset" value="cancel">Cancel</button></p>
</form>
</fieldset>
<!-- end .content --></div>
<?php
error_reporting(0);
if (!empty ($_POST)){
if ($_POST ["username"] == NULL){
echo "Please insert your username!";}
else{
$strusername=$_POST["username"];
echo "<p>$strusername</p>";
}}
if (!empty ($_POST)){
if ($_POST ["password"] == NULL){
echo "Please insert the password!";}
else {
$strpassword=$_POST["password"];
echo "<p>$strpassword</p>";
}}
?>
</body>
page=profile
<body>
<div class="container">
<div class="content">
<table>
<td colspan="5"><b>PROFILE</b></td>
<tr>
<td><div align="left">Username:</div></td><td><?php echo $username ?></td></tr>
<td><div align="left">Name:</div></td><td><?php echo $name ?></td></tr>
<td><div align="left">Username:</div></td><td><?php echo $username ?></td></tr>
<td><div align="left">Birthday:</div></td><td><?php echo $birthday ?></td></tr>
<td><div align="left">Contact Number:</div></td><td><?php echo $contact ?></td></tr>
<td><div align="left">Gender:</div></td><td><?php echo $gender ?></td></tr>
<td><div align="left">Address:</div></td><td><?php echo $address ?></td></tr>
<td><div align="left">My payment Method:</div></td><td><?php echo $payment ?></td> </tr>
<tr>
<td><div align="left"><button type="submit" value="Edit Profile" >Edit Profile</button></div> </td>
</tr>
</table>
<!-- end .content --></div>
<!-- end .container --></div>
</body>
Well, Try to use the post and get.
The PHP superglobals $_GET and $_POST are used to collect form-data.The example below displays a simple HTML form with two input fields and a submit button:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.
To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
The output could be something like this:
Welcome John
Your email address is john.doe#example.com
The same result could also be achieved using the HTTP GET method:
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
and "welcome_get.php" looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
I Suggest you to follow this tutorial here
I hope it will help you, best regards
Use $_SESSION
In every page start with <?php session_start(); ?>
then store $_SESSION['email'] = test_input($_POST["email"]);
Then in any page you can use echo $_SESSION['email'];
You should use SESSION for simple login functionality....
refer this tutorial about basic login and functionality

Error in modifying mysql database using netbeans

I'm working on a php code that connects to mysql database using beans.
I have written this code for inserting record into the database using a form.
However, the recode cannot be added to the database, and I dont know why.
the same problem happens with the update..
Please anybody can check the code and tell me where is the problem.
<?php
if(isset($_POST['submitted'])) {
$dbhost='localhost:8888';
$dbuser='root';
$dbpass='root';
$dbname='clients';
$conn=mysql_connect($dbhost,$dbuser,$dbpass,$dbname);
$cardnum = $_POST['cardnum'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$idnum = $_POST['idnum'];
$mobnum = $_POST['mobnum'];
$visit = $_POST['visit'];
$sqlinsert = "INSERT INTO clients (cardnum, fname, lname, idnum, mobnum, visit)
VALUES ('$cardnum', '$fname', '$lname', '$idnum', '$mobnum', '$visit')";
if(!mysqli_query($conn, $sqlinsert)) {
die("Error inserting new record");
}
$newrecord = "One record added successfuly";
}
?>
<html>
<head>
<title>
<b>Add New Client</b>
</title>
</head>
<body bgcolor="#F0FFFF">
<br/> <br/> <br/>
<font size="4" face="WildWest" color="#4EE2EC"><b>Insert Data for the Client</b></font>
<br/>
<form method="post" action="Add.php">
<input type="hidden" name="submitted" value="true"/>
<fieldset>
<lable><font color="#48CCCD"> Card Number: </font><input type="text" name="cardnum" size="30"/> </lable><br/>
<lable><font color="#48CCCD"> First Name: </font><input type="text" name="fname" size="30"/> </lable><br/>
<lable><font color="#48CCCD"> Last Name: </font><input type="text" name="lname" size="30"/> </lable><br/>
<lable><font color="#48CCCD">ID Number: </font><input type="text" name="idnum" size="30"/> </lable><br/>
<lable><font color="#48CCCD"> Mobile Number: </font><input type="text" name="mobnum" size="30"/> </lable><br/>
<lable><font color="#48CCCD"> Visits:</font><input type="text" name="visit" size="30"/> </lable><br/>
</fieldset>
<br/>
<input type="submit" value="Add" aligh="right"/>
<?php
echo $newrecord
?>
</form>
</body>
</html>

Clear html text fields

I have an HTML form for submitting and retrieves data from MySQL database with two buttons, "Save/Submit" and "New/Reset"
It fetch data correctly from MySQL database but when I click on New/Reset button for new contact entry it couldn't clear forms text fields. My HTML and PHP codes are as under:
<?php
//Database Connection file.
include'connect.php';
$sql = mysql_query("SELECT * FROM contact_list WHERE id='1'");
While($result = mysql_fetch_assoc($sql)){
$fname = $result['fname'];
$lname = $result['lname'];
$email = $result['email'];
$contact = $result['contact'];
}
if(isset($_POST['fname'])&&isset($_POST['lname'])&&isset($_POST['email'])&&
isset($_POST['contact'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$contact = $_POST['contact'];
if($sql = mysql_query("INSERT INTO contact_list VALUES ('', '$fname', '$lname',
'$email', '$contact')")){
echo'Contact Save Successfully.';
}else{
echo'Contact not save.';
}
}
?>
<html>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>">
//Clean all fields of forms for new entry.
<input type="reset" value="New">
//Save or submit form data into mysql database
<input type="submit" value="Save">
</form>
</html>
You can do this easily by using jQuery
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#btnReset").click(function(){
$("#fname").val("");
$("#lname").val("");
$("#email").val("");
$("#contact").val("");
});
});
</script>
</head>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>" id="fname">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>" id="lname">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>" id="email">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>" id="contact">
//Clean all fields of forms for new entry.
<input type="reset" value="New" id="btnReset">
//Save or submit form data into mysql database
<input type="submit" value="Save" id="btnSave">
</form>
</html>

Categories