0

I'm trying to save data from a form into a mysql database. I can connect to the database but for some reason I can't get it to insert the data into the database. I have a feeling it might just be a syntax error I'm not seeing.

Any help would be much appreciated.

PHP

// Get values from form 
 $Nombre=$_POST['Nombre'];
 $Email=$_POST['Email'];
 $Telefono=$_POST['Telefono'];

// Insert data into mysql 
$sql="INSERT INTO $leads(Nombre, Email, Telefono)VALUES('$Nombre','$Email','$Telefono')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
}

else {
echo "ERROR";
}

HTML

  <form action="leads.php" method="POST">
       <input placeholder="Nombre" type="text" name="Nombre" maxlength="40"/>
       <input placeholder="Email" type="text" name="Email" maxlength="100"/>
       <input placeholder="Teléfono" type="text" name="Telefono" maxlength="9" pattern=".{8,}"    required title="8 numeros mínimo"/>
       <button class="btn-cita" name="cita">Hacer Cita</button>
  </form>
2
  • mysql_query($sql) or die(mysql_error()); use this to get the error and posted the error here. Commented Mar 16, 2014 at 15:23
  • You should always try first echo $sql and look for if output is correct. This is kind of most basic debugging you could do when dealing with sql. Commented Mar 16, 2014 at 15:26

1 Answer 1

1

Yes, you do. You are vulnerable to SQL injection attacks, and are using undefined variables in your query:

$sql="INSERT INTO $leads(Nombre, Email, Telefono)VALUES('$Nombre','$Email','$Telefono')";
                  ^^^^^^---undefined

Producing a query something like

INSERT INTO (Nombre, etc...
Sign up to request clarification or add additional context in comments.

2 Comments

Do you know how I can fix this?
I just realized that I had written the name of the table 'leads' instead of using the $tbl_name variable I had set up. $sql="INSERT INTO $leads(Nombre, Email, Telefono)VALUES('$Nombre','$Email','$Telefono')"; $result=mysql_query($sql);

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.