0

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.\n");
        return false;
    }       
    if (empty($fatherName)) {
        echo nl2br("Please write email .\n");   
    }
    if (empty($email)) {
        echo nl2br("Please write email .\n");
    }
    if (empty($NIC)) {
        echo nl2br("Please write NIC number .\n");  
    };
}

?>

<form id="form1" name="form1" method="post" >
<p>&nbsp;</p>
<p align="center" class="style1">   REGISTERATION FORM </p>
<p>&nbsp;</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>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Cesar
  • 7
  • 3
  • 1
    You have to use mysqli_query (http://php.net/manual/es/mysqli.query.php) not `mysql_query()` – leonardo_palma Oct 28 '15 at 16:54
  • 2
    You also need to test for blank values before you issue the mysql query. In your current code you set the variables at the top but you test for blank values after the query. You need to test for blanks at the top, before you set the variable names. You should change the script logic. – CharlesEF Oct 28 '15 at 17:11
  • If i use msqli_query error occurs Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\form\form.php on line 32 – Cesar Oct 28 '15 at 18:05
  • CharlesEF i changed the logics but nothing stored why and no error occurs – Cesar Oct 28 '15 at 18:06

1 Answers1

0
$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.

Community
  • 1
  • 1
miken32
  • 42,008
  • 16
  • 111
  • 154