3

I am new to PHP and want to create a script that will update the users first and last name if they need to do so. I have the code below and it ends up echoing out UPDATE fixableusers SET first='trenton' WHERE id='6' on the settings.inc.php page but it is not updating the table. Is there a reason it is not updating the table?

<?php
include_once('dbh.php');

session_start();
$userSession = $_SESSION['id'];

if(isset($_SESSION['id'])) {
    $postTest = $_POST['first'];
    $sql = "UPDATE fixableusers SET first='$postTest' WHERE id='$userSession'";
} else {
    echo 'Do something else';
}

My dbh.php file includes the following code:

<?php

$conn = mysqli_connect('localhost', 'root', 'root', 'users');
Prakash Saini
  • 481
  • 3
  • 11
Trenton Tyler
  • 1,692
  • 3
  • 24
  • 53

3 Answers3

5

You are excluding the mysqli_query function,

Add this mysqli_query($conn, $sql); after build the $sql.

Naga
  • 2,190
  • 3
  • 16
  • 21
1

Inside your if statement you're just declaring variables, in order to actually execute a query you need to :
if(isset($_SESSION['id'])) { $postTest = $_POST['first']; $sql = "UPDATE fixableusers SET first='$postTest' WHERE id='$userSession'"; mysqli_query($conn, $sql); }

And you need to improve this ( handle errors ..etc)

Taki
  • 17,320
  • 4
  • 26
  • 47
1
<?php
// Connect your DB
include_once('dbh.php');

//Start Session    
session_start();

if(isset($_SESSION['id'])) {

// Update Query
$updateqry = "UPDATE fixableusers SET first='".$_POST['first']."' WHERE id='".$_SESSION['id']."'";
mysqli_query($conn, $updateqry);

} else {

        echo 'Do something else';
}

?>
Karthik
  • 5,589
  • 18
  • 46
  • 78
  • 1
    Please add some explanation to the code so the OP and further readers can understand your code. The OP is a beginner and so are many others on this site. – beerwin Dec 23 '16 at 08:35