2016-09-26 2 views
-2

aktualisiere Ich bin Anfänger in PHP, ich versuche, bestimmte Reihe zu aktualisieren, aber funktioniert nicht. Ich brauche Ihre Hilfe: update.php DIR. '/db_connect.php'; // Verbinden mit db $ db = new DB_CONNECT();Wie man spezifische Tabellenreihe in mysql Tabelle mit PHP

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> 
+0

Welche Fehler werden Sie bekommen? – Epodax

+0

chk Ihre Verbindung ist, und Ihr Code ist offen für SQL Injection. – devpro

+0

Verbindung ist in Ordnung. kein Fehler ich bekomme –

Antwort

0

Bitte unten 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 . "); 
    } 

Dank verwenden.

+1

Wer weiß, '$ location' ist String oder Integer. – devpro

+0

sql Injektion hier – Drew

+0

$ Speicherort ist eine Zeichenfolge –

0

Ihre SQL-Abfrage ändern, wie unten

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

sql Injektion hier – Drew

0

Wenn ich Ihren Code so etwas testen:

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

Es gibt diese Anfrage:

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

Bitte prüfen hier haben beide Werte ein Problem.

Problem:

Sie verwenden {} außerhalb der Anführungszeichen. Sie müssen es innerhalb des Anführungsstrichs verwenden.

Modified Abfrage mit demselben Beispiel:

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

Ergebnis:

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

Side Hinweise:

  • Verwenden mysqli_* oder PDO, weil mysql_* veraltet und in PHP 7 gesperrt ist.
  • Ihr Code ist offen für SQL Injection, Sie müssen mit SQL Injection schützen oder einfach Prepared Statement verwenden.

Einige andere Referenz für Prepared Statement: php mysql bind-param, how to prepare statement for update query