Problems in updating from PHP - 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.

Related

Error while trying to save User Input to MySQL with PHP

Whenever I try to save user input to a MySQL database, it saves empty rows as soon as I load the page. Here is my index.php file:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$uName = mysqli_real_escape_string(mysqli_connect($servername, $username,
$password, $dbname), $_POST['username']);
$pass = mysqli_real_escape_string(mysqli_connect($servername, $username,
$password, $dbname), $_POST['password']);
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO users (username, password)
VALUES ('$uName', '$pass')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="post">
<input type="text" name="username"> <br> <br>
<input type="password" name="password"> <br> <br>
<input type="submit" name="submit">
</form>
</body>
</html>
Please tell me if there is anything I am doing wrong.
Thanks
Please see modified code below:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit'])){
$uName = mysqli_real_escape_string(mysqli_connect($servername, $username,
$password, $dbname), $_POST['username']);
$pass = mysqli_real_escape_string(mysqli_connect($servername, $username,
$password, $dbname), $_POST['password']);
$sql = "INSERT INTO users (username, password)
VALUES ('$uName', '$pass')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post">
<input type="text" name="username"> <br> <br>
<input type="password" name="password"> <br> <br>
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(isset($_POST['submit']))
{
$uName=$_POST['username'];
$password=$_POST['password'];
$sql = "INSERT INTO users (username, password) VALUES ('$uName', '$pass')";
if(mysqli_query($conn, $sql)){
{
echo "New record inserted successfully";
}
else
{
echo "There have some problem";
}
}
?>
Please Try this one

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>

Why this code inserts blank?

I´m trying to create a form connected to a database but when I fill out the form and I refer to the table in phpMyAdmin I see that it have entered a blank record instead of form data. I´m using PhpStorm.
I think all this code is correct...
That is the form of the .html:
<form id="form1" name="form1" method="post" action="index.php">
<label for="userSignUp">Email</label>
<input type="text" name="userSign" id="userSignUp" />
<label for="passwordSignUp">Password</label>
<input type="password" name="passwordSign" id="passwordSignUp" />
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form>
I have the following .php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$db_selected = mysqli_select_db($conn, $dbname);
$userSignUp = ""; // If I substitute "" with characters at this time the table is well updated
$passwordSignUp = ""; // Same as before
if(isset($_POST['userSign'])){
$userSignUp = $_POST['userSign'];
}
if (isset($_POST['passwordSign'])) {
$passwordSignUp = $_POST['passwordSign'];
}
$sql = "INSERT INTO test.person (FirstName, Password) VALUES ('$userSignUp', '$passwordSignUp')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();

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

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.

PHP login validation when user is not active

I developed a PHP login system that when the user logs in and the account is still not activated by the admin it will display message2 and if the user inputs wrong credentials it will display mesage1
I am almost done with the work. but i am confused on why the condition always falls under message1.
here is my code.
<?php
session_start();
if(isset($_POST["submit"])){
// windows
// $servername = "localhost";
// $username = "root";
// $password = "";
// $dbname = "loginDB";
// linux
$servername = "localhost";
$username = "root";
$password = "gmg0ddepfrxs";
$dbname = "loginDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn){
die("Connection failed: " . mysqli_connect_error());
}
$uname = $_POST["name"];
$password = $_POST["pwd"];
$sql = "SELECT * FROM user WHERE user_name='$uname' AND password='$password'";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){
$utype = $row["user_type"]; //1
$status = $row["user_status"]; //0
$username = $row["user_name"]; //USERNAME
$password = $row["password"]; //PASSWORD NG USER
}
if (mysqli_num_rows($result) > 0 AND $utype == '1'){
// session_start();
$_SESSION["login"] = "access";
setcookie("name",$uname,false);
echo "<script>window.location.href=\"member.php\"</script>";
}elseif (mysqli_num_rows($result) <= 0) {
echo "<script>window.location.href=\"index.php?msg=1\"</script>";
}elseif ($utype == '0') {
echo "<script>window.location.href=\"index.php?msg=2\"</script>";
}
}
?>
here is my HTML code.
<html>
<form action='login.php' method="post">
<table cellspacing='5' align='center'>
<tr>
<td>Username:</td>
<td>
<input required type='text' name='name' />
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input required type='password' name='pwd' />
</td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' name='submit' value='Submit' />
</td>
</tr>
</table>
<?php if(isset($_GET[ "msg"])){
$errmsg=$ _GET[ "msg"];
if($errmsg==1 ){ echo "<div style='text-align: center;'><h5> <font color = \"red\ "> Incorrect username and/or password! have you registered? </font></div>"; }
elseif ($errmsg==2 ) { echo
"<div style='text-align: center;'><h5> <font color = \"red\ "> Contact admin to activate your account. </font></div>"; } } ?>
</form>
<div style="text-align: center;">
<a href="register.php">
<button>Register here</button>
</a>
</div>
</html>
use a simple header location to redirect your users:
<?php
session_start();
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "gmg0ddepfrxs";
$dbname = "loginDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn){
die("Connection failed: " . mysqli_connect_error());
}
$uname = $_POST["name"];
$password = $_POST["pwd"];
$sql = "SELECT * FROM user WHERE user_name='$uname' AND password='$password'";
$result = mysqli_query($conn, $sql);
$row = mysql_fetch_assoc($result);
$totalRows_result = mysql_num_rows($result);
if ($totalRows_result == 0) {
header("location: index.php?msg=1");}
$utype = $row["user_type"]; //1
$status = $row["user_status"]; //0
if ($utype == '1'){
$_SESSION["login"] = "access";
setcookie("name",$uname,false);
header("location: member.php");
} else {
header("location: index.php?msg=2");
}
}
?>

Categories