MySqli let user edit single record - php

learner here, going a little nuts..
i seem to be getting no errors, yet it is not updating.
connection is fine.
if(isset($_GET['edit_id'])){
$sql="SELECT * FROM info WHERE id=" .$_GET['edit_id'];
$result= mysqli_query($connect, $sql);
$row = mysqli_fetch_array($result);
}
//update
if(isset($_POST['btn-update'])){
$name=$_POST['name'];
$suname=$_POST['surname'];
$age=$_POST['age'];
$result=$_POST['result'];
$log=$_POST['log'];
$update="UPDATE employeeinfo SET name='$name', surname='$surname' WHERE id=". $_GET['edit_id'];
$up= mysqli_query($connect, $update);
if(!isset($sql)){
die("Error $sql" .mysqli_connect_error());
}
else
{
header("location: record.php");
}
}
and my calling
<html>
<body>
<h1>edit info<h1>
<form method="post">
name:<input type="text" class="form-control" name="name" value="<?php echo $row['name']; ?>"><br><br>
surname: <input type="text" name="surname" placeholder="Surname" value="<?php echo $row['surname']; ?>"><br><br>
log: <input type="text" name="log" placeholder="0" value="<?php echo $row['log']; ?>"><br><br>
<button type"submit" name"btn-update" id="btn-update" onclick="update()"><strong>Update</strong></button>
<button type="button" value="button">Cancel</button>
</form>
<script>
function update(){
var x;
if(confirm("Updated data Successfully") == true){
x = "update";
}
}
</script>
Not even sure if that script does anything, because my prompt said "update successful" yet clearly it wasnt. Thank you guys as always

Related

PHP - Update SQL Statement mysqli database+Variables

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_REQUEST['teamname'];
$email = $_REQUEST['email'];
$password = (md5($_REQUEST['password']));
$query = "UPDATE users SET email = ?,password = ? WHERE name = ?";
$statemnt = $conn->prepare($query);
$statemnt->bind_param('sss',$email,$password,$name);
$statemnt->execute(); echo $name,$email,$password; var_dump();
$statemnt->close(); $conn->close(); } ?>
managed to get the SELECT Statement figured out before this one and still having issues with the UPDATE - a form above this php snippet and is suppose to fill out $email $password and $name
<form method="post" action="">Team Name:<br>
<input type="text" name="teamname" value="<?php echo $name;?>">
<br>Email:<br><input type="text" name="email" value="<?php echo $email;?>">
<br>Password:<br><input type="text" name="password" value="">
<br><br><input type="Submit" value="Update the Record" name="Submit">
</form>
EDITED TO THE FOLLOWING (there is code above this part and below dont expect u want to see the rest of my html code - the bottom is what i am have trouble with):SELECT STATEMENT and var_dump is working but when i enter a password into the form it doesnt trigger the Submit and ultimately the UPDATE Statement - i have worked on it today again to no avail. pls any help would be appreciated not sure what im doing wrong - also var_dump at the bottom is outputing all of the values now
<?php
if (isset($_POST['submit'])) {
$sql = $conn->prepare("UPDATE users SET email=? , password=? WHERE team=?");
$postedemail=$_POST['teamemail'];
$postedpassword= $_POST['teampassword'];
$sql->bind_param("ssi",$postedemail,$postedpassword,$_POST["mySelect"]);
if($sql->execute()) {
$success_message = "Edited Successfully";
} else {
$error_message = "Problem in Editing Record";
}
var_dump($postedpassword);
var_dump($postedemail);
}
$stmt = $conn->prepare("SELECT team, name, email, password FROM users WHERE team = ?");
$stmt->bind_param("i", $_POST["mySelect"]);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No rows');
while($rows = $result->fetch_assoc()) {
$name = $rows['name'];
$email = $rows['email'];
$password = $rows['password'];
}
var_dump($password);
var_dump($name);
var_dump($email);
var_dump($_POST['mySelect']);
$stmt->close();
?>
<?php if(!empty($success_message)) { ?>
<div class="success message"><?php echo $success_message; ?></div>
<?php } if(!empty($error_message)) { ?>
<div class="error message"><?php echo $error_message; ?></div>
<?php } ?>
<form name="frmUser" method="post" action="">
<label>NAME:</label>
<input type="text" name="teamname" class="txtField" value="<?php echo $name?>">
<label>EMAIL:</label>
<input type="text" name="teamemail" class="txtField" value="<?php echo $email?>">
<label>PASSWORD</label>
<input type="text" name="teampassword" class="txtField" value="">
<input type="submit" name="submit" value="Submit" class="demo-form-submit">
</form>
thanks
You have this at the begining of your script : $selectedOption = $_POST["mySelect"];
Nowhere in your code (especially in your <form></form>) I see any input named "mySelect"
Add this field in your form and the problem should be solved.
var_dump(); helps a lot debugging.

