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

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.

Related

unable to insert url in mysql database using php

Html code:
<input type="file" class="form-control" name="video[]" placeholder=" Enter Image file">
<div><h2>OR</h2></div>
<input type="text" class="form-control" name="yurl" placeholder=" Enter youtube embedded code">
<button type='submit' name="save" class='btn btn-success waves-effect waves-light'><i class='fa fa-upload'></i> Save</button>
php code:
<?php
if(isset($_POST['save'])){
$yurl = ($_POST['yurl']);
$new_data=mysqli_real_escape_string($connection, stripslashes($yurl));
echo $yurl;
foreach($_FILES["video"]["tmp_name"] as $key=>$tmp_name){
$temp = $_FILES["video"]["tmp_name"][$key];
$name = $_FILES["video"]["name"][$key];
if(empty($temp))
{
break;
}
move_uploaded_file($temp,"../uploads/galleryvideos/".$name);
$sql = "INSERT INTO gallaryvids (video ,youtube)
VALUES ('$name', '$yurl')";
if (mysqli_query($connection,$sql)) {
?>
<script>
window.location.href = "view_gal_video.php";
</script>
<?php
}
}
}
?>
Url which I want to insert: https://youtu.be/sA0-QXbLnaE
both video and link get inserted
when i only want to insert link not a video it fails but don't gives any error
Updated code with database connection.
if(isset($_POST['save'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$yurl = $_POST['yurl'];
//$video='test';
foreach($_FILES["video"]["tmp_name"] as $key=>$tmp_name){
$temp = $_FILES["video"]["tmp_name"][$key];
$name = $_FILES["video"]["name"][$key];
if(empty($temp))
{
break;
}
move_uploaded_file($temp,"../uploads/galleryvideos/".$name);
$sql = "INSERT INTO gallaryvids (video, youtube)VALUES ('$name', '$yurl')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}

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.

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

Adding user to MySQL database in php using phpMyAdmin

I think I am successfully connecting to my database by:
<?php
$user = 'root';
$pass = '9KSroMDjEqNmEYY4';
$db = 'chatservice';
$host = '127.0.0.1';
$conn = new mysqli($host, $user, $pass, $db, 3306) or die("Unable to connect");
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
?>
My question is how I would use the registration code to successfully add a user to the database. When entering the form I press register I do not get any error messages stating that the registration didn't succeed. It seems that the php code is not being reached after the initial connection. I am new to php and mySQL so any tips on formatting would be nice too!
<?php
require('connect.php');
if(isset($_POST['user']) && isset($_POST['password'])){
$user = $_POST['user'];
$id = $_POST['IDNUM'];
$password = $_POST['password'];
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$id', '$password')";
$result = mysqli_query($query);
if($result){
$msg = "Registered Sussecfully";
echo $msg;
}
else
$msg = "Error Registering";
echo $msg;
}
?>
<div class="register-form">
<title>Chat Page Start</title>
<form action="" methods="POST">
<p>
<label>Username: </label>
<input id="user" type="text" name="user" placeholder="user" />
</p>
<p>
<label>ID: </label>
<input id="IDNUM" type="text" name="IDNUM" placeholder="ID number" />
</p>
<p>
<label>Password: </label>
<input id="password" type="password" name="password" placeholder="password" />
</p>
<a class="btn" href="login.php">Login</a>
<input class="btn register" type="submit" value="Register" />
</form>
</div>
Another thing is how would I check the status of my database connection and where I should be checking this status?
your database connection is mysqli_connect and you execute the query in mysql_query is not proper.
<?php
require('connect.php');
if(isset($_POST['user']) && isset($_POST['password'])){
$user = $_POST['user'];
$id = $_POST['IDNUM'];
$password = $_POST['password'];
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', ' $id ', '$password')";
$result = mysqli_query($query,$conn);
if($result){
$msg = "Registered Sussecfully";
}
else
$msg = "Error Registering";
}
?>
You are connecting database using mysqli:
$conn = new mysqli('localhost', $user, $pass, $db, 3306) or die("Unable to connect");
And executing query using mysql:
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$IDNUM', '$password')";
$result = mysql_query($query);

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