Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

What's wrong with My codes, it doesn't submitting data to database.

Here is my PHP form Codes.

<?php

    $servername = "localhost";
$username = "paywrite_bishnoi";
$password = "ashok";
$dbname = "paywrite_bookset";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if( isset( $_POST['addButton'] ) )
{
    $date = addslashes( $_POST['date'] );
    $amount = addslashes( $_POST['amount'] );
    $remark = addslashes( $_POST['remark'] );


    $sql = "INSERT INTO transaction (date, amount, remark)
    VALUES ('$date', '$amount', '$remark')";
    mysqli_close($conn);

}

?>

<!DOCTYPE html> <html> <head> <title> Ashok Bishnoi - Add New Transaction </title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <link href='http://fonts.googleapis.com/css?family=Laila:600' rel='stylesheet' type='text/css'> </head> <body>

<div class="container"> <form role="form" action="" method="POST"> <div class="form-group"> <label for="Date">Date:</label> <input type="date" class="form-control" name="date" id="date" placeholder="Date when transection Happened."> </div> <div class="form-group"> <label for="amount">Amount:</label> <input type="number" class="form-control" name="amount" id="amount" placeholder="The amount which i received or expense."> </div> <div class="form-group"> <label for="remark">Remark:</label> <textarea class="form-control" name="remark" id="remark" placeholder="Details about Transaction"></textarea> </div> <button type="submit" name="addButton" class="btn btn-success">Add Now</button> <a href="index.html" class="btn btn-danger">Cancel</a> <a href="viewall.php" class="btn btn-warning">View All</a> </form> </div>

<footer> <p class="copy"> © <?php echo Date("Y");?> AshokBishnoi.com All Right Reserved </p> </footer>
</body> </html>

1 Answer

Mateusz Hyla
Mateusz Hyla
4,658 Points

You dont execute your query.

You just asign to variable $sql the query body but you have to execute it like :

$conn->query($sql);

Something like that, you can search for opther execution options under this link :

  1. http://php.net/

BTW. I got very dark font with your PHP source code unreadable, I just highlight it but It is probably my LInux problem or Chromium :p.

Cheers

Thanks Buddy :)