0

Why does it not update the database??

<?php
ob_start();
session_start();
require_once 'dbconnect.php';

if( !isset($_SESSION['user']) ) {
header("Location: index.php");
exit;
}
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

$Nname = $_POST['name'];
$Nemail = $_POST['email'];
$Nlocation = $_POST['location'];

$sql = "UPDATE users SET userName = '$Nname', userEmail = '$Nemail',      userLocation = '$Nlocation' WHERE userId=".$_SESSION['user'];
/*if(! $sql ) {
           die('Could not update data: ' . mysql_error());
        }
        echo "Updated data successfully\n";
 */
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Profiel <?php echo $userRow['userName'] ?></title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Raleway:400, 600"    rel="stylesheet">
</head>
<body>
<header>
<h1>Profiel</h1>
</header>
<nav>
  <a href="/home.php">Home</a>
  <a href="/profile.php">Profiel</a>
  <a href="/logout.php?logout">Log uit</a>
</nav>
<div class="content">
  <h1>Welkom <?php echo $userRow['userName'] ?></h1>
  <?php/* switch (true) {
    case isset($_GET['change']):*/?>
    <form class="update" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
    <table>
      <p><tr><td></td><td>Oud</td><td>Nieuw</td></tr></p>
      <p><tr><td>Naam: </td><td><?php echo $userRow['userName'] ?></td><td>    <input type="text" name="name" value="<?php echo $userRow['userName'] ?>"></td></tr></p>
      <p><tr><td>Email: </td><td><?php echo $userRow['userEmail'] ?></td><td><input type="text" name="email" value="<?php echo $userRow['userEmail'] ?>"></td></tr></p>
      <p><tr><td>Werk Locatie: </td><td><?php echo $userRow['userLocation'] ?></td><td><input type="text" name="location" value="<?php echo $userRow['userLocation'] ?>"></td></tr></p>
    </table>
    <button type="submit" name="button">Opslaan</button>
  </form>
    <?php /*break;
    default:?>
    <table>
      <p><tr><td>Naam: </td><td><?php echo $userRow['userName'] ?></td></tr></p>
      <p><tr><td>Email: </td><td><?php echo $userRow['userEmail'] ?></td></tr></p>
      <p><tr><td>Werk Locatie: </td><td><?php echo $userRow['userLocation'] ?></td></tr></p>
    </table>
    <a href="/profile.php?change">veranderen</a>
    <?php echo $Nname; ?>
    <?php break;
  }*/?>
</div>

I dont understand why it is not updating the database, i tried everyting. Thanks for helping. The database connection is good. But i dont know how i send that UPDATE to my database i dont understand that. Could someone say how i need to do it??

  • 3
    You never connect to the database and execute the query. – Jay Blanchard Feb 22 '17 at 21:40
  • 3
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Feb 22 '17 at 21:40
  • Step 1, Connect to a database. 2, SHOW FORM. 3 CHECK IF FORM WAS SUBMITTED, 4 Define SQL query, 5 EXECUTE QUERY – Duane Lortie Feb 22 '17 at 21:43

0 Answers0