How to delete a post in a page with multiple posts? [MySql] - php

I want to make a query that deletes a post(thread) from a page but in that page I have multiple posts, I have in my database a field called id_thread is the unique id of every post but I'm listing multiple posts of the same user in the page, this is the code of the page where the posts are listed:
<?
session_start();
if(isset($_SESSION['id']))
{
$servername = "(...)";
$username = "(...)";
$password = "(...)";
$dbname = "(...)";
$connect = mysqli_connect($servername, $username, $password, $dbname);
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
}
else
{
header(" url=index.php");
}
?>
(... hidded non relative html code ....)
<?
$id = (isset($_GET['id'])) ? $_GET['id'] : $_SESSION['id'];
$query = mysqli_query($connect,"SELECT * FROM thread inner join category on thread.id_type=category.id_type WHERE username = '".$id."'");
echo '<div class="container marg-t-100">'."All posts from User: ".$id.'</div>';
echo '<div class="container-fluid marg-t-25">';
while($row = mysqli_fetch_array($query))
{
echo '<div class="col-md-1"></div>';
echo '<div class="col-md-11">';
echo '<form role="form" action="delete.php" method="post"><a type="submit" class="btn btn-danger marg-t-10 pull-right">Delete Thread</a></form>';
echo $row['title'] ."<br><br>".$row['content']."<br><br>".'<div class="pull-right">'. "date: ".$row['data']."<br>"."author: ".$row['username']."<br>".$row['name'].'</div>' ."<br><br><br><hr><br>";
echo '</div>';
}
echo '</div>';
mysqli_close($connect);
?>
(...)
this is an image of the page im talking about:
I want that when I click "Delete Thread" the respective post(thread) gets deleted from database.
If you see in the page code above you can see that the delete button takes action at delete.php, this is my code on that file:
<?php
session_start();
if(isset($_SESSION['id']))
{
$servername = "(...)";
$username = "(...)";
$password = "(...)";
$dbname = "(...)";
$connect = mysqli_connect($servername, $username, $password, $dbname);
$id = (isset($_GET['id'])) ? $_GET['id'] : $_SESSION['id'];
$query = mysqli_query($connect,"Delete FROM thread WHERE username = '".$id."' and id_thread ='".(I WANT THE RELATIVE ID HERE)."'");
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
}
else
{
header(" url=index.php");
}
?>

In your page that lists user's posts, inside the form add a hidden field with a value of the post ID and a name attribute in which you will use later in your POST variables.
echo '<form role="form" action="delete.php" method="post"><button type="submit" class="btn btn-danger marg-t-10 pull-right" value="Delete Thread"><input type="hidden" name="thread_id" value="'.$row['id'].'"></form>';
And in delete.php query part:
$query = mysqli_query($connect,"Delete FROM thread WHERE username = '".$id."' and id_thread ='.$_POST['thread_id']);

<?
echo '<form role="form" action="delete.php?id='.$row['thread_id'].'" method="post"><input type="submit" class="btn btn-danger marg-t-10 pull-right" value="Delete Thread"></form>';
delete.php
<?php
session_start();
if(isset($_SESSION['id']))
{
$servername = "(...)";
$username = "(...)";
$password = "(...)";
$dbname = "(...)";
$connect = mysqli_connect($servername, $username, $password, $dbname);
$id=$_SESSION['id'];
$thread_id = $_GET['title'];
$query = mysqli_query($connect,"Delete FROM thread WHERE username = '".$id."' and thread_id ='".$thread_id."'");
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
}
else
{
header(" url=index.php");
}
?>
I hope this is what you asked for.

Related

Wordpress PHP FIle not running

I have a custom search form on my website with this code:
<center>
<form action="orderstatus.php" method="POST">
<input type="text" name="OrderLineNumber" value="">
<br>
<input type="submit" value="Submit">
</form>
</center>
And although I call orderstatus.php to action . When I press the submit button I get this Url : blablabla.de/action_page.php?OrderLineNumber=+23563623
This means instead of running orderstatus.php it's running action_page.php
My orderstatus.php is trying to print One Select Row from database like this :
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "*******";
$username = "*******";
$password = "*******";
$dbname = "********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM OrderStatus";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc() & fetch_assoc() = $_POST["name"] ) {
echo "<br> Order Line Number: ". $row["OrderLineNumber"]. " - Date Started: ". $row["Date Started"]. " " . $row["Status"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

Problems in updating from PHP

I have a form that I want to have record changes update my SQL database-table.
In my index.php-file I have f.ex this:
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";
$tbname = "myVis";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM " . $tbname . " WHERE id = '$_POST[id]'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$image = $row['image'];
$course = $row['course'];
$frdate = $row['frdate'];
$todate = $row['todate'];
$email = $row['email'];
$checkout = $row['checkout'];
}
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
...
</head>
<body>
<form action='' method='post'>
<table>
...
<tr>
<td>Efternamn:</td>
<td>
<input id="lastname2" type="text" value='<?php echo $lastname; ?>' /></td>
</tr>
...
</table>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['submit2'])) {
...
if (isset($_POST['lastname2'])) {
$lastname = $_POST['lastname2'];
}
...
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";
$tbname = "myVis";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql2 = "UPDATE " . $tbname . " SET firstname='$firstname', lastname='$lastname', image='$image', course='$course', frdate='$frdate', todate='$todate', email='$email', checkout='$checkout' WHERE id ='".$_POST['id']."'";
$result = mysqli_query($conn, $sql2);
if ($result) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
}
}
?>
<input type="submit" name="submit2" id="submit2" value="Spara" />
<input type="button" name="back" id="back" value="Tillbaka" onclick="history.back()" />
</form>
</body>
</html>
When I try to change a value (say lastname) nothing changes and all values are back to the selected record from the db-table.
How can I get PHP to understand and have a changed value update the table?
Don't quite understand the sequence in my index-file.
Please help.
Regards,
/Fredrik.
Put your submit code at the top of the page after your database connection. Then when you submit the form first match the table column with submitted post values. If any column has different post value then update that column.

