Niloofar24 15 Posting Whiz

@lps, your explanation was good but would you please explain it one more time but a little more clear please?! Sorry i didn't understand what should i do exactly well (just because my english is not good enough, so sometimes i have to ask friends to explane one more time and more clear to get the point better, thank you:) ).

Niloofar24 15 Posting Whiz

THank you @broj1, it works. But i had to change this:

if(empty(trim($_POST["title"])) || empty(trim($_POST["author"])) || empty(trim($_POST["content"]))) {

to this:

if(empty($_POST["title"]) || empty($_POST["author"]) || empty($_POST["content"])) {

Because with using trim() i got error.

I read the link you gave me, but could you please explain every line i will point to? I can't understand them wellm need some more clear explanation, thank you.

$sql = "INSERT INTO Posts (Title, Author, Content)

//why you have used : sign before each one? what does it do?
VALUES (:title, :author, :content)";

// what is $stmt for?
// what does bindParam do exactly?
$stmt->bindParam(':Title', $title);
$stmt->bindParam(':Author', $author);
$stmt->bindParam(':Content', $content);
$conn->exec($sql);
} catch(PDOException $e) {

// why you have typed echo $sql in this line?
//what does it echo and print on the page?
//(sql is the variable here that holds an order, the order of inserting data into table, right?
//so the $sql variable has not anything to echo and print, right?
//maybe is misunderstanding for me.
echo $sql . "<br>" . $e->getMessage();
Niloofar24 15 Posting Whiz

Hello.
There is an html table in admin page, with head columns (POST ID | POST TITLE | POST AUTHOR | ACTION).
And in each row, will contain a post id, a post title, a post author that gets from database.
And in the Action column, there is a "delete" word that is a link with href="#" for now.
I want the script when i click on the delete word in each rows, erase that row of data from the table of the database and from the html table.
I don't know how should i do that. Just need guidance to understand what i can do for that, thank you.

Niloofar24 15 Posting Whiz

Thank you @diafol.

Niloofar24 15 Posting Whiz

What do you mean by this? Sorry my English is not good enough, so i didn't understand what you mean by this:

And for security reasons use prepared statements.

Yes it works. Now when i click the submit button, no blank rows will be added into table and no error appears on the top of the page. Thank you.

But there is an other problem now.
First i login to this page(the page of writting posts) from login.php page by entering username and password, but suddenly after entering to the page of writting posts, the message "You forgot to enter some required data" will appear on the top of the page, but i haven't clicked submit button yet!
How can i fix it? I don't want script to show this message exactly after login to page without any action.

Niloofar24 15 Posting Whiz
<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $title = $_POST['title'];
    $author = $_POST['author'];
    $content = $_POST['content'];

    if(empty($_POST["title"]) || empty($_POST["author"]) || empty($_POST["content"])) {
        echo "You forgot to enter some required data";
    } else {
        die($sql);
        $sql = "INSERT INTO Posts (Title, Author, Content)
        VALUES ('$title', '$author', '$content')";
    }

    $conn->exec($sql);
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>


<html>
<head>
<title>Admin Area</title>
</head>
<body>
<h1>Welcome <?php echo $_POST["username"]; ?> To Admin Area</h1>

<h2>Feel free to write your new post:</h2>
<form method="post">
   Title: <input type="text" name="title">
   <br><br>
   Author: <input type="text" name="author">
   <br><br>
   Content: <textarea name="content" rows="10" cols="40"></textarea>
   <br><br>
   <input type="submit" name="submit" value="Submit">
</form>


</body>
Niloofar24 15 Posting Whiz

I add that part after line 4, after }else{.
The result was the same as before. No blank rows has been addded into table but this error appears on the top of the page:

You forgot to enter some required data
SQLSTATE: General error: trying to execute an empty query 
Niloofar24 15 Posting Whiz

No i can!...wait i think maybe i have not understand the main point correctly! i searched for mysql manual and check some of the links, but didn't find any definition for <> sign there... it seems i was looking for wrong topic yes?!

Please tell me about that. I WANT TO LEARN IT SERIOUSLY.

Niloofar24 15 Posting Whiz

I couldn't find it, can you please explain it to me?

Niloofar24 15 Posting Whiz

@broj1, i tested your code, the result was almost the same as mine. No blank rows will be added into table and it's good, but that error is still appeared on the top of the page, and now just this sentence "You forgot to enter some required data" is appeared on the top of that error.

Niloofar24 15 Posting Whiz

Hi, as you know i'm creating my own CMS.
This is a part of my code:

     if (!empty($_POST["Title"]) && !empty($_POST["Author"]) && !empty($_POST["Content"])) {
         $sql = "INSERT INTO Posts (Title, Author, Content)
         VALUES ('$title', '$author', '$content')";
     }

I want the script to check if Title Field and Author field and Content field are empty, stop sending blank rows into table. Because when i click the submit button while all these fields are empty and then check the table, i see blank rows added into table.

Now with the code above, no blank rows will be added to table but when i click the submit button while all those fields are empty, this error will be appeared on the top of the page suddenly!
General error: trying to execute an empty query
Well, how can i remove that error from the top of the page?!

Niloofar24 15 Posting Whiz

What is <> and '' that you have used between title and Limit?
What do they mean?

Niloofar24 15 Posting Whiz

Yes, it's my second day in Java. I decided to create a chat messenger so i have started to learn Java. Of course i'm not new to programming, i code in c++, Python and php.
I thought maybe it's better to start with a good tutorial for creating chat messenger from the first step.

Niloofar24 15 Posting Whiz

This is my last code changes:

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT ID, Title, Author, Content FROM Posts";
    $result = $conn->query($sql);

    foreach($result as $row) {
        echo $row['ID'] . ") " .  $row['Title'];
        echo "<br>" . " by: " . $row['Author'];
        echo "<br>";
        echo $row['Content'];
        echo "<br><br><br>";
    }

} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

With this code, all posts in the Posts table will be printed into the page and users can see all post one after another.

Now how can i ask the script to echo only 3 posts in each page?
I think @lps mentioned this limitation in his code, but i didn't understand it clear.

And there must be a "NEXT" button to direct users into next pages to see next 3 posts, correct? So how should i do it? (My code above is in "user-view-page.php" file, so do i need more php pages to show next 3 postes with NEXT button?!)
For now, just need your guidance, thank you.

Niloofar24 15 Posting Whiz

Hello.
I'm looking for a good tutorial for creating a chat messenger in java. I checked this link but it was not good and clear, was poor tutorial without clear explanation and details.
Can you help me with another tutorial? Tutorial video or text both are good.
(Let me mention that i'm new to java)
Thank you.

Niloofar24 15 Posting Whiz

I changed my code to this:

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT Title FROM Posts";
    $result = $conn->query($sql);

    foreach($result as $row) {
        echo $row['Title'];
        echo "<br>";
    }

} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

And the output was all Titles. Is it good or is better to write it in another way?!

And something more... in my Posts table there are some rows that are completely empty (they have been created when i clicked the Submit button on the editing post area without typing any title or author name or any content.)
Now whith the code above, those empty rows will be printed in the output too. I mean sth like this:

First post



my first post here...

Second post
a new post

You can see blank lines between titles that that are printed. How can i tell the script not to print those blank lines?

Niloofar24 15 Posting Whiz

Can you guide me more clear please?
There is 4 fields in my Posts table: ID - Title - Author - Content
How should i change my code and get data from those column?

Niloofar24 15 Posting Whiz

Well, still can't find the right way to solve it.

@mattster can you tell me why it doesn't work?

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT * FROM Posts WHERE";
    $result = $conn->query($sql);

    foreach($result as $row) {
        echo $row['Title'];
        echo "by: " . $row['Author'];
        echo "<br>";
        echo $row['Content'];
        echo "<br><br>";
    }

} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
?>
Niloofar24 15 Posting Whiz

