zeeshan haider February 2016

Why query not inserting record

i am trying to insert simple two values in db but they are neither inserting nor giving errors. i do not know about what's happening. I also tried to sql query outside the if statement but it did not work.

please help

coding file

<?php 

    include 'includes/db_connection.php';


    if (isset($_POST['submit']))
     {
       $name = test_input($_POST["name"]);
       $fathername = test_input($_POST["fathername"]);


    $sql = "INSERT INTO student (name,fathername)
    VALUES ('$name','$fathername')";
    mysqli_query($conn, $sql);

    if(mysqli_query($conn, $sql))
    {
        $last_id = mysqli_insert_id($conn);
        echo "New record created successfully. Last inserted ID is: " . $last_id;
    }
    else 
    {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
     function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }

    }


    ?>


    <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.2.43/jquery.form-validator.min.js"></script>
        </head>



        <body>

            <form action="valid_test.php" method="post" id="registration-form">
            <div>
        Name
        <input name="name" data-validation="length alphanumeric" 
             data-validation-length="6-15" 
             data-validation-error-msg="User name has to be an alphanumeric value (6-15 chars)">
        &        

Answers


Pupil February 2016

When we post HTML form, elements get posted with name attribute.

You are missing name attribute for submit button.

Change:

<input type="submit" value="Validate">

To:

<input type="submit" value="Validate" name="submit">

As your submit button does not have a name attribute, the condition

if (isset($_POST['submit'])) is not getting fulfilled and hence code not working.

Also, you are repeating mysqli_query($conn, $sql);. Remove first instance of it.


J van Amerongen February 2016

First, in your PHP, where is $conn: you are trying to insert something that doesn't exist.Second, your SQL isn't quite right, instead of that you should have something like this:

$sql = "INSERT INTO student (name, fathername) VALUES (" . $name . ", " . $fathername . ");";

What you did in your question, is you put in '$name' and '$fathername' as individual characters, what you want is the value of the variable, this is how you do that.

Regards

Post Status

Asked in February 2016
Viewed 2,589 times
Voted 4
Answered 2 times

Search




Leave an answer