-2

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.

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Rift
  • 1
  • 2
  • Wrap off `quotes` from `table and column` name instead use `backtick`. Problem in this line `tasks ('TName','TDesc')` – Saty Dec 31 '15 at 10:23
  • Please take a look into your http server error log file. There you can read what the issue is instead of having to _guess_. – arkascha Dec 31 '15 at 10:29
  • Side note: your code is wide open to sql injections. – arkascha Dec 31 '15 at 10:29

3 Answers3

0

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)";
jilesh
  • 436
  • 1
  • 3
  • 13
  • Thanks to everyone for the feedback and specially to Vasanth Kumar and jilesh for the answers. Once i removed the backticks from the 2 elements in here ('TName','TDesc') it worked. And i also used this echo "Error: " . $sqltask. "
    " . mysqli_error($conn); really helpful line to figure out why it wasn't working in the first place.
    – Rift Dec 31 '15 at 11:09
0

You are mixing OOP style and Procedural Style in your code

You are used Procedural Style in your DBconnect.php file. And You are missing ; in your connection file.

DBconnect.php file should be:

<?php    
   $con= mysqli_connect("localhost", "root","","first_app");    
?>

register_code.php code should be:

<?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 = mysqli_query($con,"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';

Add_Task_code.php file code should be:

<?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 (mysqli_query($con,$sqltask))
        header("Location: Tasks.php");
    else
        header("Location: Add_Task.php");
}
?>
Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28
0

Try to make the below changes and see what the actual error is.then debug your code.

if($_SERVER['REQUEST_METHOD']=='POST')
{
   $TaskName = $post['TName'];
   $TaskDesc = $post['TDesc'];

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

   if ($con->query($sqltask))
     echo "Successfully Inserted";
   else
     echo "Error: " . $sqltask. "<br>" . mysqli_error($conn);
}
?>
Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46