Hello again, couldn't check here for almost 3 weeks but now i'm here again to continue. Stil need help to solve the user-view-page.php problem, i can't get and echo data from Posts table.

@mattster, i used the code you gave me but i faced with a blanck page.

@lps, i used your code but look at this:
$sql = "SELECT * FROM Post ORDER BY post_date, post_time";
There is no post_data and post_time column in my table.

@diafol, SELECT field1, field2... FROM table what do you mean by "field" from table? Sorry i didn't understand where exactly you pointed with the "field".

Niloofar24 15 Posting Whiz

So i shoud chang it to this?
$sql = "SELECT * FROM Posts";

And haow should i echo? It this model correct?!

foreach($result as $row) {
    echo result['Title'];
    echo "by: " . result['Author'];
    echo "<br>";
    echo result['Content'];
    echo "<br><br>";
}
Niloofar24 15 Posting Whiz

Hello again.
I have insert title, author and content data into table with admin-are page and now want to create a user-view-page.php that everybody enter into that page and see all posts one after an other, something like this model:

first title by first author
first content

seconr title by second author
second content

.
.
last title by last author
last content

This is my code but it doesn't work:

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT * FROM Posts WHERE";
    $result = $conn->query($sql);

    foreach($result as $row) {
        echo $result;
    }

} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
?>
<html>
<head>
<title>user view page</title>
</head>
<body>
<?php
echo $result;
?>
</body>
</html>

What changes this code needs? Where is the problem? With sql syntax or with php code?

Niloofar24 15 Posting Whiz

Yes right, thank you.

Niloofar24 15 Posting Whiz

OOPSS!! sorry i found the problem.
The problem was here Content CHAR(100), there was no need for ,.

Niloofar24 15 Posting Whiz

Well, me again!
Have an other problem with creating an other table. I want to create a Posts table with this code:

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = 'CREATE TABLE Posts(
    ID INT(128) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    Title CHAR(100),
    Author CHAR(100),
    Content CHAR(100),
    )';

    $conn->exec($sql);
    echo "Table Posts created successfully.<br>";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

But when i type the url of that file in the browser, this error appear on the page:

CREATE TABLE Posts( ID INT(128) UNSIGNED AUTO_INCREMENT PRIMARY KEY, Title CHAR(100), Author CHAR(100), Content CHAR(100), )
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 6

I didn't understand what is the syntax error for? The error says at line 6, but line 6 is empty!

Niloofar24 15 Posting Whiz

Yessss!! It works:) Really thank you @lps for all explanation and answer.

The last problem was because of this part:

$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";

Instead of 4 space i set 8 space before each line.

Niloofar24 15 Posting Whiz

Oh no! I still face with a blank page!

Niloofar24 15 Posting Whiz

I understood, so the code must be like this:
(But so why i still face with a blank page?)

<?php
if(isset($_POST['submit'])){
    $servername = "localhost";
    $dbname = "mydbname";
    $dbusername = "mydbusername";
    $dbpassword = "mydbpassword";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $username = $_POST['username'];
        $password = $_POST['password'];
        $password = md5($password);

        $sql = "SELECT Password FROM Users WHERE Username = '$username";
        $result = $conn->query($sql);
    }

        if(count($result) > 0) {
            if ($password == $row['Password']){
                header("Location: admin-area.php");
            }else{
                echo "Password incorrect!";
            }
        }else{
            echo "Username incorrect!";
        }

        $conn->exec($sql);
    } catch(PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }

    $conn = null;
}
?>
<html>
<head>
    <title>Login Page</title>
</head>
<body>
<form action="admin-area.php" method="post">
Username: <input type="text" name="username"><br>
Password : <input type="text" name="password"><br>
<input type="submit" value="submit">
</form>
</body>
Niloofar24 15 Posting Whiz

The User table has 3 columns, ID, Username and Password.

Well i still need more explanation to understand it well.
These two lines get the password of the $username and put it into $rsult, am i right?! Correct?!

        $sql = "SELECT * FROM Users WHERE Username = '$username";
        $result = $conn->query($sql);

The part you gave me in 5 lines, check to see if the $result value is not empty with count() > 0 correct? And then print the row, will print the password right?

If not please explain your 5 lines code for me, thank you. I want to understand each line exacty and very well.

I thought i should put those 5 lines before my if part and change this if ($password == $result) { to this if ($password == $row) { , but i faced with a blank page. Why? What was the reason.
It seems i didn't understand sql well.

(Thank you for your explanation and answers)

Niloofar24 15 Posting Whiz

Yes you are right, i just copied and pasted the code to see if it works well and the problem is solved, then focuse on the code to understand it well. (And that is because i'm in a hurry to solve this problem and complete the creation of my primary version of CMS today, i know it's not correct.)

Is it possible to help me with this more please? I didn't understand the main problem here, can you please tell me what should i do for this part and make it more clear for me plase? Just need a hint to understand the way. Thank you. (It seems i didn't get the exact problem of the code.)

he queried result from the 'Users' table will be all columns as you select all in you query, therefore the comparism in if ($password == $result) { will have to be reviewed again.

Niloofar24 15 Posting Whiz

@lps, i copied your code and pasted it, but there is a problem. If i enter a wrong password, it login into admin-area.php! And if i enter a wrong username, it again enters into admin-area.php!
But it should login only if the username and password both are correct.

Niloofar24 15 Posting Whiz

Is this if part correct?

 if $password == $result {
header("Location: admin-area.php");
}

I want the scipt to check the hash string of the password that user enters with the hash string of the password that user has insert into table with register form before.

Niloofar24 15 Posting Whiz

Hello.
This is my CMS login page; login.php:

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $username = $_POST['username'];
    $password = $_POST['password'];
    $password = md5($password);

    $sql = "SELECT * FROM Users WHERE Username = '$username";
    $result = $conn->query($sql);

    if $password == $result {
        header("Location: admin-area.php");
    }



    $conn->exec($sql);
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>



<html>
<head>
    <title>Login Page</title>
</head>
<body>
<form action="admin-area.php" method="post">
Username: <input type="text" name="username"><br>
Password : <input type="text" name="password"><br>
<input type="submit" value="submit">
</form>
</body>

But when i type the complete url of login.php page in the browser, i face with a blank page without the login form. Where is the problem?!

Niloofar24 15 Posting Whiz

Yesssss! It works :)
Thank you @lps.

Niloofar24 15 Posting Whiz

I think the reason must be the action= wich is not in the <form> tag. The action sets the Destination where data should be sent to, correct? So here that the Destination is not an other php file, is db table, what can i type for action for the <form> tag?

Niloofar24 15 Posting Whiz

Hello.
Look at this file please:
register.php

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "INSERT INTO Users (Username, Password)
    VALUES ('$username', '$password')";

    $conn->exec($sql);
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

<html>
<head>
    <title>Register Page</title>
</head>
<body>
<form method="post">
Your Username: <input type="text" name="username"><br>
Your Password: <input type="text" name="password"><br>
<input type="submit" value="submit">
</form>
</body>

Why it can't send the username and password that gets from those two fields to the table? I checked the table, the ID was incresed everytime but the fileds of username and password were empty on the table.

Niloofar24 15 Posting Whiz

Well it seems the tutorial videos i'm using are not good and have so many problems. It's better to recreate the CMS again from the first step, using an other way.

Niloofar24 15 Posting Whiz

I deleted echo "Login successful"; but stil get this "Please check your login details!" message above the login fields. And also there is no log_error.

Niloofar24 15 Posting Whiz

Thank you @matrixdevuk.

Thank you @mattster.

Niloofar24 15 Posting Whiz

What is session_start for?

Niloofar24 15 Posting Whiz

Look at this please:

<?php
session_start();
include('includes/functions.php');

if(isset($_POST['login'])) {
    if(isset($_POST['username'])) {
        if(isset($_POST['password'])) {
            $username = $_POST['username'];
            $query = mysql_query("SELECT * FROM users WHERE Username = '$username'") or die (mysql_error());
            $user = mysql_fetch_array($query);

            if(md5($_POST['password']) == $user['Password']) {
                echo "Login successful";
                $_SESSION['user'] = $user['Username'];
                header("Location: index.php");
            } else {
                echo "Please check your login details!";
                include('login.php');
            }
        } else {
            echo "Please check your password!";
            include('login.php');
        }
    } else {
        echo "Please check your username!";
        include('login.php');
    }
} else {
    echo "Please check that you filled out the login form!";
    include('login.php');
}
?>

When i try to login, i get this error:
Please check your login details!

Niloofar24 15 Posting Whiz

I replaced the last one with this:

    <?php
    session_start();
    include('includes/functions.php');

And for two other files, i moved the html part into after session_start().

But still i have problem, nothing changed. I can't login into admin area and will get the error to check details, i mean the error message for login form code. But now there is no log_error, so how should i find the problem?

I'm using a tutorial video. I typed all codes exactly like the vido but the result was different. In that video, session_start() was not the first thing on the file, was like what i post in my seccond post here, but there was no problem on the video.

Niloofar24 15 Posting Whiz

Well, in 2 files, session_start() is not the first thing in the page, is like this:

<html>
<head>
<title>page title</title>
</head>
<body>
<?php
session_start();

And in one other file is like this:

<?php
include('includes/functions.php');
session_start();

And what is BOM?
How can i understand if all files are saved az UTF-8 without BOM or not? What should i do for that?

Niloofar24 15 Posting Whiz

Hello.
I'm creating a CMS. I've got this log_error.
Can you explain the problem for me if the error message is clear enough for you to understand it? I'm new to PHP.
the log_error says:

PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent

Niloofar24 15 Posting Whiz

Thank you @snippsat. I have 2 questions:

print link['title'] what is title? The alt of the <img> tag?

except KeyError: what does keyError mean here? What the keyword keyError is for?

Niloofar24 15 Posting Whiz

I want the script to find all the names of singers that starts with the latter "A" in a url page. First i asked my script to check the content of all <il> and <td> tags but now i want to change it.
I want the script to check the url page i gave to it and then find and save all the names of singers on that page that starts with the letter "A". It's my homework and i don't know how to do that. Friends suggest me using dictionary to check words with it to find if they are names of human or not. But the homework asks me not to do that.
So now i want my script to find every word that start with "A" in that url page and print it for me. Then i should find a way to ask my crawle just save those words starting with "A" that are singers names.
Very difficult!!!

Niloofar24 15 Posting Whiz

I tried and it's type was <type 'unicode'>.

Niloofar24 15 Posting Whiz

Thank you @fireburner29. I will check it.

Niloofar24 15 Posting Whiz

Well i changed it into this:

for word in soup.text:
    if word.startswith('A'):
        print word

But now the output is this: (many A letter)

A
A
A
A
A
.
.
Niloofar24 15 Posting Whiz

Hello!
How can ask my scipt to print every word in a url page that starts with the letter "A" in this case?
This is my code:

from bs4 import BeautifulSoup
import urllib2

url = 'http://www.thefamouspeople.com/singers.php'
html = urllib2.urlopen(url).read()
soup = BeautifulSoup(html)
for word in soup.text:
    if soup.text.startswith('A'):
        print soup.text

But it doesn't print anything for output.

Niloofar24 15 Posting Whiz

Thank you @DaniUserJS, i will go to watch them right now :) and hope to learn it well.