Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There

I have created an HTML form linked to some PHP code to write fields to a SQL DB but it does'nt work ? Can anyone have a look at my code and let me know if you can find any reason why it would not work ?

HTML FORM
<h1>Create Your Account</h1>
      
      <div class="namef">
      Name: <br><br> </div>
      <div class="surnamef">
      Surname: <br><br></div>
      <div class="emailf">
      Email: <br><br> </div>
      <div class="passwordf">
      Password: <br><br></div>


PHP Code
<?php

define('DB_NAME','Mydbname');
define('DB_USER','Myusername');
define('DB_PASSWORD','Mpassword');
define('DB_HOST','Myhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

$db_selected = mysql_select_db (DB_NAME, $link) ;

$Name = $_POST['Name'];
$Surname = $_POST['Surname'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];

$sql = "INSERT INTO Users (Name,Surname,Email,Password) VALUES ('".$Name."','".$Surname."',
'".$Email."','".$Password."')";

$link->query($sql);

mysql_close($link)

?>


What I have tried:

Please see my code in problem descriptions section. This is what i have tried.
Posted
Updated 8-May-18 4:39am
v3

Your html only contains text and no input controls. There's no POST request being made with the Name, Surname, Email and Password parameters.

Have a look here[^] on how to create a form. For example:

<form action="/login.php" method="post">
  Name:<br><input type="text" name="Name"><br>
  Surname:<br><input type="text" name="Surname"><br>
  E-mail:<br><input type="text" name="Email"><br>
  Password:<br><input type="password" name="Password"><br>
  <input type="submit" value="Submit">
</form> 
 
Share this answer
 
v2
Comments
Member 13817239 8-May-18 9:31am    
Thanks Thaddues, i think i pasted my code wrong here is what i have in html:

Create Your Account




Name:

Surname:

Email:

Password:
[no name] 8-May-18 10:38am    
Could you post it inbetween pre-tags in the original question? Otherwise it will not show the html but the formatted text.
MadMyche 8-May-18 9:59am    
Without a defined method, this form will default to a GET operation
[no name] 8-May-18 10:37am    
Thanks MadMyche, you're right of course. I've updated the example.
1. As Thaddeus answered, this will require a form. In addition, this form will need a method="POST" attribute.

2. The operation on the database will be an INSERT, and the appropriate method to call that within PHP is execute() and not query()

3. To avoid risks of SQL Injection, your SQL command should not be a string concantenation. You should utilize parameters.

Please review the PHP information for this, which has samples
http://php.net/manual/en/pdo.prepared-statements.php[^]
 
Share this answer
 
Comments
Member 13817239 8-May-18 12:16pm    
Thanks so much , highly appreciated. Will make the changes advised :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900