0

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

5
  • have you tried ...mysqli_affected_rows($sql) == 1... ? Commented Aug 25, 2015 at 8:06
  • Besides your update query being wrong, it seems that you inverted the success/failure messages. Commented Aug 25, 2015 at 8:11
  • @099 thanks for your tip, I tried it but same effect. I get the message "Updated data successfully" but no change inside my database Commented Aug 25, 2015 at 8:12
  • in update_profile.php you have this "UPDATE user WHERE email = '".$_SESSION['email']."... - I can't see session_start(); at the top of the page ! perhaps that's the problem ? Commented Aug 25, 2015 at 8:13
  • You need to learn UPDATE query!! Commented Aug 25, 2015 at 8:13

5 Answers 5

1

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']."'");
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your code. I tried this query. Now I only get a blank page. No "Updated data successfully" message or any error message. Also nothing has changed inside the database.
@ChristophC., Sorry, there was a syntax error. Try again
That´s it! Awesome!! Thank you very much! Just one thing I do not understand. Right now my form has just one input! If my form gets about 20 inputs I have to declare every like you did? So for example: "UPDATE user SET firstname_parent='".$update_firstname_parent."', lastname_parent = '".$update_lastname_parent."', street_parent = '".$update_street_parent."' and so on? Am I right?
w3schools.com/php/php_mysql_update.asp UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value
don't forget to escape strings to avoid injections and check for empty strings
|
0

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";
}

Comments

0

your QUERY is wrong.

USE "UPDATE ''user SET firstname_parent='".$update_firstname_parent."' WHERE email = '".$_SESSION['email']."'";

Comments

0

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>

Comments

0

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']."'");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.