Reputation: 938
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
Upvotes: 0
Views: 96
Reputation: 309
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']."'");
Upvotes: 0
Reputation: 16772
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>
Upvotes: 0
Reputation: 695
your QUERY is wrong.
USE
"UPDATE ''user SET firstname_parent='".$update_firstname_parent."' WHERE email = '".$_SESSION['email']."'";
Upvotes: 0
Reputation: 5157
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']."'");
Upvotes: 1
Reputation: 333
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";
}
Upvotes: 0