0

I am trying to create a form where the variable fields is sent to a database in phpmyadmin. The problem is that the fields in phpmyadmin are blank, as if I hadn't write anything in the form. The code is the following in PHP:

$name = $_POST["name"];
$institution = $_POST["institution"];
$email = $_POST["email"];
$country = $_POST["country"];
$hcp = $_POST["hcp"];
$texto = $_POST["texto"];
$state = $_POST["state"];
$license = $_POST["license"];

$connection = mysql_connect($database_host, $database_username, $database_password);
if(!$connection) { // if our attempt to connect failed
   die('Could not connect: ' . mysql_error());
}

mysql_select_db($database_name, $connection) or die("You cannot select data base");


$consulta = ("INSERT INTO `1264`(`name`, `institution`, `email` , `country`, `hcp` , `texto` , `state` , `license`) VALUES ('".$name."','".$institution."','".$email."','".$country."','".$hcp."','".$texto."','".$state."','".$license."');");
    echo $consulta; die;

mysql_query($consulta,$connection) or die("You cannot register. Try it later, please.");

And in HTML the format of the form is the following:

<form method="post" action="php/enviar.php">


    <span class="texto azul">Name</span><input class="caja texto" name="name" type="text"/><br /><br />
<span class="texto azul">Institution</span><input class="caja texto" name="institution" type="text"/><br /><br />
 <span class="texto azul">Email address</span><input class="caja texto" name="email" type="text"/><br /><br />

<span class="texto azul">Country</span><select id="country" class="caja texto" name="country">
<option value="">Country...</option>

 <option value="Afganistan">Afghanistan</option>
...
</select>
<div id="fadein">
        <span id="usa" class="texto azul inputText">HCP Designation</span><select name="hcp" class="caja inputText texto" disabled>
            <option value="des">Designation...</option>
            <option value="md">MD</option>
            <option value="np">NP</option>
            <option value="rn">RN</option>
            <option value="other">Other</option>
        </select><br /><br />
        <span id="texto" class="texto azul inputText">If other, please specify</span>
        <textarea class="caja inputText texto" name="texto" disabled></textarea><br /><br />
        <span class="texto azul inputText">State of Licensure</span><input class="caja inputText texto" name="state" type="text" disabled/><br /><br />
        <span class="texto azul inputText">License number</span><input class="caja inputText texto" name="license" type="text" disabled/><br /><br />

</div>
        </form>

Does anybody know why it doesn't accept the fields that I write in the form?

  • 1
    Because you advised your script to die before running the query, have a look at `echo $consulta; die;` You're using a deprecated API, you're wide open to sql injection. So it's for the better. – VMai May 27 '14 at 10:39
  • I have deleted echo $consulta; die; but it doesn't work :( – user3665872 May 27 '14 at 11:00
  • Anything being printed out on the page? – asprin May 27 '14 at 11:01
  • The page works like if it had been sent perfect. It appears the last page, with the "thanks" message and the message saying that you have applied properly. But the problem is placed in phpmyadmin, where I see white fields in the database. I can only see the ID because is A/I. – user3665872 May 27 '14 at 11:16
  • So that means the insert query is running. Just after `$license = $_POST["license"];`, put `print_r($_POST);die();` and see what is printed out on the page – asprin May 27 '14 at 11:32
  • This is what is printed: Array ( ) – user3665872 May 27 '14 at 11:34
  • So that's the issue there. Nothing is being posted by the form. I see that the HTML you posted only contains 1 element named `name`. Where are the others? Where is the form closing tag? – asprin May 27 '14 at 11:43
  • Yes, I have the others and the closed tag but it was a really long code because the country select is massive. – user3665872 May 27 '14 at 11:45
  • I have added the most of the code over here :) – user3665872 May 27 '14 at 11:50
  • Please be aware that the mysql extension has been deprecated since 2012, in favor of the mysqli and PDO extensions. It's use is highly discouraged. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Oldskool May 27 '14 at 13:03
  • So... how should I write the code instead mysql...??? – user3665872 May 27 '14 at 13:37

2 Answers2

0

This line is the problem.

echo $consulta; die;

You're instructing the script to exit even before getting to the mysql_query($consulta,$connection) line. Remove the die and it should be good to go provided your connection is good and there's no syntax error in your query.

You also don't require the ( and ) when storing the query as string. So

$consulta = "INSERT INTO `1264`(`name`, `institution`, `email` , `country`, `hcp` , `texto` , `state` , `license`) VALUES ('".$name."','".$institution."','".$email."','".$country."','".$hcp."','".$texto."','".$state."','".$license."')";

Sidenote: You should stop using mysql_* functions as they are deprecated post v5.5. Switch to PDO or mysqli instead.

asprin
  • 9,579
  • 12
  • 66
  • 119
-1

You should first check post array you get.

Do this first:

echo '<pre>';print_r($_POST);exit;

Check what values you get.

Note: Also check value name fields in form & key of post array should be same.

Also replace query with given below.

$consulta = "INSERT INTO `1264`(`name`, `institution`, `email` , `country`, `hcp` , `texto` , `state` , `license`) VALUES ('".$name."','".$institution."','".$email."','".$country."','".$hcp."','".$texto."','".$state."','".$license."')";
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27