How to check the status of a checkbox (switch) after the submit

in a form i put in a checkbox (switch), how do i post after submitting to checking the status of the switch for MySQL update? thank you
Consequently the MySQL table does not change.
From checkbox and submit
<form method="POST" action="process.php">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="switch-1">
<input type="checkbox" id="switch-1" class="mdl-switch__input" checked>
<span class="mdl-switch__label">Autenticazione a due fattori</span>
</label>
....
<input onclick="conferma();" class="mdl-button mdl-js-button
mdl-button--raised mdl-js-ripple-effect mdl-button--accent" type="submit"
value="Salva" name="submitBtn">
</form>
Switch control and MySQL update (process.php)
<?php
session_start();
mysql_connect(localhost) or die(mysql_error());
mysql_select_db("*******") or die(mysql_error());
$user = $_SESSION['users'];
if(isset($_POST['submitBtn'])) { //form submission occured
if(!isset($_POST['switch-1'])){
$sql = "UPDATE `*******`.`login_users` SET `auth` = \'checked\' WHERE username = '$user'";
header("location: https://*******.php");
} else {
$sql = "UPDATE `*******`.`login_users` SET `auth` = \'unchecked\' WHERE username = '$user'";
header("location: https://*******.php");
}
}
?>
1) Modify your form
<form method="POST" action="process.php">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="switch-1">
<input type="checkbox" name="switch-1" id="switch-1" class="mdl-switch__input" checked>
<span class="mdl-switch__label">Autenticazione a due fattori</span>
</label>
....
<input type="submit" value="Salva" name="submitBtn" class="mdl-button mdl-js-button
mdl-button--raised mdl-js-ripple-effect mdl-button--accent" >
</form>
2) Create a new process.php page in the same working directory and add these.
EDIT:
<?php
session_start();
$user = $_SESSION['users'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "YourDBName";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submitBtn'])) { //form submission occured
if(isset($_POST['switch-1'])){
$sql = "UPDATE login_users SET auth = 'checked' WHERE username = '$user'";
} else {
$sql = "UPDATE login_users SET auth = 'no' WHERE username = '$user'";
}
if ($conn->query($sql)) {
echo "Updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
echo "Form Submission Error";
}
$conn->close();
?>
Hope it's helpful.

i cant input values into a form directly from my data in the database

how do i get the value in the form input to be equal to the data fetched from my database . when i tried to retrieve the value from the post super global i got Array ( [john_doe] => ) i.e an empty value
<?php print_r($_POST); ?>
<form action="" method="post">
<?php while($subs = mysqli_fetch_assoc($result)) { ?>
<input type="" name="john_doe" value='<?php echo $subs["menu_name"]; ?>'>
<input type="submit" value="sumbit">
<?php } ?>
</form>
There will be some wrong with your database fetching, you can try this following code for your purpose
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "**YOUR QUERY**";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
<input type="text" name="john_doe" value='<?php echo $row["menu_name"] ;?>'>
<input type="submit" value="sumbit">
$conn->close();
?>

Update MySQL entry through PHP form

I am writing a script right now to update a MySQL data entry through a PHP form. I know there are lots of tutorials and also a lot of answers here on Stackoverflow. My problem is that I get a "Updated data successfully" message but the data are not updated inside my database. Maybe someone find the error or can tell me what I did wrong and what I have to change. Here is the needed code:
Form:
<?php
session_start();
if(empty($_SESSION)) // if the session not yet started
session_start();
if(!isset($_SESSION['email'])) { //if not yet logged in
header("Location: login.php");// send to login page
exit;
}
include 'header.php';
$get = "SELECT * FROM user email WHERE email = '".$_SESSION['email']."'";
$result_get = mysqli_query($connect, $get);
$_SESSION['data'] = mysqli_fetch_assoc($result_get);
?>
<form method="POST" action="update_profile.php" class="form-horizontal form-label-left">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Vorname:</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" name="firstname_parent" class="form-control" value="<?php echo $_SESSION['data']['firstname_parent']; ?>">
</div>
</div>
<button type="submit" name="update" value="update" class="btn btn-warning btn-lg btn-block">PROFIL VERVOLLSTÄNDIGEN</button>
</form>
update_profile.php:
<?php
session_start();
// Create connection credentials
$db_host = 'localhost';
$db_name = 'DBNAME';
$db_user = 'DBUSER';
$db_pass = 'DBPASS';
// Create mysqli object
$connect = new mysqli ($db_host, $db_user, $db_pass, $db_name);
// Error Handler
if ($connect->connect_error) {
printf ("Connection failed: %s\n", $connect->connect_error);
exit();
}
// Check if form is submitted
if (isset ($_POST['update'])) {
$update_firstname_parent = mysqli_real_escape_string ($connect, $_POST['firstname_parent'] );
}
$sql = mysqli_query ($connect, "UPDATE `user` SET
firstname_parent='".$update_firstname_parent."' WHERE email = '".$_SESSION['email']."'";
if (mysqli_affected_rows($connect) == 0) //<--
{
die('Could not update data: ' . mysql_error());
} else {
echo "Updated data successfully\n";
}
mysql_close($connect);
?>
EDIT:
I changed the update_profile.php due to some comments here. Now I do not get a "Updated data successfully" message, now I only get a blank page and no data is updated inside the database.
Thanks,
Chris
Your update query is wrong. Change it
$sql = mysqli_query ($connect, "UPDATE `user` WHERE email = '".$_SESSION['email']."' (`firstname_parent`)
VALUES ('$update_firstname_parent')");
to
$sql = mysqli_query ($connect, "UPDATE `user` SET
firstname_parent='".$update_firstname_parent."' WHERE email = '".$_SESSION['email']."'");
Isn't your if statement incorrect? You are checking to see if something is changed and then outputting the error rather than the otherway around?
if (mysqli_affected_rows($connect) == 1)
{
die('Could not update data: ' . mysql_error());
} else {
echo "Updated data successfully\n";
}
to
if (mysqli_affected_rows($connect) == 0) //<--
{
die('Could not update data: ' . mysql_error());
} else {
echo "Updated data successfully\n";
}
your QUERY is wrong.
USE
"UPDATE ''user SET firstname_parent='".$update_firstname_parent."' WHERE email = '".$_SESSION['email']."'";
There was syntax error in your UPDATE and a missing session_start(); in the update.php file. This should work just fine.
update.php
<?php
session_start();
// Create connection credentials
$db_host = 'localhost';
$db_name = 'DBNAME';
$db_user = 'DBUSER';
$db_pass = 'DBPASS';
// Create mysqli object
$connect = new mysqli ($db_host, $db_user, $db_pass, $db_name);
// Error Handler
if ($connect->connect_error) {
printf ("Connection failed: %s\n", $connect->connect_error);
exit();
}
// Check if form is submitted
if (isset ($_POST['update'])) {
$update_firstname_parent = mysqli_real_escape_string ($connect, $_POST['firstname_parent'] );
}
$sql = mysqli_query ($connect, "UPDATE `user` SET
firstname_parent='".$update_firstname_parent."' WHERE email = '".$_SESSION['email']."'");
if (mysqli_affected_rows($connect) == 1)
{
die('Could not update data: ' . mysql_error());
} else {
echo "Updated data successfully\n";
}
mysql_close($connect);
?>
<?php
session_start();
if(empty($_SESSION)) // if the session not yet started
session_start();
if(!isset($_SESSION['email'])) { //if not yet logged in
header("Location: login.php");// send to login page
exit;
}
include ('header.php');
$get = "SELECT * FROM user email WHERE email='".$_SESSION['email']."'";
$result_get = mysqli_query($connect, $get);
$_SESSION['data'] = mysqli_fetch_assoc($result_get);
?>
<form method="POST" action="update_profile.php" class="form-horizontal form-label-left">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Vorname:</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" name="firstname_parent" class="form-control" value="<?php echo $_SESSION['data']['firstname_parent']; ?>">
</div>
</div>
<button type="submit" name="update" value="update" class="btn btn-warning btn-lg btn-block">PROFIL VERVOLLSTÄNDIGEN</button>
</form>
First thing to know if the data is correct try to echo
echo($connect, "UPDATE `user` SET
firstname_parent='".$update_firstname_parent."' WHERE email = '".$_SESSION['email']."'";
if the data shows then your code is correct but here's mine:
$sql = mysqli_query("UPDATE user SET firstname_parent='".$update_firstname_parent."' WHERE email = '".$_SESSION['email']."'");

Categories