-2

I am beginner in php , i am trying to update specific row but does not work . i need your help: update.php DIR . '/db_connect.php'; // connecting to db $db = new DB_CONNECT();

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

$id = $_POST["id"];
$location = $_POST["location"];
  $sql=mysql_query("UPDATE citizenalert set location={'$location'} WHERE id ={'$id'}");
}

  ?> 

db_config.php

  <?php

   define('DB_USER', "root"); // db user
   define('DB_PASSWORD', ""); // db password (mention your db password here)
     define('DB_DATABASE', "ikirenga"); // database name
      define('DB_SERVER', "localhost"); // db server
     ?>

db_connect.php

           <?php

  class DB_CONNECT {

// constructor
function __construct() {
    // connecting to database
    $this->connect();
}

// destructor
function __destruct() {
    // closing db connection
    $this->close();
}

/**
 * Function to connect with database
 */
function connect() {
    // import database connection variables
    require_once __DIR__ . '/db_config.php';

    // Connecting to mysql database
    $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());


    // Selecing database
    $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

    // returing connection cursor
    return $con;
}

/**
 * Function to close db connection
 */
function close() {
    // closing db connection
    mysql_close();
}
    }
   ?>

request_update.php

               <!doctype html>

                <html lang="en">

                   <body>
                  <form name="updates" method="post" action="update.php" > 
                   <label>User location</label> <input id="location"  type="text" name="location" > <br><br>
                     <input type="submit" name="submit" value="Submit" /> 
                     </form>  
                     </body>
                     </html>
Niragire Sam
  • 1
  • 1
  • 6

3 Answers3

0

Please use below Syntax

<?php
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();

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

$id = $_POST["id"];
$location = $_POST["location"];
$sql = mysql_query(" UPDATE citizenalert set location = " . $location . "   WHERE id = " . $id . ");
  }

Thanks.

devpro
  • 16,184
  • 3
  • 27
  • 38
0

Change your SQL query as below

$sql=mysql_query("UPDATE citizenalert set location='{$location}' WHERE id = '{$id}'");
Saurabh
  • 776
  • 1
  • 5
  • 15
0

If i test your code something like that:

<?php 
$id = 1;
$location = "test";
echo $sql="UPDATE citizenalert set location={'$location'} WHERE id ={'$id'}";
?>

It's giving this query:

UPDATE citizenalert set location={'test'} WHERE id ={'1'} 

Please check here, both of values having issue.

Problem:

You are using {} outside the quotes. you need to use it inside the single quote.

Modified Query with same example:

echo $sql="UPDATE citizenalert set location='{$location}' WHERE id ='{$id}'";

Result:

UPDATE citizenalert set location='test' WHERE id ='1'

Side Notes:

  • Use mysqli_* or PDO, because mysql_* is deprecated and closed in PHP 7.
  • Your code is open for SQL Injection, you must need to protect with SQL Injection or just simply use Prepared Statement.

Some other Reference for Prepared Statement: php mysql bind-param, how to prepare statement for update query

Community
  • 1
  • 1
devpro
  • 16,184
  • 3
  • 27
  • 38