Updating Userprofile

I have a session.php and a welcome.php page. There I have my userprofile.
I am trying to update the email field.
When I change my email address and press the update button I get the echo Update Successful, but it does not update. I think it still has the old value from the session and I need to clear it.
session.php:
<?php
include('connection.php');
session_start();
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($db,"select * from clients where email = '$user_check'");
$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$_SESSION['email']= $row['email'];
$_SESSION['username']= $row['username'];
$_SESSION['firstname']= $row['firstname'];
$_SESSION['lastname']= $row['lastname'];
$_SESSION['birthdate']= $row['birthdate'];
$_SESSION['street']= $row['street'];
$_SESSION['streetnr']= $row['streetnr'];
$_SESSION['city']= $row['city'];
$_SESSION['plzz']= $row['plzz'];
if(!isset($_SESSION['login_user'])){
header("
location:http://localhost:81/Left_over_youth_website/pages/login.php");
}
?>
welcome.php:
<?php
include("../php/session.php");
?>
<div>
<div class="col-md-9">
<div class="imagepageback">
<div class="card">
<div class="profilimage"></div>
<hr class="verticalline">
<?php
$sql = "UPDATE clients SET emaill = $_SESSION WHERE email = $user_check";
$update = mysqli_query($db, $sql);
if($update){
echo("update sucessful");
$_SESSION["emaill"] = $email_session;
}
?>
<form class="form" action="" method="post">
<input id="usernamee" class="usernamee" type="text" name="username" value="<?php echo $_SESSION['username']; ?>" readonly>
<input id="emaill" class="emaill" type="text" name="email" value="<?php echo $_SESSION['email']; ?>" readonly>
<input id="firstnamee" class="firstnamee" type="text" name="firstname" value="<?php echo $_SESSION['firstname']; ?>" readonly>
<input id="lastnamee" class="lastnamee" type="text" name="lastname" value="<?php echo $_SESSION['lastname']; ?>" readonly>
<input id="birthdatee" class="birthdatee" type="text" name="birthdate" value="<?php echo $SESSION['birthdate']; ?>" readonly>
<div>
<input id="streett" class="streett" type="text" name="street" value="<?php echo $_SESSION['street']; ?>" readonly>
<input id="nrr" class="nrr" type="text" name="streetnr" value="<?php echo $_SESSION['streetnr']; ?>" readonly>
</div>
<div>
<input id="cityy" class="cityy" type="text" name="city" value="<?php echo $_SESSION['city']; ?>" readonly>
<input id="plzz" class="plzz" type="text" name="plzz" value="<?php echo $_SESSION['plzz']; ?>" readonly>
</div>
<a href="#" class="editprofilelink" id="editprofilelink" onclick="editable(); showbt()"; >Edit Profile</a>
Change Password
<input hidden id="btupdate" type="submit" name="btupdate" value="Update">
</form>
</div>
</div>
</div>
</div>
</div>
echo 'update successful' on update query.
Try the following.
in welcome.php:
<?php
if(isset($_POST['btupdate'])){
$email = $_REQUEST['email'];
$sql = "UPDATE clients SET emaill = '$email' WHERE email = '$email_session'";
$update = mysqli_query($db, $sql);
if($update)
{
echo("update successful");
}
?>
You have a fatal error over this code:
<?php
$email = "SELECT * From Clients";
$sql = "UPDATE clients". "SET emaill = $email". "WHERE email = $email_session";
if(isset($_POST['btupdate'])){
$update = mysqli_query($db, $sql);
echo("update sucessful");
}
?>
this part ("UPDATE clients". "SET emaill = $email". "WHERE email = $email_session") returns this string "UPDATE clientsSET emaill = example#example.comWHERE email = example2#example.com
try this instead:
$sql="UPDATE clients SET emaill = $email WHERE email = $email_session"
be careful when using concatinate it won't add any space for you.
And you are not changing the $_SESSION["email"] after changing it. you must add $_SESSION["email"] = $email;
<?php
$email = "SELECT * From Clients";
$sql = "UPDATE clients SET emaill = $email WHERE email = $email_session";
$update = mysqli_query($db, $sql);
if($update){
echo("update sucessful");
}
?>

How to edit/update data in mysql using php

I've been researching everywhere regarding this matters and i still cant edit/update my entry. Noted, this is my first real program and my level is somewhat mediocre. So i would like to seek your advise/help regarding this matter.
CODE:
<?php
include('config.php');
if(isset($_POST['edit']))
{
$sql = "select * from hotels where BIL =" .$_GET['edit'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
}
if(isset($_POST['submit'])){
$hotel = $_POST['hotel'];
$address = $_POST['address'];
$update = "update hotels set $hotel = 'hotel', $address = 'address' where BIL = ".$_GET['edit'];
$up = mysqli_query($link, $update);
if (isset($sql)){
die ("Error $sql" .mysqli_connect_error());
}
else
{
header ("location: view.php");
}
}
?>
<html>
<body>
<form method="post" >
New Hotel: <br />
<textarea name="hotel" cols="50" rows="2" value="<?php echo $row['hotel']; ?>" /></textarea>
<br />
Address: <br / >
<textarea name="address" cols="50" rows="10" value="<?php echo $row['address']; ?>" /></textarea><br /><br />
<input type="submit" name="submit" value="update" />
</form>
<script>
function update(){
var x;
if(confirm("update Succesfully") == true){
x= "update";
}
}
</script>
</body>
</html>
Extra note: I copied this code from a youtube channel...
https://www.youtube.com/watch?v=jQzRDwY6zPc
Thanks in advance!

How to make login when user is signing up to a system

Presently in my login system a user has to do login after sign up.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Not a member yet?.Sign up here.By Sign up You are accepting our terms and conditions </p>
<fieldset>
<legend>Registration Info</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="<?php if (!empty($username)) echo $username; ?>" /><br />
<label for="email">Email id:</label>
<input type="email" id="email" name="email" /><br />
<label for="password1">Password:</label>
<input type="password" id="password1" name="password1" /><br />
<label for="password2">Password (retype):</label>
<input type="password" id="password2" name="password2" /><br />
</fieldset>
<input type="submit" value="Sign Up" name="create" />
</form>
If the form submitted correctly
<?php
if (!empty($user_username) && !empty($user_password)) {
// Make sure someone isn't already registered using this username
$query = "SELECT * FROM <database> WHERE username = '$username'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
// The username is unique, so insert the data into the database
$query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
$result=mysqli_query($dbc, $query) or die("cant create an account");
}else{
die("Somthing went wrong");
}
}
?>
Once it is done he has to go to login page and requerd to login using the following codes
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Log In</legend>
<label for="username">Username:</label>
<input type="text" name="username" value="<?php if (!empty($user_username)) echo $user_username; ?>" /><br />
<label for="password">Password:</label>
<input type="password" name="password" />
</fieldset>
<input type="submit" value="Log In" name="submit" />
</form>
I dont want this system happend like this when the user is signing up that time itself the user has to be taken to home page. And the session variables also has to be setted while signing up process.
Try to reuse the code, it will help to manage your code and application , Please create a function for login process, and use the same function for normal login and after signup then call the same function with the details used for signup.
function authenticate($user_id, $pwd)
{
// do the logic for login,
//if success then set value in session and redirect to the respective page
//if failed then error page
}
if (!empty($user_username) && !empty($user_password)) {
// Make sure someone isn't already registered using this username
$query = "SELECT * FROM <database> WHERE username = '$username'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
// The username is unique, so insert the data into the database
$query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
$result=mysqli_query($dbc, $query) or die("cant create an account");
// call the authenticate function for login
authenticate($username, SHA('$password1'));
}else{
die("Somthing went wrong");
}
}
Try something like this :-
<?php
if (!empty($user_username) && !empty($user_password)) {
// Make sure someone isn't already registered using this username
$query = "SELECT * FROM <database> WHERE username = '$username'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
// The username is unique, so insert the data into the database
$query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
$result=mysqli_query($dbc, $query) or die("cant create an account");
$_SESSION["username"] = $username; // assign session variable here.
header("Location: home.php"); // redirect to home page
}else{
die("Somthing went wrong");
}
}
?>

