Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Validation with SQL
    text
    copied!<p>Hi I'm brand new to PHP and I'm just doing a simple form to learn. It just contains an email address, I want to Validate the information and send it to the database. My problem is connecting it to a database. I've done a few tutorials but just leave myself confused. </p> <p>Right now this is my homepage</p> <p>reg.php</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Registration Form&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php include("validation.php"); ?&gt; &lt;form method="post" action="connect.php" name="form"&gt; &lt;ul id="errors"&gt; &lt;li&gt;&lt;?php echo $err_email; ?&gt;&lt;/li&gt; &lt;/ul&gt; &lt;div id="wrapper"&gt; &lt;div&gt;Email&lt;/div&gt; &lt;div class="input"&gt;&lt;input type="text" name="email" value="&lt;?php echo $val_email; ?&gt;" /&gt;&lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This is my validation.php file </p> <pre><code>&lt;?php if($_POST) { $email = $_POST['email']; // Email if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email)) { $val_email = $email; }else{ $err_email = 'Please enter valid Email address.'; } if((strlen($val_email)&gt;0) ){ header("Location: reg"); }else{ } } ?&gt; </code></pre> <p>finally my connect file</p> <pre><code> &lt;?php $host="localhost"; $username="admin"; $password=""; // Mysql password $db_name="davidtest"; // Database name $tbl_name="users"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("davidtest")or die("cannot select DB"); // Get values from form $email=$_POST['email']; // Insert data into mysql $sql="INSERT INTO $users(email)VALUES('$email')"; $result=mysql_query($sql); // close connection mysql_close(); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload