-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>
10
  • What errors are you getting? Commented Sep 26, 2016 at 8:58
  • chk your connection ist, and your code is open for SQL Injection. Commented Sep 26, 2016 at 9:00
  • 1
    It amazes me that people are still using mysql_ functions still to this day. Commented Sep 26, 2016 at 9:11
  • 1
    No wonder. 60% of stackoverflow shows it Commented Sep 26, 2016 at 9:12
  • 1
    @devpro sad thing is that they are probably commercial websites stackoverflow.com/questions/38297105/… Commented Sep 26, 2016 at 9:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

13 Comments

who knows, $location is string or integer.
sql injection here
$location is a string
@NiragireSam: did u checked the others answers??
yes i am checking it and i don't find any change
|
0

Change your SQL query as below

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

1 Comment

sql injection here
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.