I have created a database in MySQL WAMP server and is not saving results. Please tell me what should I do. There is no error while submitting in localhost.
Can't add more details:
<?php
if (isset($_POST['Submit'])) {
$firstName = $_POST['Name'];
$fatherName = $_POST['fatherName'];
$email = $_POST['Email'];
$NIC = $_POST['NIC'];
$server = '127.0.0.1';
$vid = 'root';
$pwd = '';
$conn = mysqli_connect($server, $vid, $pwd);
if (!$conn) {
echo "server not found";
} else {
echo"stored successfully";
@mysql_select_db("database1");
//$firstName= "nazi";
//$fatherName="Ali";
//$email= "nazia.se@yahoo.com";
//$NIC= 67567678;
//$sqlquery="INSERT INTO tab1 (name,fname,email,nic) VALUES ('$firstName', '$fatherName', '$email', $NIC)";
$sqlquery = "INSERT INTO form(name,fname,email,nic) VALUES ('$firstName', '$fatherName', '$email', $NIC)";
//echo $sqlquery;
mysql_query($sqlquery);
}
}
if (isset($_POST['Submit'])) {
if(empty($firstName)){
echo nl2br("Please write name.
");
return false;
}
if (empty($fatherName)) {
echo nl2br("Please write email .
");
}
if (empty($email)) {
echo nl2br("Please write email .
");
}
if (empty($NIC)) {
echo nl2br("Please write NIC number .
");
};
}
?>
<form id="form1" name="form1" method="post" >
<p> </p>
<p align="center" class="style1"> REGISTERATION FORM </p>
<p> </p>
<table width="447" border="1">
<tr>
<td width="169">Name</td>
<td width="262"><label>
<input name="Name" type="text" />
</label></td>
</tr>
<tr>
<td>Father Name</td>
<td><label>
<input type="text" name="fatherName" />
</label></td>
</tr>
<tr>
<td>email Address</td>
<td><label>
<input name="Email" type="text" />
</label></td>
</tr>
<tr>
<td>NIC </td>
<td><label>
<input name="NIC" type="text" size="15" />
</label></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Submit" /> </td>
</tr>
</table>
</form>
$conn = mysqli_connect($server, $vid, $pwd);
...
@mysql_select_db("database1");
You're not using the right functions here. mysql
and mysqli
are two totally different things. One of them hasn't even been supported for years. As well, you're leaving yourself wide open for people to wipe out your database. Try something like the following. And if it makes no sense, read up on prepared statements and their benefits.
$conn = new mysqli($server, $vid, $pwd, "database1");
$sqlquery = "INSERT INTO form(name,fname,email,nic) VALUES (?, ?, ?, ?)";
$stmt = $conn->prepare($sqlquery);
$stmt->bind_param("sssi", $firstName, $fatherName, $email, $NIC);
$stmt->execute();
$mysqli->close();
Finally, if you want to check for empty values you should probably do it before you write things to the database, not after.