Search code examples
phpdatabaseformsmysqliwampserver

Cannot insert data into database (php/mysqli) (Phpstorm 9/Wamp)


I have an issue with inserting the data that I gather from one of my forms into my database. Each form adds data to a different table in the database(one into users and one into tasks).

I use one form for registration and I'll paste the important parts of the code below(this one is working). This is the form part of the Register.php file

<form method="post" action="register_code.php">
<div class="FormElement">
    <input name="user_name" type="text" class="Tfield" id="user_name" required placeholder="User Name">
</div>

<div class="FormElement">
    <input name="password" type="password" class="Tfield" id="password" required placeholder="Password">
</div>

<div class="FormElement">
    <input name="email" type="email" class="Tfield" id="email" required placeholder="E-mail">
</div>

<div class="FormElement">
    <input name="first_name" type="text" class="Tfield" id="first_name" required placeholder="First Name">
</div>

<div class="FormElement">
    <input name="last_name" type="text" class="Tfield" id="last_name" required placeholder="Last Name">
</div>

<div class="FormElement">
    <input type="submit" id="Register" name="Register" value="Register" class="button">
</div>

This is the register_code.php file

  <?php
require "DBconnect.php";
$post = $_POST;
if(isset($post)) {
    session_start();
    $UName = $post['user_name'];
    $PW = md5($post['password']);
    $FName = $post['first_name'];
    $LName = $post['last_name'];
    $Email = $post['email'];

    $sql = $con->query("INSERT INTO users (user_name, password, email, first_name, last_name) VALUES ('$UName','$PW','$Email', '$FName', '$LName')");
    if($sql)
        header("Location: Registration_successful.php");
    else
        echo "Please try again to register";
}

include 'Register.php';

And another form I use to add data into another table(named tasks). The data I gather from this file will not insert into my database for some reason.

This is the form part of the Add_Task.php file:

    <form method="post" action="Add_Task_code.php">

    <div class="FormElement">
        <input name="TName" type="text" class="Tfield" id="TName" required placeholder="Task name">
    </div>

    <div class="FormElement">
        <input name="TDesc" type="text" class="TextField" id="TDesc" required placeholder="Task summary">
    </div>

    <div class="FormElement">
        <input type="submit" id="Submit" name="Submit" value="Submit" class="button">
    </div>
</form>

And this is the code from the Add_Task_code.php file

  <?php
require 'DBconnect.php';

$post=$_POST;

if(isset($post))
{
    $TaskName = $post['TName'];
    $TaskDesc = $post['TDesc'];

    $sqltask="INSERT INTO tasks ('TName','TDesc') VALUES ('$TaskName','$TaskDesc')";

    if ($con->query($sqltask))
        header("Location: Tasks.php");
    else
        header("Location: Add_Task.php");
}
?>

The file DBconnect.php only contains this:

   <?php

$con= mysqli_connect("localhost", "root","","first_app")

?>

The problem is that even though the code is similar in both forms only one of them is working. Every time I run the Add_Task.php file it redirects me to the same page (as I instructed it) since it does not add anything to the database.

I also checked the tables just in case it adds something but it does not.


Solution

  • please set your primary_key(id) as auto increment in table tasks. if you not set it might be possible. and change this line

    $sqltask="INSERT INTO tasks ('TName','TDesc') VALUES ('$TaskName','$TaskDesc')";
    

    like this :

    $sqltask="INSERT INTO tasks (TName,TDesc) VALUES ($TaskName,$TaskDesc)";