Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konrad Pilch
Konrad Pilch
2,435 Points

Whats wrong with this code? SQL PHP Database

HI,

I want to add records after the user has filled the data in the input.

This code doesnt work :

if(!isset($firstname) || $firstname == '' || !isset($lastname) || $lastname == ''  || !isset($email) || $email == ''){
        $error = "Please fill the details";
        header("Location: index.php?error=".urldecode($error));
        exit();
    } else {
    //Inserting Records to the Darabase/Attempt insert query execution
    $querry = "INSERT INTO persons (first_name, last_name, email_address) VALUES ('$firstname', '$lastname', '$email')";


    if(mysqli_query($con, $querry)){
        echo "Records added successfully.";
        header("Location: index.php");
        exit();
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
    }

}

The whole code of it is :

database.php

<?php

//Connecting to the database
$con = mysqli_connect("localhost", "root","root", "dbname");

// Check connection
if($con === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

if(isset($_POST['submit'])){

    $firstname = mysqli_real_escape_string($con, $_POST['firstname']);
    $lastname = mysqli_real_escape_string($con, $_POST['lastname']);
    $email = mysqli_real_escape_string($con, $_POST['email']);






if(!isset($firstname) || $firstname == '' || !isset($lastname) || $lastname == ''  || !isset($email) || $email == ''){
        $error = "Please fill the details";
        header("Location: index.php?error=".urldecode($error));
        exit();
    } else {
    //Inserting Records to the Darabase/Attempt insert query execution
    $querry = "INSERT INTO persons (first_name, last_name, email_address) VALUES ('$firstname', '$lastname', '$email')";


    if(mysqli_query($con, $querry)){
        echo "Records added successfully.";
        header("Location: index.php");
        exit();
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
    }

}

}

// Close connection
mysqli_close($con);
?>

the index is simple

<?php include 'database.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Records Form</title>
</head>
<body>
<form action="database.php" method="post">
    <p>
        <label for="firstName">First Name:</label>
        <input type="text" name="firstname" id="firstName">
    </p>
    <p>
        <label for="lastName">Last Name:</label>
        <input type="text" name="lastname" id="lastName">
    </p>
    <p>
        <label for="emailAddress">Email Address:</label>
        <input type="text" name="email" id="emailAddress">
    </p>
    <input type="submit" value="Add Records">
</form>
</body>
</html>

2 Answers

Konrad Pilch
Konrad Pilch
2,435 Points

Very funny xd i was missing name for my button. It should be like this :

<input type="submit" name="submit" value="Add Records">

As a preface, I have just begun learning databases, but I do not understand the logic of your variables firstName, lastName, and email and how they are then used. It looks like you assign them a SQL statement, then try to use the same variable to be the actual value put in the database. I would think you need the input data in a separate variable that is inserted into the database. But I could be completely wrong.

Konrad Pilch
Konrad Pilch
2,435 Points

Hi, xd i got it working.

Well, everyone do it different.

I made it like this:

When you type something in the input, i basicaly made an variable for it lastname etc.. so then to use it , i just write lastname in my querry instead of writing $_Post etc.. which will save me time and make less writing as well as make it clean.

I hope you get me. If you want to know more , or something , i can explain you in detail what exactly im doing. But this should cover.

I understand assigning the names to a variable and would do the same. I don't understand what is before $_POST: mysqli_real_escape_string($con, $_POST['firstname']);

I Just have not got that far in the course yet.

Konrad Pilch
Konrad Pilch
2,435 Points

The best thing is to check out the documentation or past it in google . Im learning it this way :)

I agree. I am about to start a project that will use everything i have learned by the time it is really done. There is no better way to really learn than to create something you are really interested in.

Konrad Pilch
Konrad Pilch
2,435 Points

I learned tones from documentation. I do what I need, meaning I want to make e.g dacebooknloke site so.I will need to have register, log in log out etc.. So I'm doing this now. And i get it simple as first, nd then I will improve on it with security and stuff.

I kind of find hard here on treehouse to do learn PHP: p it has database foundation, that's useful, then some others, but their not as good, maybe I should just go throw the tutorials and just look the logic and hopefully I will be able to write it in my own words, that's how I learn. Indo a project with tutorial on the side and then without looking i do it again. That's how I learn and that's the only way to learn for me. My own experience

Before I started studying here I built several web sites using Joomla. I needed robust security with log in, classes, and some features changing for specific users. It was fairly easy to do. My biggest problem has really been the CSS, but that could be because of a number of reasons.

I really like Joomla, but it's coding is beyond what I understand right now.

Konrad Pilch
Konrad Pilch
2,435 Points

CSS is the scariest xd i know i know css but I feel like I don't, I dunno why. I mean, I probably can do anything, i just need more hands on it. I use bootstrap and i can customise it however I want but if I don't use bootstrap, i can do the same thing. I really just look up on internet nd find out :). Also, css is kind of easy. There are like kain points to it like infine block, padding and margins and table cells and hmm maybe that's all, once you know hownto bud it, it should be ok: p whenever i make websites, i feel like I can do it, but I dint know, it's weird: p really, i think experience is needed more.