PHP connect mysql showing undefined variable - php

I have a problem, while am connecting phpmyadmin database from my php.
The below code is for form,
<div id="wb_element_instance53" class="wb_element">
<form class="wb_form wb_mob_form" method="post"><input type="hidden" name="wb_form_id" value="18498be5"><textarea name="message" rows="3" cols="20" class="hpc"></textarea>
<table>
<tr>
<th class="wb-stl-normal">Name </th>
<td><input type="hidden" name="wb_input_0" value="Name"><input class="form-control form-field" type="text" value="" name="wb_input_0" required="required"></td>
</tr>
<tr>
<th class="wb-stl-normal">Email </th>
<td><input type="hidden" name="wb_input_1" value="E-mail"><input class="form-control form-field" type="text" value="" name="wb_input_1" required="required"></td>
</tr>
<tr class="area-row">
<th class="wb-stl-normal">Message </th>
<td><input type="hidden" name="wb_input_2" value="Message"><textarea class="form-control form-field form-area-field" rows="3" cols="20" name="wb_input_2" required="required"></textarea></td>
</tr>
<tr class="form-footer">
<td colspan="2"><button type="submit" class="btn btn-default">Submit</button></td>
</tr>
</table>
</form>
<script type="text/javascript">
Then, i tried to connect phpmyadmin database using php code below,
<?php
/*
$connect=mysqli_connect('localhost','root','','Contact_db') ;
if(mysqli_connect_errno($connect))
{
echo 'Failed to connect';
}
// create a variable
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";
//Execute the query
mysqli_query($connect,$sql);
?>
But, the above showing the error:
Notice: Undefined variable: name in this line "$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";"
Notice: Undefined variable: email in in this line "$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";"
Notice: Undefined variable: message in in this line "$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";"

What if the isset() fails??
Repair:
have a $sql only if the params are set..
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message']) ){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";
//Execute the query
mysqli_query($connect,$sql);
}

The problem is that $_POST search for name of the input. You name is wb_input_0, try this:
if (isset($_POST['wb_input_0'])) {
$name = $_POST['wb_input_0'];
}
And the same for email and message. However i would not advice to name inputs like that

try this:
$email ='';
$name ='' ;
$message ='';
print_r($_POST);//to review is all vars in form.
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}

if (isset($_POST['name'])) {
$name = $_POST['name'];
}else{
$name = '';
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}else{
$email = '';
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}else{
$message = '';
}

Do yourself a favour and prepare your statement:
$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES (?,?,?)";
$stmt = mysqli_prepare($connect, $sql);
$name="";
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
$email="";
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
$message="";
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
mysqli_stmt_bind_param($stmt,"sss",$name,$email,$message);
mysqli_stmt_execute($stmt);
Note that your current $_POST won't have those fields because your named them differently (and twice) so you also need to fix that.

Related

html form data is not inserting in MySQL database using PHP and PDO

since i am new new here,i'm trying to be specific on my question. please tell me if you need more info.
Whenever i'm trying to submit the form it doesn't show any error regarding the code. it only show echo statement "FAILED"
i am using this loop to see if the values are submitted or not. this is working fine. it shows that all the values are submitted but these values are not inserting into database .
foreach ($_POST as $key => $value) {
echo "($key) => ($value)<br/>";
}
my html form code is :
<div class="formstyle">
<h2> Sign up </h2>
<center>
<form method = 'POST' name="form1" onSubmit="return validateForm()" action="">
<table border='0'>
<tr>
<td><LABEL for="firstname">First Name:<sup style="color:#F00">*</sup> </LABEL></td>
<td><INPUT type="text" name = "fname" id="fname" value="<?php echo $fname;?>"></td><td width="200px"><i style="color:red;" id="pointfn"></i></td>
</tr>
<tr>
<td><LABEL for="lastname">Last Name:<sup style="color:#F00">*</sup> </LABEL></td>
<td><INPUT type="text" name ="lname" id="lname" value="<?php echo $lname;?>"> </td><td width="200px"><i style="color:red;" id="pointln"></i></td>
</tr>
<tr>
<td><LABEL for="gender">Gender:<sup style="color:#F00">*</sup> </LABEL></td> <td>
<INPUT type="radio" name="gender" id="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male"> Male
<INPUT type="radio" name="gender" id="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female"> Female</td><td width="200px"> <i style="color:red;" id="pointgendr"></i></td>
</tr>
<tr>
<td><LABEL for="email">Email:<sup style="color:red;">*</sup> </LABEL></td>
<td><INPUT type="text" name = "email" id="email" value="<?php echo $email;?>"></td><td width="200px"><i style="color:red;" id="pointemail"></i></td>
</tr>
<tr>
<td><LABEL for="password">Password:<sup style="color:#F00">*</sup> </LABEL></td>
<td><INPUT type="password" name ="password" id="password" value="<?php echo $password;?>"></td><td width="200px"><i style="color:red;" id="pointpassword"></i></td>
</tr>
<tr>
<td></td><td><br/><INPUT type="submit" name = "register" value="Create Account">
<INPUT type="reset" onClick="return confirmreset()"></td>
</tr>
<tr>
<td></td><td style="font-size:12px;text-align:right;"><br/><i style="color:red;font-size:12px;align:right;" >* - Mandatory</i></td>
</tr>
</table>
</form></center>
this is the php code that inserting everything into database
require('connect.php');
$fname = $lname = $gender = $email = $password = "";
if(isset($_POST['register'])){
$stmt = $pdo->prepare('INSERT INTO user(fname,lname,gender,email,password)
VALUES (:fname, :lname, :gender, :email, :password)');
$stmt->bindValue(':fname',$fname);
$stmt->bindValue(':lname',$lname);
$stmt->bindValue(':gender',$gender);
$stmt->bindValue(':email',$email);
$passwordHash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 12));
$stmt->bindValue(':password',$passwordHash);
$stmt->execute();
$email_stmt = $pdo->prepare("SELECT email FROM user WHERE email = :email");
$email_stmt->bindParam(':email', $email);
$email_stmt->execute();
if ($email_stmt->rowCount()>0){
echo 'Email Already Exists. Use Different Email OR Login ';
} else {
//Successful Registration
echo 'Registration Successful';
}
} else {
echo 'FAILED';
}
?>
any help would be appreciated. Cheers.
you are checking if email taken after using insert
try this
if(isset($_POST['register'])){
$password = $_POST['password'];
$fname = $_POST['fname'];
$email = $_POST['email'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$passwordHash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 12));
//validate form here
$email_stmt = $pdo->prepare("SELECT email FROM user WHERE email = :email");
$email_stmt->bindParam(':email', $email);
$email_stmt->execute();
$result = $email_stmt->fetch(PDO::FETCH_ASSOC);
if (empty($result)) {
$stmt = $pdo->prepare('INSERT INTO user(fname,lname,gender,email,password)
VALUES (:fname, :lname, :gender, :email, :password)');
$stmt->bindValue(':fname',$fname);
$stmt->bindValue(':lname',$lname);
$stmt->bindValue(':gender',$gender);
$stmt->bindValue(':email',$email);
$stmt->bindValue(':password',$passwordHash);
if ($stmt->execute()) {
echo 'Registration Successful';
}
else {
echo 'FAILED';
}
}
else {
echo 'Email Already Exists. Use Different Email OR Login ';
}
}
Looks like you don't need a foreach for this form. I think you should try for example:
echo $_POST['fname'];
http://php.net/manual/en/reserved.variables.post.php
Also this name = "fname" should be this name="fname"
You must put POST'S into variables.
Try this
require('connect.php');
$fname = $lname = $gender = $email = $password = "";
if(isset($_POST['register'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$password = $_POST['password'];
$stmt = $pdo->prepare('INSERT INTO user(fname,lname,gender,email,password)
VALUES (:fname, :lname, :gender, :email, :password)');
$stmt->bindValue(':fname',$fname);
$stmt->bindValue(':lname',$lname);
$stmt->bindValue(':gender',$gender);
$stmt->bindValue(':email',$email);
$passwordHash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 12));
$stmt->bindValue(':password',$passwordHash);
$stmt->execute();
$email_stmt = $pdo->prepare("SELECT email FROM user WHERE email = :email");
$email_stmt->bindParam(':email', $email);
$email_stmt->execute();
if ($email_stmt->rowCount()>0){
echo 'Email Already Exists. Use Different Email OR Login ';
} else {
//Successful Registration
echo 'Registration Successful';
}
} else {
echo 'FAILED';
}
?>

Problems creating a php form that sends data to database

I'm new to php, trying to make this simple form but I keep finding different examples of how to do it but they're all done with mysql and I've been told to switch to mysqli.
<html>
<head>
<title>
</title>
</head>
<body>
<form action="process.php" method="post">
<table>
<tr><th>Student Details</th></tr>
<tr>
<td><label for="student_name">Student Name</label></td>
<td><input type="text" name="student_name" id="student_name"/> </td>
</tr>
<tr>
<td><label for="student_email">Student Email</label></td>
<td><input type="email" name="student_email" id="student_email"/> </td>
</tr>
<tr>
<td><label for="student_city">Student City</label></td>
<td><input type="text" name="student_city" id="student_city"/> </td>
</tr>
<tr>
<td><button name= "submit"type="submit">Submit</button></td>
</tr>
</table>
</form>
</body>
</html>
Could someone please look at this code and tell me how to:
A) Avoid the following errors:
Undefined variable: insert in C:\Users\CEO\Google
Drive\Form\process.php on line 30
Warning: mysqli_query() expects parameter 1 to be mysqli, null given
in C:\Users\CEO\Google Drive\Form\process.php on line 30
B) Apparently this form is a total security risk, what should I add to fix that?
<?php
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'college';
$conn = mysqli_connect($server, $user, $pass, $db); //Connect to Database
if(isset($_POST['submit'])){
$name = $_POST['student_name'];
$email = $_POST['student_email'];
$city = $_POST['student_city'];
if($name != "" || $email != "" || $city != ""){
$insert = "INSERT INTO students(student_name, student_email,student_contact) VALUES ('$name','$email','$city')";
$query = mysqli_query($conn,$insert);
echo "Data inserted";
}else{
echo "Failed to insert data";
}
}
if (!mysqli_query($insert, $conn)) {
die('Error: ' . mysqli_error($conn));
}
echo "1 record added";
mysqli_close($conn);
You assign to $insert inside the if block. But then you try to perform the query outside the if block. So if the if condition is not met, you'll still try to call mysqli_query(), but with an uninitialized variable. You should move that into the if.
if(isset($_POST['submit'])){
$name = $_POST['student_name'];
$email = $_POST['student_email'];
$city = $_POST['student_city'];
if($name != "" || $email != "" || $city != ""){
$insert = "INSERT INTO students(student_name, student_email, student_contact)
VALUES ('$name','$email','$city')";
if (mysqli_query($conn,$insert)) {
echo "Data inserted";
}else{
echo "Failed to insert data: " . mysqli_error($conn);
}
} else {
echo "You have to fill in name, email, or city";
}
}
But it's better to use prepared statements.
if(isset($_POST['submit'])){
$name = $_POST['student_name'];
$email = $_POST['student_email'];
$city = $_POST['student_city'];
if($name != "" || $email != "" || $city != ""){
$insert = mysqli_prepare("INSERT INTO students(student_name, student_email, student_contact)
VALUES (?, ?, ?)") or die(mysqli_error($conn));
mysqli_stmt_bind_param($insert, "sss", $name, $email, $city);
if (mysqli_stmt_execute($insert)) {
echo "Data inserted";
}else{
echo "Failed to insert data: " . mysqli_error($conn);
}
} else {
echo "You have to fill in name, email, or city";
}
}

PHP form vadation

I am have created a form and having problems validating it as I am getting a few errors when I try to preview it in the browser.
The errors
Undefined index: username in on line 269
Undefined index: email in on line 270
Undefined index: fname in on line 271
Undefined index: lname in on line 272
Undefined index: pnumber in on line 273
Undefined index: address in on line 274
Undefined index: password in on line 275
**my php scripts starts on line 35 and ends on line 131**
<!--DB connection--> (line 35)
<?php
$localhost = "localhost";
$dbuser = "student";
$dbpass = "student";
$dbname = "Curiosity_Pizza";
$connect = mysqli_connect($localhost,$dbuser,$dbpass)or die ("Could not connect to database!");;
mysqli_select_db($connect, "$dbname" );
?>
<!-- inserting form data in to DB-->
<?php
$username =$_POST['username'];
$email =$_POST['email'];
$fname =$_POST['fname'];
$lname =$_POST['lname'];
$pnumber =$_POST['pnumber'];
$address =$_POST['address'];
$password = sha1($_POST['password']);
$inssert = 'INSERT into client(username, email, fname, lname, pnumber ,address, password) VALUES ("'.$username.'","'.$email.'","'.$fname.'","'.$lname.'", "'.$pnumber.'", "'.$address.'","'.$password.'")';
mysqli_query($connect,$inssert);
?>
<!--Registration Valadation-->
<?php
//define varibles and sst to empty (w3Schhols)
$usernameErr = $emailErr = $fnameErr = $lnameErr = $pnumberErr = $addressErr = $passwordErr = "";
$username = $email = $fname = $lname = $pnumber = $address = $password = "";
if ($_SERVER["REQUEST_METHOD"]== "POST"){
$username = test_input($_POST["username"]);
$email = test_input($_POST["email"]);
$fname = test_input($_POST["fname"]);
$lname = test_input($_POST["lname"]);
$pnumber = test_input($_POST["pnumber"]);
$address = test_input($_POST["address"]);
$password = test_input($_POST["passoerd"]);
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"]== "POST"){
if (empty($_POST["userename"])){
$usernameErr = "Userename is a required field";
}else{
$username = test_input($_POST["username"]);
}
if (empty($_POST["email"])){
$emailErr = "Email is a required field";
}else{
$email = test_input($_POST["email"]);
}
if (empty($_POST["fname"])){
$fnameErr = "First Name is a required field";
}else{
$fname = test_input($_POST["fname"]);
}
if (empty($_POST["lanme"])){
$lnameErr = "Last Name is a required field";
}else{
$lname = test_input($_POST["lname"]);
}
if (empty($_POST["pnumber"])){
$pnumberErr = "Phone Number is a required field";
}else{
$pnumber = test_input($_POST["$number"]);
}
if (empty($_POST["address"])){
$addressErr = "Address is a required field";
}else{
$address = test_input($_POST["address"]);
}
if (empty($_POST["$password"])){
$passwordErr = "Password is a required field";
}else{
$password = test_input($_POST["password"]);
}
} (line 131)
**My html/form not sure if its relevant low**
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" />
<table width="600" border="0">
<tr>
<th><label for="Username">Username:</label></th>
<td><input type="text" name="username" />
<span class="error">*<?php echo $usernameErr;?></span></td>
</tr>
<tr>
<th><label for="email">email:</label></th>
<td><input type="email" name="email" />
<span class="error">*<?php echo $emailErr;?></span></td>
</tr>
<tr>
<th><label for="fname">First Name:</label></th>
<td><input type="text" name="fname" />
<span class="error">*<?php echo $fnameErr;?></span></td>
</tr>
<tr>
<th><label for="lname">Last Name:</label></th>
<td><input type="text" name="lname" />
<span class="error">*<?php echo $lnameErr;?></span></td>
</tr>
<tr>
<th><label for="pnumber">Phone Number:</label></th>
<td><input type="number" name="pnumber" />
<span class="error">*<?php echo $pnumberErr;?></span></td>
</tr>
<tr>
<th><label for="address">Address:</label></th>
<td><input type="text" name="address" />
<span class="error">*<?php echo $addressErr;?></span></td>
</tr>
<tr>
<th><label for="password">Passowrd:</label</th> <td><input type="password" name="password" />
<span class="error">*<?php echo $passwordErr;?></span></td>
</tr>
<tr>
<td><input type="submit" name="Submit"</td>
</tr>
</table>
</form>
I have tried reading the other posts on here but have not found anything that would work.
The errors means that the array does not contain the index. Make sure that your $_POST contains username... etc. Try
print_r($_POST)
And make sure it contains "username."
You can also use
array_key_exists("username", $_POST)
to make sure that $_POST contains username.
array_key_exists: http://php.net/manual/en/function.array-key-exists.php
TL;DR
Make sure $_POST contains "username" and any other index that PHP complains is undefined.
Check on the form that is posting information to make sure that the names match up with the parameters you're using.
For instance, you should have something like <input name='username'> somewhere in your form, etc.
Also, you should always do:
if( isset($_POST[INDEX]) )
{
// Stuff with $_POST[INDEX]
}
to make sure that the POST was successfully received.
Also, you can try using Fiddler:
http://www.telerik.com/fiddler
to figure out what is/isn't being posted.
Don't use $_POST['username'] as it might not contain username, first check if there is actually a value with isset isset($_POST['username']) ? $_POST['username'] : null
or better, create a function to handle input data for example:
<?php
function post($index, $default = null) {
return isset($_POST[$index]) ? $_POST[$index] : $default;
}
//Use it like that:
$username = post('username');
$email = post('email');
$fname= post('fname');
...
Edit: Added more example for better clarity.

Connect form to my SQL database?

I am trying to have this form submit to a database but I am having no luck. I am a complete newbie and based this code off another form that is already in use. Can anyone spot errors that would make it not work properly? Any help would be huge.
<?php
include("includes/captcha.php");
if (!empty($_POST['submitcontestant'])) {
$addcontestantsql = "INSERT INTO ".$config['db_dbpaper'].".contest (company, name, address, phone)";
$company = mysql_real_escape_string($_POST['company']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);
$captcha = mysql_real_escape_string($_POST['captcha']);
<?php
include("includes/captcha.php");
if (!empty($_POST['submitcontestant'])) {
$addcontestantsql = "INSERT INTO ".$config['db_dbpaper'].".contest (company, name, address, phone)";
$company = mysql_real_escape_string($_POST['company']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);
$captcha = mysql_real_escape_string($_POST['captcha']);
$addcontestantsql .= " VALUES('$company', '$name', '$email', '$phone')";
$allowed = false;
if (empty($company) && empty($name) && empty($address) && empty ($email) && empty($phone)) {
echo '<strong>Please go back and fill out all the fields!</strong>';
}
elseif ($captcha != $captchaans) {
echo '<strong>CAPTCHA incorrect!</strong>';
}
else {
$allowed = true;
echo '<strong>You are in the contest!</strong>';
}
if ($allowed) {
db_exec($addcontestantql);
}
}
?>
<?php
$companysql = "SELECT DISTINCT company FROM ".$config['db_dbpaper'].".contest ";
$mainsql = "SELECT * FROM ".$config['db_dbpaper'].".contest ";
if (!empty($_POST['submit'])) {
$companyname = mysql_real_escape_string($_POST['company']);
$mainsql .= "WHERE company like '$companyname'";
}
$company = db_list($companysql);
$contest = db_list($mainsql);
?>
<div id="fullwidthpage">
<h2>Contest</h2>
<p>Please fill the out form and then click submit. Thank you.</p>
<form method="post">
<table width="491" border="0">
<tr>
<td width="107">Company:</td>
<td width="374"><input name="company" type="text" /></td>
</tr>
<tr>
<td>Name:</td>
<td><input name="name" type="text" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="address" type="text" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input name="phone" type="text" /></td>
</tr>
</table>
<p><?php echo '<img src="'.BASE_URL.'images/captcha/'.date('n').'.jpg">'; ?><br />
What color is this?:<br />
<input name="captcha" size="25" style="text-transform: lowercase;"/>
<input name="submitcontestant" type="submit" value="Submit" />
</p>
</form>
id suggest you to start here : http://se2.php.net/manual/en/intro.mysqli.php
you're way off track at the moment and basically asking someone to code it for you.
after your posts update : it seems that the problem is you're not connecting to sql at all try using $link = mysql_connect('host', 'mysql_user', 'mysql_password');
All set, I was creating my data table in the wrong database, new table was added in the appropriate database and all better :). Thanks to everyone who gave me input!

Insert query to insert into a MySQL database does not work. (PHP)

I'm trying to insert values from a form and insert it onto a MySQL database.
But it does not work.
I'm a beginner,please help.
Here is my Code:
<?php
function register(){
$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$phone = $_POST['phone'];
//echo $name;
$con = mysqli_connect("localhost", "root", "", "LinuxCreditSociety");
$rs = $con->query("insert into cust_mst (customer_id,customer_name,customer_address,customer_mobile,email_id)values(1003,'$name','$address','$phone','$email')");
$rs->free();
$con->close();
}
?>
<html>
<head>
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="phpcss.css"></link>
</head>
<body>
<div style="position:absolute;left:300px;top:5px">
<h1 align="center"><font face="Purisa" size="20" color="purple">Linux Credit Society</font></h1>
</div>
<div style="position:absolute;right:160px;top:5px"><img src="linux.jpg" height="150" /></div>
<div style="position:absolute;left:140px;top:5px"><img src="linux.jpg" height="150" /></div>
<form method="post">
<div class="st1">
Name:</br></br>
Address:</br></br>
Email-id:</br></br>
Phone#:</br></br>
</div>
<div class="st2">
<div style="position:absolute;top:5px">
<input type="text" name="name">
</div>
<div style="position:absolute;top:78px">
<input type="text" name="address">
</div>
<div style="position:absolute;top:148px">
<input type="text" name="email">
</div>
<div style="position:absolute;top:218px">
<input type="text" name="phone">
</div>
<div style="position:absolute;top:290px;">
<input type="submit" value="Register">
</div>
</div>
</form>
<div style="position:absolute;top:320px;">
<?php
if(isset($_POST['submit']) && $_POST['submit'] == "Register")
register();
?>
</div>
</body>
</html>
And this is how I have created my database:
create database LinuxCreditSociety;
use LinuxCreditSociety;
create table cust_mst(
customer_id int,
customer_name varchar(50),
customer_address varchar(70),
customer_mobile double,
email_id varchar(50));
insert into cust_mst values(1001, 'Jack Mathew', 'Bandra', '9998887770', 'jackm#yahoo.com');
insert into cust_mst values(1002, 'Jill Roberts', 'Dadar', '999665550', 'jillr#rediff.com');
EDIT:
Guys I just made one change and it worked thankyou.Now I will work on Injections as well!!
Here is what I did:
I just changed the call from ->
<?php
if(isset($_POST['submit']) && $_POST['submit'] == "Register")
register();
?>
to this ->
<?php
if(isset($_POST['name']) && $_POST['name'] != "")
register();
?>
Surely you want this the other way around:
$_POST[name] = $name;
$_POST[address] = $address;
$_POST[email] = $email;
$_POST[phone] = $phone;
eg:
$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$phone = $_POST['phone'];
I assume your data is coming from $_POST. Change
$_POST[name] = $name;
$_POST[address] = $address;
$_POST[email] = $email;
$_POST[phone] = $phone;
Into
$name = $con->real_escape_string($_POST['name']);
$address = $con->real_escape_string($_POST['address']);
$email = $con->real_escape_string($_POST['email']);
$phone = $con->real_escape_string($_POST['phone']);
Note the use of mysqli_real_escape_string() in order to prevent SQL injections. Also, since you're already using the mysqli extension, consider building prepared statements rather than interpolating variables into the query string.
Last side note: to prevent error notices like "Use of undefined constant name - assumed 'name' in /your/script", access array keys using strings (i.e. $_POST['name'] instead of $_POST[name]).
Try this, but dont forget to sanitize your variables !!!
function register(){
$name = $_POST['name'];
$address = $_POST['address'];
$phone = $_POST['email'];
$email = $_POST['phone'];
$con = new mysqli("localhost", "root", "", "LinuxCreditSociety");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$con->query("insert into cust_mst (customer_id,customer_name,customer_address,customer_mobile,email_id)values(NULL,'$name','$address','$phone','$email')")) {
printf("Errormessage: %s\n", $con->error);
}
$con->close();
}
function register(){
$name = $_POST['name'];
$address = $_POST['address'];
$phone = $_POST['email'];
$email = $_POST['phone'];
$con = mysqli_connect("localhost", "root", "", "LinuxCreditSociety");
$rs = $con->query("insert into cust_mst (customer_id,customer_name,customer_address,customer_mobile,email_id)values(NULL,'{$name}','{$address}','{$phone}','{$email}')");
$rs->free();
$con->close();
}

Categories