Jump to content

From a HTML form to a MYSQL Database.. I need Help Check my code please.


madjack87

Recommended Posts

Html Form

 

<body>
<form id="form1" name="form1" method="post" action="database1.php">
  <p>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" tabindex="10" />
    |  </p>
  <p>
    <label for="email">Email:</label>
    <input type="text" name="email" id="email" tabindex="20" />
  </p>
  <p>
    <label for="submit"></label>
    <input type="submit" name="submit" id="submit" value="Submit" />
    <br />
  </p>
</form>
</body>

 

Php page

<?php
$username = "root";
$password = "password";
$database = "newletter";
$table = "contacts";

mysql_connect ('localhost',$username,$password);

mysql_select_db($database) or die ("Could not connect");

$name = $_POST['name'];
$email = $_POST['email'];

$sql = "INSERT INTO $table(name,email) VALUES($name,$email)";

mysql_close();
?> 

I would apply some validation to ensure that the email address is really an email address and that the name is not null etc.

 

Also change your code on your insert to this

 

$sql = "INSERT INTO $table(name,email) VALUES('$name','$email')";

 

I placed single quotes around your values as they will be strings.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.