Error in inserting values entered through form into database table?

My form is perfectly working

This is my php code from where I am trying to insert values

                        <?php 



        $conn = mysqli_connect("localhost", "root", ""); // Establishing Connection with Server..
       $db = mysqli_select_db($conn,"test"); // Selecting Database

              // // Check connection
        if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
         } 
     echo "Connected successfully";
    if (isset($_POST['submit'])) {

       $name = isset($_POST['full_name']);
      $address = isset($_POST['address']);
     $school_name = isset($_POST['school_name']);
     $medium = isset($_POST['medium']);
     $percentage = isset($_POST['percentage']);
    $student_mobile_no = isset($_POST['student_mobile_no']);
     $parent_mobile_no = isset($_POST['parent_mobile_no']);

   if($name !='' && $address !='' && $school_name !='' && $medium !='' && $percentage !='' && 
   $student_mobile_no !=''  && $parent_mobile_no !='' ){


    $query = mysqli_query($conn,"INSERT INTO  registration(full_name, address, 
   school_name, 
    medium,percentage,student_mobile_no,parent_mobile_no) values ('$name', '$address', 
    '$school_name','$medium,$percentage,$student_mobile_no,$parent_mobile_no')"); //Insert 
    Query
      echo "Form Submitted succesfully";

    }
      if ($conn->query($query) === TRUE) {
      echo "New record created successfully";
     } else {
       echo "Error: " . $query . "<br>" . $conn->error;
    }
   }
     mysqli_close($conn); // Connection Closed

 ?>

The database connection is successfull.

but the data is not inserting into database .
Where I went wrong ? How can I fix it ?

$query = mysqli_query($conn,“INSERT INTO registration(full_name, address, school_name, medium,percentage,student_mobile_no,parent_mobile_no) values (‘$name’, ‘$address’, ‘$school_name’,‘$medium,$percentage,$student_mobile_no,$parent_mobile_no’)”);

check your quote marks.

1 Like

That was silly mistake , I corrected it now its inserting values in table in every column its not taking values that I am filling in form , instead its inserting 1 every column ?

You’re executing your query multiple times. Your IF should be checking if $query is true.

1 Like

You should consider using Prepared Statements, because

  1. you wouldn’t have to quote variables by yourself, so this mistake would have given you an error message that tells you your variables don’t match it’s placeholders
  2. you wouldn’t give everybody the chance to delete your complete database
2 Likes

Have a look at the values you are extracting from the form:

       $name = isset($_POST['full_name']);
      $address = isset($_POST['address']);
     $school_name = isset($_POST['school_name']);
     $medium = isset($_POST['medium']);
     $percentage = isset($_POST['percentage']);
    $student_mobile_no = isset($_POST['student_mobile_no']);
     $parent_mobile_no = isset($_POST['parent_mobile_no']);

You could do some debugging, use var_dump() or something to see what you’re actually putting into those variables.

You should look at prepared statements - that would have got around the mistake with quotes straight away, and would deal with the problem you’ll get when Mr O’Connor tries to register.

2 Likes

I fixed it now and its working perfectly Thanks for the help

Slightly off-topic: You could help yourself and those trying to help you by taking more care over formatting your code. At least use the same number of spaces or tabs on each line of code in a block and indent code within each if block.

I will take care of it definitely , sorry for the troubles

1 Like

Hey I am trying to generate a random seat no and save it to my table every time a user submited the form , for that I am using this method

   $seat_no = rand(0000,9999);

   if($name !='' && $student_mobile_no !=''){
        $query = mysqli_query($conn,"INSERT INTO student_registration(seat_no,full_name, 
        address, school_name, medium,percentage,student_mobile_no,parent_mobile_no) values 
      ('$seat_no','$name', '$address', '$school_name','$medium','$percentage','$student_mobile_no','$parent_mobile_no')"); //Insert Query
}
  • I have also created a column in my table called seat_no
    But its not generating seat number randomly .
    I sthis correct way ?

What is it doing?

I want to generate seat number/roll no of student randomly every time a student submit registration form

Yes, but what is your code actually doing that leads you to believe it is not working? Does it not generate a number, or does it generate the same one all the time, or something else? Save me the job of typing in the code to see for myself.

ETA - do you actually want a random number, or do you also need the seat number to not be one that a previous row has already used up? That’s a different thing.

  • I just wnat to generate random number .
  • When I submit my form there is no entry of new data after adding above code for random seat no , so that is not working

OK, on the face of it there’s nothing wrong with the code posted. What type of column is seat_no? If it’s a numeric column, will adding the quotes around it in the query cause you a problem? If you test the query in phpmyadmin you can see whether it’s that, or just remove the quotes.

1 Like

… and if removing the quotes fixed it, look again at prepared statements. That’d be twice in one thread where they’d have prevented an issue.

2 Likes

if the insert failes, there’s probably an error message, so let it show you

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.