PHP not writing data in MYSQL

This is a school project and this particular page is to register a new user it does not display errors but it does not fill the MYSQL data base the connection for the database is in another page and I used the require function functions.php is where I am writing the connection function please help :(
<?php
include_once("menu.php");
?>
<form action="login.php" method="POST">
<?php
if ((isset($_POST['username']))&& (isset($_POST['password'])) && (isset($_POST['password2'])) && (isset($_POST['email'])))
{
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$email = $_POST['email'];
if ($password == $password2)
{
require_once("functions.php");
$connection = connectToMySQL();
$Query = "SELECT count(*) FROM tbl_users WHERE username='$username'";
$Result = mysqli_query($connection,$Query)
or die("Error in the query :". mysqli_error($connection));
$row = mysqli_fetch_row($Result);
$counter = $row[0];
if ($counter > 0)
{
echo "Username alredy exsist with the movie assosiation website<br/>";
echo "<input type=\"submit\" class=\"button\" value=\"Back\"/>";
}
else
{
$insertQuery = "INSERT INTO 'tbl_users'(username,password,email,role) VALUES ('$username',sha1('$password'),'$email','registered')";
$insertResult = mysqli_query($connection,$insertQuery)
or die("Error in the query :". mysqli_error($connection));
echo "account created !! <br />";
echo "<input type=\"button\" class=\"button\" value=\"Log In\" onclick=\"location.href='login.php'\"> ";
}
}
}
else
{
?>
<label>
<span>Username:</span>
<input id="username" type="text" name="username" placeholder="enter your Username" required />
</label></br>
<label>
<span>Password</span>
<input id="password" type="password" name="password" placeholder="enter your Password" required />
</label></br>
<label>
<span>Re-Enter Password</span>
<input id="password2" type="password" name="password2" placeholder="re-enter your Password" required />
</label></br>
<label>
<span>Email</span>
<input id="email" type="email" name="email" placeholder="enter email" required />
</label></br>
<label>
<span> </span>
<input id="submit" class="button" type="submit" name="submit" value="Submit"/>
</label>
</form>
<?php
}
?>
<?php
require_once("footer.php")
?>
remove single quote from your table name
try this
$insertQuery = "INSERT INTO `tbl_users`(username,password,email,role) VALUES ('$username',sha1('$password'),'$email','registered')";
instead of
$insertQuery = "INSERT INTO 'tbl_users'(username,password,email,role) VALUES ('$username',sha1('$password'),'$email','registered')";
Error in your sql statement.
Try this.
$insertQuery = "INSERT INTO tbl_users (username,password,email,role) VALUES ('{$username}',sha1('{$password}'),'{$email}','registered')";
or this
$insertQuery = "INSERT INTO tbl_users (username,password,email,role) VALUES ('".$username."',sha1('".$password."'),'".$email."','registered')";

Categories