Niloofar24 15 Posting Whiz

Thank you @lps, it works ok now with using Content: <textarea name="content" rows="10" cols="40"><?php echo str_replace("<br />","",$row['Content']); ?></textarea> I think you have edited your last post.

$data = ereg_replace("(\r\n|\n|\r)", "<br />",$text);, and <textarea"><?php echo preg_replace('/\<br(\s*)?\/?>/i', PHP_EOL, $data); ?></textarea/>

I tested the code above but it doesn't work well, has the same problem i faced with last time.

I know here is not the place to say that but:
Sorry. I don't expect people here doing all the job for me at all. I dont post my problems here in the forum to get the correct code to copy and paste in my script. As i have mentioned before in my some topics, i need people's guidance not the exact answers. Every time i face with a new problem i try alot to find the way and do it myself, if i don't find the solution finally, i post my question here to get guidance. Sometimes i have problem even with the code people gave me and i don't find how to correct it myself so i have to ask.
My last question was small problem for you and maybe for many of members but not for me. I just want to learn. (LEARNING is more important than creating that cms for me.)

...anyway, thank you for your all answers and helps.

Niloofar24 15 Posting Whiz

I deleted that html part from delete-action.php file, but still when i click delete button, the message "Success" appears above the html table but the html table won't update itself.

Niloofar24 15 Posting Whiz

When i click delete button, the Success message appears above the html table but the html table doesn't update itself, i have to refresh the page to check the html table again and after refreshing the page, the post i have deleted is removed from there.

The delete-action.php file:

<?php
    include('connection.php');
    try {

        $id=$_POST['id'];
        $sql = "DELETE FROM Posts WHERE ID = '$id'";
        $conn->exec($sql);
        echo "Success"; // the word here must be same as ajax comparism
    } catch(PDOException $e) {
        echo "Query error:".$sql . "<br>" . $e->getMessage();
    }
    $conn = null;
?>

<html>
<head>
<title>Delete-action</title>
</head>
</html>

And the script for admin page:

<?php
session_start();

// If the user is not logged in send him/her to the login form
if ($_SESSION["Login"] != "YES") {
header("Location: user-view-page.php");
}
include('connection.php');
try {
    $sql = "SELECT ID, Title, Author, Content FROM Posts";
    $result = $conn->query($sql);
    echo "<h4>LOGGED IN AS: " . $_SESSION["Username"] . "</h4>";
    echo "<p id='err_msg'></p>";//add this line for error message display, may add styling to this for bold and red color words
    echo "<table cellspacing='0' cellpadding='20' border='1px solid black' border-collapse='collapse'>";
    echo "<tr><th> POST ID </th><th> POST TITLE </th><th> POST AUTHOR </th><th> ACTION </th></tr>";
    foreach($result as $row) {
        echo "<tr class='tr'><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a id='".$row['ID']."' class='btn_dlt' href='#'> delete </a> <br> <a href='edit-page.php?post=" . $row['ID'] . "'>Edit</a></td></tr>";
    }
    echo "</table>";
} catch(PDOException $e) {
    echo "Query error:".$sql . "<br>" . $e->getMessage();
}
$conn = null; …
Niloofar24 15 Posting Whiz

This is what i typed in the textarea for a new post and inserting into db with $content = nl2br(htmlentities($_POST['content'])); :

hi
hi
hi

Then i checked db table and it was like this:

hi<br />
hi<br />
hi

Then i checked to see how the script would echo the post on the page and it was:

hi
hi
hi

It seems to be ok but...
After that i went to edit the post.
When i clicked the edit button and entered into edit page, the content of the textarea was like this:
(with Content: <textarea name="content" rows="10" cols="40"><?php echo str_replace("<br />","\n",$row['Content']); ?></textarea>)

hi

hi

hi

It was not what i wanted to face with but anyway i edited:

hi

hi

hi //here i press the Enter key once and add a new word.
welcome

I checked db table again and it was like this:

hi<br />
<br />
hi<br />
<br />
hi<br />
welcome

I checked to see how the script would echo it on the page:

hi

hi

hi
welcome

And finally when i wanted to edit it again the textarea was like this:

hi



hi



hi

welcome
Niloofar24 15 Posting Whiz

Thank you @lps for explanation.

You forgot to answer my question about updating the page and html table at the time of clicking the Delete button.

Niloofar24 15 Posting Whiz

@lps, with using $content = nl2br(htmlentities($_POST['content'])); in my script, i can type a text in the textarea and use ENTER key for line breaking. Then when i send the content into db table, data will be saved there with <br> tags.
But when i want to edit that content...
The script gets data from db table and show it in the textarea again for editing. So at this time i see the text with <br> tags in it and if i add a new sentence to the text and click the submit button, data will be updated in the db, and after that when i echo the post on the page for reading, the text contains <br> tags.
How should i solve it?

But if you wish to assign values back to the textarea, remember to str_replace the <br /> back into \n.

Would you please explain it more clear?
Is it about the problem i pointed above?

Niloofar24 15 Posting Whiz

Well, it doesn't work, because in every page we will have 3 posts and with using $index = 1; and $index++; we will have posts with id number 1 to 3 in each page. In the first page will have posts with id number 1-2-3 and in the second page, posts will be changed but the id number wich is $index infact, won't change.

Niloofar24 15 Posting Whiz

I read here and here, but would you please give me a simple example of how to use it in a text? Like what we see in the second link i pointed?

It means users should use and type |nl2br while they are typing their text in the textarea themselves?

Niloofar24 15 Posting Whiz

Oh! i'm sorry i found the problem. I forgot to change this part:

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

@lps i used that way but faced mhith some errors.

Niloofar24 15 Posting Whiz

Hi.
In my CMS, when i type a content for a new post in the textarea, like this:

one
two
three
four
five
six

and then click the submit button, it will save in the db table like the way i have typed, every word in a separate line.
But when the script echo the post on the page, i see all words are in one line like this:

one two three four five six

Why? Do i have to type tags like <br> while typing the content on the text area?! But users must feel free to write their posts without thinking about using html tags while they are typing!

RikTelner commented: Good question, I'd like to know too. +2
Niloofar24 15 Posting Whiz

Hello everybody!
There is a problem with my db table. For example i send 6 posts (with title, author and content) to the db table one after an other, so their post's id would be 1-2-3-4-5-6.
Then if i delete the post number 6 and then write a new post (which it's id must be 7), after that when i echo all posts on the page to see them, i see posts with ids 1-2-3-4-5-7 !!
How can i solve this problem? When i delete the post with id 6, the next post should be set there into id 6.

Niloofar24 15 Posting Whiz

This is very sad.... for now just want to learn Java. Thank you for your answers.

Niloofar24 15 Posting Whiz

Yesss :) It works! Thank you @lps.
I used the ajax part, the php loop for the html layout and the delete-action.php.

I have some questions.
First, i thought with the ajax part, when i click the delete link, data will be deleted from db and also from html table at the same time, but when i click, data will be deleted from db and a success message will be printed above the html table at the same time.
Now i have to refresh the page to update the html table. Can i do that with macking changes in the ajax code you typed above?

And would you please explaine these lines for me?

 //What to you mean by "for later usage"?
 var btn_dlt = $(this); //assign the clicked button into a variable for later usage



 //I didn't understand this part:
 if(msg == "Success"){
/*using the clicked button to find the nearest parent with the class name to delete*/
btn_dlt.closest('.tr').remove();

Thank you.

Niloofar24 15 Posting Whiz

Yes it work ok now. Thank you @cereal for clear explanation and for updating my code:) I understood it well.

cereal commented: You're welcome! +13
Niloofar24 15 Posting Whiz

Thank you @lps. I understood. I will do what you recommend to me.

Niloofar24 15 Posting Whiz

Thank you @AleMonteiro, and tahnk you @JorgeM for your guidance.

And for everyone that may have the same problem as what i had; this link is a very clear and simple explanation for how to use sessions.

Niloofar24 15 Posting Whiz

Hello.
Please look at 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);

        $id=$_GET['id'];

        $sql = "DELETE FROM Posts WHERE ID = :id";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $id, PDO::PARAM_STR);
        $stmt->execute();
        header("location: admin.php");
    } catch(PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }            
    $conn = null;
?>

I see some tutorial examples put this part in a for example "connection.php" file:

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

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

and then using include(connection.php); in their php files. Now if you look at my code above, you see there is this line in it:

        try {

So, what should i do? Can i forget to use try-catch in my scripts? If not so how can i put that part of code in the connection.php file to use with include()?

Can i put 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);

in the connection.php file while the try block left open?! and be closed in the other php files?

Niloofar24 15 Posting Whiz

My last code changes is:

<?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);

    echo "<h1>Welcome to Admin area</h1>";

    echo "<table cellspacing='0' cellpadding='20' border='1px solid black' border-collapse='collapse'>";
    echo "<tr><th> POST ID </th><th> POST TITLE </th><th> POST AUTHOR </th><th> ACTION </th></tr>";

    foreach($result as $row) {
        echo "<tr><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a href='delete-action.php?id=" . $row['id'] . "'> delete </a></td></tr>";
    }

        echo "</table>";

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

And this is delete-action.php file:

<?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);

        $id=$_GET['id'];

        $sql = "DELETE FROM Posts WHERE ID = :id";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $id, PDO::PARAM_STR);
        $stmt->execute();
        header("location: admin.php");
    } catch(PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }            
    $conn = null;
?>

I have used this link, it should works but it didn't!... Is there anyone to help me with that??!!

Niloofar24 15 Posting Whiz

This is my last update, i tried to use the script of this link, (look at PHP File: deletephp.php there), but it didn't work for me.
Mine:

<?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);

    echo "<h1>Welcome to Admin area</h1>";

    echo "<table cellspacing='0' cellpadding='20' border='1px solid black' border-collapse='collapse'>";
    echo "<tr><th> POST ID </th><th> POST TITLE </th><th> POST AUTHOR </th><th> ACTION </th></tr>";

    foreach($result as $row) {
        echo "<tr><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a href='admin.php?del={" . $row['ID']. "}'><input type='button' class='submit' value='Delete'></a>";
    }
        echo "</table>";

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



if (isset($_GET['del'])) {
    $servername = "localhost";
    $dbname = "blenderl_CMS";
    $dbusername = "blenderl_nilofar";
    $dbpassword = "andishe#n";

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

        $del = $_GET['del'];

        $sql = "DELETE FROM Posts WHERE ID = :del";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':del', $del, PDO::PARAM_STR);
        $stmt->execute();
    } catch(PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }            
    $conn = null;
}

?>
Niloofar24 15 Posting Whiz

Nobody can help me with this problem?! No more suggestion?
I have sent my last update that still doesn't work.

Niloofar24 15 Posting Whiz

Thank you @cereal for clear explanation.
Well, i have deleted that file unfortunatelly.

Now this is my last update for now: (ofcourse i have not changed the design of the page for now, but will redesign it later after i finished editing the php part of the page.)

Please look at it's php part and see how it is? I think it's not good or standard because of 2 more php part i have added to it, is it?

<?php

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

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

        $total = $conn->query("SELECT COUNT(id) as rows FROM Posts")
                 ->fetch(PDO::FETCH_OBJ);

        $perpage = 3;
        $posts = $total->rows;
        $pages = floor($posts / $perpage);

        # default
        $get_pages = isset($_GET['page']) ? $_GET['page'] : 0;

        $data = array(

            'options' => array(
                'default' => 0,
                'min_range' => 0,
                'max_range' => $pages
                )
        );

        $number = trim($get_pages);
        $number = filter_var($number, FILTER_VALIDATE_INT, $data);
        $range = $perpage * $number;

        $prev = $number - 1;
        $next = $number + 1;

        $stmt = $conn->prepare("SELECT ID, Title, Author, Content FROM Posts LIMIT :limit, :perpage");
        $stmt->bindParam(':perpage', $perpage, PDO::PARAM_INT);
        $stmt->bindParam(':limit', $range, PDO::PARAM_INT);
        $stmt->execute();

        $result = $stmt->fetchAll();

    } catch(PDOException $e) {
        $error = $e->getMessage();
    }

    $conn = null;



if(isset($_POST['submit-register'])) {
$servername = "localhost";
$dbname = "blenderl_CMS";
$dbusername = "blenderl_nilofar";
$dbpassword = "andishe#n";

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 = "INSERT INTO Users (Username, …
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);

    echo "<table cellspacing='0' cellpadding='20' border='1px solid black' border-collapse='collapse'>";
    echo "<tr><th> POST ID </th><th> POST TITLE </th><th> POST AUTHOR </th><th> ACTION </th></tr>";

    foreach($result as $row) {
        echo "<tr><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a href='admin.php?post=" . $row['ID'] . "'>Delete</a> <br> <a href='edit-page.php?post=" . $row['ID'] . "'>Edit</a> </td></tr>";
    }
        echo "</table>";

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


if($_GET['POST']){
    $servername = "localhost";
    $dbname = "blenderl_CMS";
    $dbusername = "blenderl_nilofar";
    $dbpassword = "andishe#n";

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

        $delete_id = $_GET['POST'];

        $sql = "DELETE FROM Posts WHERE ID = '$delete_id'";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':title', $title, PDO::PARAM_STR);
        $stmt->bindParam(':author', $author, PDO::PARAM_STR);
        $stmt->bindParam(':content', $content, PDO::PARAM_STR);
        $stmt->execute();
    } catch(PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }            
    $conn = null;
}

?>

<html>
<head>
<title>admin</title>
</head>
<body>
<hr>
<?php echo "<a href='writing-post.php'>writing new post</a>"; ?>
</body>
</html>

But it doesn't work. I tried to send each post's id by using <td> <a href='admin.php?post=" . $row['ID'] . "'>Delete</a> (admin.php is the address of this page) to this part if($_GET['POST']){.

Why it doesn't work?

Niloofar24 15 Posting Whiz

Why you set $error = FALSE; and $result = FALSE; to False?

You changed this part:

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

to this:

} catch(PDOException $e) {
    $error = $e->getMessage();
}

Was the first model incorrect?! Should i change this part of every php files in to the second model?!

What does this if statment checke?

 if($error)
{
echo "<div class=\"error\"><strong>Database Error:</strong> $error</div>";
}

And would you please explain this too?

 if($result && count($result) > 0)
{

Why both result and count(result)?

Thank you, you helped me alot specialy with your clear explanation.
Good luck my friend.

Niloofar24 15 Posting Whiz

Yesss :) it work nice thank you again @cereal!
But what was the problem in my code that it didn't work? I compared your last code with the last one you gave and i tried and didn't work, but didn't find the difference that caused th problem.
DO you know what was that problem?

Niloofar24 15 Posting Whiz

Thank you @cereal, were good and very clear explanations.

I changed the code. There is still a problem. Now the only page i can see is page one with posts 1-2-3. When i click on the Next button, nothing happens and it won't go to the second page.
Does it back to these 2 parts?
$get_page = isset($_GET['page']) ? $_GET['page'] : 0;
and
'default' => 0,

When i changed them from 0 to 1 to test, then the only page i could access was the second page with posts 4-5-6 in it.

Niloofar24 15 Posting Whiz

@Eagle.Avik, so you means i should use this? The way i used was incorrect?

    $conn = new PDO("mysql:host=".$servername.";dbname=".$dbname, $dbusername, $dbpassword

as +lps mentioned, you should use ajax to call the script or use form.

You said i shouldn't use that submit button whi Delete value, and you gave a form part instead. But where i should use that? In that echo part that i put Delete button before?

Niloofar24 15 Posting Whiz

Would you please explain this line for me?

//What does floor do here?
$pages = floor($posts / $perpage);


# get current page, define 1 as default value
// What does this line say?
$get_page = isset($_GET['page']) ? $_GET['page'] : 1;

// I didn't understand this part well.
$data = array(
    'options' => array(
        'default' => 1,
        'min_range' => 1,
        'max_range' => $pages
    )
);

Thank you.

Niloofar24 15 Posting Whiz

I think the problem must be in these lines:
here:

$range = $perpage * $number;

here:

$stmt = $conn->prepare("SELECT ID, Title, Author, Content FROM Posts LIMIT :limit, :perpage");

And here:

$stmt->bindParam(':limit', $range, PDO::PARAM_INT);

$perpage is 3 wich means how many posts should be echo on the page, correct?
$number is the number of the page we are in to read it's posts, correct?
And the limit value that it's duty is setting the start post, will be filled with $range value.

$range = $perpage * $number;

3 = 3 x 1

So here if we are in the first page, the $range value will be set to 3, and then with this part (':limit', $range, the limit value will be set to 3 wich means the script should starting to echo posts from post number 4, correct?!

So how can i solve it?

Niloofar24 15 Posting Whiz

Any more suggestion for how to edit this code to work? Every guidance is welcome.

Niloofar24 15 Posting Whiz

Thank you @cereal for your clear explanation and for the links.

Yes it works fine, thank you again. But there is a little problem i tried to fix but i couldn't find the exact part needs to be changed.
the first page contains posts 4-5-6, and the second page contains posts 7-8-9. So where is the page for posts 1-2-3?
Please help me first with just pointing to the part i should change because i want to do it myself to find if i have understood the code you gave me or not, thank you.

Niloofar24 15 Posting Whiz

I want each page to show only 3 posts to the user, so posts 1-2-3 go into page 1, and posts 4-5-6 go into page 2, and posts 7-8-9 go into page 3.
For now i can enter the x value that choose the first post that must be shown on the page, by typing the number on the input box and then with clicking on the submit button, the value will be sent to the script.

But i want to change it.
I want to set 2 buttons; the previous button and the next button.
I want when i click on the next button, the script add 3 numbers into x value, and the first post that must be shown on the page start from that post.... if i click one more time, add 3 numbers to the x value again and .....
And when click the previous button, reduce x value 3 numbers and move back to show the post....
I'm not sure if i have explained what i want clear or not, sorry!
Now need your guidance, thank you.

Niloofar24 15 Posting Whiz

Yes it works, thank you @cereal.
Would you please explain these lines for me?

    //What does trim exactly do?
    $number = trim($_POST['number']);

    //What does this line say?
    $number = filter_var($number, FILTER_VALIDATE_INT);

And i didn't understand this part exactly:

    $data = array(
    'options' => array(
    'default' => 1,
    'min_range' => 1,
    'max_range' => 100
    )
    );
    $number = filter_var($number, FILTER_VALIDATE_INT, $data);
Niloofar24 15 Posting Whiz

Yes, please look at this, maybe this is my mistake somewhere:

<?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);

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

        $stmt = $conn->prepare("SELECT ID, Title, Author, Content FROM Posts LIMIT :limit, 3");
        $stmt->bindParam(':limit', trim($_POST['number']), PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();

        foreach($result as $key => $row) {
            echo $row['ID'] . ") ";
            echo "<a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a>";
            echo "<br>" . " by: " . $row['Author'];
            echo "<br>";
            echo $row['Content'];
            echo "<br><br><br>";
        }
    }

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


<html>
<head>
<title>User View Page</title>
</head>
<body>
<hr>

<form method="post">
<input type="text" name="number">
<input type="submit" value="Submit">
</form>

</body>
</html>
Niloofar24 15 Posting Whiz

I tried them @cereal, but they didn't work.

Niloofar24 15 Posting Whiz

hello me again:)
There is a page in my cms that users can see my posts there and i want to limit the number of posts that must be shown to users in each page and paginate.
This is my 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);

    $number = "";

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

        $sql = "SELECT ID, Title, Author, Content FROM Posts LIMIT" . $_POST['number'] . ", 3";
        $result = $conn->query($sql);

        foreach($result as $row) {
            echo $row['ID'] . ") ";
            echo "<a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a>";
            echo "<br>" . " by: " . $row['Author'];
            echo "<br>";
            echo $row['Content'];
            echo "<br><br><br>";
        }
    }

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

<html>
<head>
<title>User View Page</title>
</head>
<body>
<hr>

<form method="post">
<input type="text" name="number">
<input type="submit" value="Submit">
</form>

</body>
</html>

How the script can take the value i send after clicking the submit button, to LIMIT x, y part?
My code above doesn't work, i face with a page with an input form and a submit button, and when i type a number and click the submit button, nothing happens.

Niloofar24 15 Posting Whiz

Hello.
i'm creating a simple CMS for myself. There is a register form, i can register as the admin of the cms and then with login form, i can enter into admin area to write a new post or edit or delete posts.
But there is a problem. I typed the link of the admin page into browser from my cellphone to see if i can enter into admin area without login, and i faced with the admin page then!
So how can i fix it? For example in wordpress if i type the link of my friend's admin page in the browser, i can't access there and will face with login form, right?
Now how should i solve this security problem?

Niloofar24 15 Posting Whiz

Well i have added this part below, after line 24:

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);

        $id = $_GET['POST'];

        $sql = "DELETE FROM Posts WHERE ID = '$id'";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':title', $title, PDO::PARAM_STR);
    $stmt->bindParam(':author', $author, PDO::PARAM_STR);
    $stmt->bindParam(':content', $content, PDO::PARAM_STR);
    $stmt->execute();
    } catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
    }            
    $conn = null;
}
?>

And have changed this part:

 foreach($result as $row) {
echo "<tr><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a href='#'>Delete</a> <br> <a href='#'>Edit</a> </td></tr>";
}

into this:

    foreach($result as $row) {
        echo "<tr><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <input type='submit' name='submit' value='Delete'" .$row['ID']. "> <br> <a href='edit-page.php?post=" . $row['ID'] . "'>Edit</a> </td></tr>";
    }

But it doesn't work. When i click on Delete button, it doesn't clear anything from db table. It seems the Delete button can't send the post's id to the script to find the exact row of the table that should be delete.

Niloofar24 15 Posting Whiz

@lps would you please explain your code with comments?

Niloofar24 15 Posting Whiz

This is my 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 = "SELECT ID, Title, Author, Content FROM Posts";
    $result = $conn->query($sql);

    echo "<table cellspacing='0' cellpadding='20' border='1px solid black' border-collapse='collapse'>";
    echo "<tr><th> POST ID </th><th> POST TITLE </th><th> POST AUTHOR </th><th> ACTION </th></tr>";

    foreach($result as $row) {
        echo "<tr><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php?post=" . $row['ID'] . "'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a href='#'>Delete</a> <br> <a href='#'>Edit</a> </td></tr>";
    }
        echo "</table>";

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

<html>
<head>
<title>admin</title>
</head>
<body>
<hr>
<?php echo "<a href='writing-post.php'>writing new post</a>"; ?>
</body>
</html>
Niloofar24 15 Posting Whiz

Yes it works :)

Thank you @JorgeM.

Thank you @lps. I tried WHERE ID = '".$_GET['post']."'"; first but it didn't work but the second way worked.

Niloofar24 15 Posting Whiz

Yes it works well with no error now.

Sory for the errors in my posts. Speed does not always help.

No need to be sorry, it's ok. Errors are a part of coding. Thank you again for your help.

Niloofar24 15 Posting Whiz

Niloofar: you should start by the links James provided you, since they are the official tutorials about how to use sockets, and the official tutorial on learning the language.

I tried those linkes again, but this is what i faced with:

"In compliance with U.S. and applicable Export laws we are unable to process your request."

So i can't access. Well, for now i think it's better to start with learnging java basic syntax.
Thank you friends:)

Niloofar24 15 Posting Whiz

@JorgeM, i edited all the title's links with this:
echo "<td><a href='single-post-page.php?post=" . $row['ID'] . "'>";

But what about $_GET['post'];? Where i should put it in the single-post-page.php script?

How i should edit this line?
$sql = "SELECT ID, Title, Author, Content FROM Posts WHERE ID = '2'";

Niloofar24 15 Posting Whiz

The complete code of the page with titles:

<?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);

    echo "<table cellspacing='0' cellpadding='20' border='1px solid black' border-collapse='collapse'>";
    echo "<tr><th> POST ID </th><th> POST TITLE </th><th> POST AUTHOR </th><th> ACTION </th></tr>";

    foreach($result as $row) {
        echo "<tr><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a href='#'>Delete</a> <br> <a href='#'>Edit</a> </td></tr>";
    }
        echo "</table>";

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

<html>
<head>
<title>admin</title>
</head>
<body>
<hr>
<?php echo "<a href='writing-post.php'>writing new post</a>"; ?>
</body>
</html>

Here <a href='single-post-page.php'> is the part that should send it's id into single-post-page.php script.

And the code of single-post-page.php is:

<?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 WHERE ID = '2'";
    $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;
?>

<html>
<head>
<title>Single-post-page</title>
</head>
</html>

Of course here i have typed an id (number 2) myself just for example WHERE ID = '2'";, here …

Niloofar24 15 Posting Whiz

Hi everybody.
I have a page with some post's title in it. I have put every title in an <a> tag. I want to click on each title and enter into an other page (single-post-page.php) to see the content of that post's title.
The script of that page (single-post-page.php) should get that post's id when i click on the title's link to send it to the db and get that post's content to echo on the page.

But my problem is that how i can send post's id to that (single-post-page.php) page by clicking on each title's link?

Niloofar24 15 Posting Whiz

I understood.
But there is one more problem.
There are 3 fields in the form to be filled: title and author and content.
If i leave all 3 fields empty, or just fill one or two of them and click the submit button, the message "You forgot to enter some required data" appears and no blank row will be added into db.
But if i fill all 3 fields and click the submit button, nothing will be sent to db and this error will be appeared on the top of the page:

INSERT INTO Posts (Title, Author, Content) VALUES (:title, :author, :content)
 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 ':title, :author, :content)' at line 2 
Niloofar24 15 Posting Whiz

I checked the link many times but unfortunatelly i faced with a blank page, i don't know why! Is there any other good and clear tutorial? Or at least can you help me with telling me where i should start from to create a chat messenger? What is better for now for the first step to start from? Can you suggest me where i can start from and what is better for now to learn? Well i know it's not easy to answer those questions and you all are not here to answer these kinds of questions but just want to know the primary form, model and shape of the way i should go through. Just need to find the first step:)
Thank you for your help and answers.

Niloofar24 15 Posting Whiz

specially i didn't understand this part

find the parent of the link again using the same id attribute and remove the row.

Niloofar24 15 Posting Whiz

This is that error:

PHP Fatal error:  Can't use function return value in write context in Line 8

And this is line 8:

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

Thank you @broj1 for clear explanation and for the link.

So now i just should add this line $stmt = $conn->prepare($sql); before line 24 $stmt->bindParam(':Title', $title); correct? i did.