0

I'm having problems sending data to mysql through a php form.

Here is my form(info.php):

<form action="send.php" class="form-horizontal" method="post" name="application">
<div class="col-md-6">
<select class="form-control" name="title">
<option>Please select </option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select><br>
<input type="text" class="form-control" placeholder="First name" name="first_name"><br>
<input type="text" class="form-control" placeholder="Surname" name="surname"><br>
Date of birth: <input type ="date" name="dob" class="form-control"><hr>
<h3>Address</h3>
<input type="text" class="form-control" placeholder="House name/ House number"     name="house"><br>
<input type="text" class="form-control" placeholder="Street Name" name="street"><br>
<input type="text" class="form-control" placeholder="Town" name="town"><br>
<input type="text" class="form-control" placeholder="Postcode" name="postcode"><br>
<hr></div>
<div class="col-md-6">
<input type="text" class="form-control" placeholder="Country of birth" name="origin"><br>
<input type="text" class="form-control" placeholder="Mobile/Phone number" name="number">       
<input type="text" class="form-control" placeholder="Email address" name="email"><br>
<input type="hidden" name="plan" value="<? $plan; ?>">
<input type="submit" class="btn btn-success btn-lg" value="Submit application">
<hr>
</form>

Here is send.php:

<?php

 $host="localhost"; // Host name 
 $username="******"; // Mysql username 
 $password="******"; // Mysql password 
 $db_name="*****"; // Database name 
 $tbl_name="*****"; // Table name 

 // Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

 // Get values from form 
 $title=$_POST['title'];
 $first_name=$_POST['first_name'];
 $surname=$_POST['surname'];
 $dob=$_POST["dob'];
 $house=$_POST["house'];
 $street=$_POST["street'];
 $town=$_POST["town'];
 $postcode=$_POST["postcode'];
 $origin=$_POST["origin'];
 $number=$_POST["number'];
 $email=$_POST["email'];
 $plan=$_POST["plan'];



 // Insert data into mysql 
 $sql="INSERT INTO $tbl_name(' ',title, first_name, surname, dob, house, street, town,                postcode, origin, number, email, plan)VALUES(' ','$title', '$first_name', '$surname', '$dob',  '$house', '$street', '$town', '$postcode', '$origin', '$number', 
'$email', '$plan')";
 $sql=mysql_real_escape_string($sql);
 $result=mysql_query($sql);

 // if successfully insert data into database, displays message "Successful". 
 if($result){
 echo "Successful";
 echo "<BR>";
 echo "<a href='insert.php'>Back to main page</a>";
 }

 else {
 echo "ERROR";
 }
 ?> 

 <?php 
 // close connection 
 mysql_close();
 ?>

When I submit the form to send.php the page goes blank. Does this mean there is something wrong with my database auth?

Thanks in advance.

7
  • 2
    Don't mix quote styles $dob=$_POST["dob']; Commented Nov 18, 2013 at 18:09
  • Many of your POST values are using " and ' as in ["dob'] etc. use ['dob'] and keep going...............>>> plus do use MySQLi_ and prepared statements or PDO. Your subject to injection using MySQL_ <<< how many times have I said that "this" month. "If I had a nickel..." Commented Nov 18, 2013 at 18:10
  • I have just amended this. Now I am getting a 'Cannot connect' error. I guess there must be something wrong with my credentials Commented Nov 18, 2013 at 18:14
  • 1
    @RamarioDepass - you can use mysql_error() to tell you what the last error from the database was; try replacing or die("cannot connect"); with or die(mysql_error());. Commented Nov 18, 2013 at 18:15
  • Well, that's kind of a good sign. Do check your credentials. Commented Nov 18, 2013 at 18:16

4 Answers 4

1

Your issue is that you never actually close most of your quotes. Your code should look like this:

<?php

 $host="localhost"; // Host name 
 $username="******"; // Mysql username 
 $password="******"; // Mysql password 
 $db_name="*****"; // Database name 
 $tbl_name="*****"; // Table name 

 // Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

 // Get values from form 
 $title=$_POST['title'];
 $first_name=$_POST['first_name'];
 $surname=$_POST['surname'];
 $dob=$_POST['dob'];
 $house=$_POST['house'];
 $street=$_POST['street'];
 $town=$_POST['town'];
 $postcode=$_POST['postcode'];
 $origin=$_POST['origin'];
 $number=$_POST['number'];
 $email=$_POST['email'];
 $plan=$_POST['plan'];



 // Insert data into mysql 
 $sql="INSERT INTO $tbl_name(' ',title, first_name, surname, dob, house, street, town,                postcode, origin, number, email, plan)VALUES(' ','$title', '$first_name', '$surname', '$dob',  '$house', '$street', '$town', '$postcode', '$origin', '$number', 
'$email', '$plan')";
 $sql = mysql_real_escape_string($sql);
 $result=mysql_query($sql);

 // if successfully insert data into database, displays message "Successful". 
 if($result){
 echo "Successful";
 echo "<BR>";
 echo "<a href='insert.php'>Back to main page</a>";
 }

 else {
 echo "ERROR";
 }
 ?> 

 <?php 
 // close connection 
 mysql_close();
 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

It was 50% of the problem.
0

try this:

<form action="send.php" class="form-horizontal" method="post" name="application">
<div class="col-md-6">
  <p>
  <select class="form-control" name="title">
    <option value="" disabled>Please select </option>
    <option value="Mr">Mr</option>
    <option value="Mrs">Mrs</option>
    <option value="Miss">Miss</option>
    <option value="Ms">Ms</option>
  </select>
  </p>
  <p><br>
    <input type="text" class="form-control" placeholder="First name" name="first_name">
  </p>
  <p><br>
    <input type="text" class="form-control" placeholder="Surname" name="surname">
  </p>
  <p>Date of birth:<br><input type ="date" name="dob" class="form-control">
  </p>
</div><hr>
<h3>Address</h3>
<p>
  <input type="text" class="form-control" placeholder="House name/ House number"     name="house">
  </p>
<p><br>
  <input type="text" class="form-control" placeholder="Street Name" name="street">
</p>
<p><br>
  <input type="text" class="form-control" placeholder="Town" name="town">
</p>
<p><br>
  <input type="text" class="form-control" placeholder="Postcode" name="postcode"><br>
</p>
<hr>
<div class="col-md-6">
  <p>
    <input type="text" class="form-control" placeholder="Country of birth" name="origin">
    </p>
  <p><br>
    <input type="text" class="form-control" placeholder="Mobile/Phone number" name="number"><form action="send.php" class="form-horizontal" method="post" name="application">
<div class="col-md-6">
  <p>
  <select class="form-control" name="title">
    <option value="" disabled>Please select </option>
    <option value="Mr">Mr</option>
    <option value="Mrs">Mrs</option>
    <option value="Miss">Miss</option>
    <option value="Ms">Ms</option>
  </select>
  </p>
  <p><br>
    <input type="text" class="form-control" placeholder="First name" name="first_name">
  </p>
  <p><br>
    <input type="text" class="form-control" placeholder="Surname" name="surname">
  </p>
  <p>Date of birth:<br><input type ="date" name="dob" class="form-control">
  </p>
</div><hr>
<h3>Address</h3>
<p>
  <input type="text" class="form-control" placeholder="House name/ House number"     name="house">
  </p>
<p><br>
  <input type="text" class="form-control" placeholder="Street Name" name="street">
</p>
<p><br>
  <input type="text" class="form-control" placeholder="Town" name="town">
</p>
<p><br>
  <input type="text" class="form-control" placeholder="Postcode" name="postcode"><br>
</p>
<hr>
<div class="col-md-6">
  <p>
    <input type="text" class="form-control" placeholder="Country of birth" name="origin">
    </p>
  <p><br>
    <input type="text" class="form-control" placeholder="Mobile/Phone number" name="number">
  </p>
  <p>       
    <input type="text" class="form-control" placeholder="Email address" name="email">
  </p>
  <p><br>
    <input type="hidden" name="plan" value="<?php $plan; ?>">
    <input type="submit" class="btn btn-success btn-lg" value="Submit application">
  </p>
</div><hr>
</form>

Here is send.php:

<?php

 $host="localhost"; // Host name 
 $username="******"; // Mysql username 
 $password="******"; // Mysql password 
 $db_name="*****"; // Database name 
 $tbl_name="*****"; // Table name 

 // Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

 // Get values from form 
 $title         =$_POST['title'];
 $first_name    =$_POST['first_name'];
 $surname       =$_POST['surname'];
 $dob           =$_POST['dob'];
 $house         =$_POST['house'];
 $street        =$_POST['street'];
 $town          =$_POST['town'];
 $postcode      =$_POST['postcode'];
 $origin        =$_POST['origin'];
 $number        =$_POST['number'];
 $email         =$_POST['email'];
 $plan          =$_POST['plan'];



 // Insert data into mysql 
 $sql="INSERT INTO `$tbl_name` (title, first_name, surname, dob, house, street, town, postcode, origin, number, email, plan) VALUES ('$title', '$first_name', '$surname', '$dob',  '$house', '$street', '$town', '$postcode', '$origin', '$number','$email', '$plan')";
 //$sql=mysql_real_escape_string($sql);
 $result=mysql_query($sql);

 // if successfully insert data into database, displays message "Successful". 
 if($result){
 echo "Successful";
 echo "<BR>";
 echo "<a href='insert.php'>Back to main page</a>";
 }

 else {
 echo "ERROR";
 }
 ?> 

 <?php 
 // close connection 
 mysql_close();
 ?>

Comments

0

you are typing the POST all wrong.
yours: $_POST["somename']; // begin with double quote inside and finish with single quote.
Correct: $_POST[""]; or $_POST['somename']; // begin with the same end end with the same. your variables are not assigned correctly because of that.

Comments

0
$sql="INSERT INTO " . $tbl_name . "(`title`, `first_name`, `surname`, `dob`, `house`, `street`, `town`, `postcode`, `origin`, `number`, `email`, `plan`) VALUES($title, $first_name, $surname, $dob, $house, $street, $town, $postcode, $origin, $number, $emai, $plan)";
$sql = mysql_real_escape_string($sql);
$result=mysql_query($sql);

1 Comment

Try to elaborate your answer and tell why the person had problems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.