0

I'm having troubles getting my code to work properly. If I type it into phpMyAdmin it works, but when I try it in the code, it doesn't update the database.

<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DATABASE", $con);

$sp=mysql_real_escape_string($_GET['file']);

$query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'"; 

mysql_close($con);

?>
4

3 Answers 3

1

Try out this code snippet and see how you get on.

<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con) {
  die('Could not connect: ' . mysql_error());
} else {
    mysql_select_db("DATABASE", $con);
    $sp=mysql_real_escape_string($_GET['file']);
    $query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'"; 
    $result = mysql_query($query);
    mysql_close($con);
}
?>

I would recommend doing it this way as mysql is no longer supported by PHP.

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");

if (!$mysqli) {
  die('Could not connect: ' . $mysqli->connect_error);
} else {
    $sp = $mysqli->real_escape_string($_GET['file']);
    $query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'"; 
    $mysqli->query(query);
    $mysqli->close();
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

You're not EXECUTING your query. You're just defining a string that happens to contain some SQL, e.g.

$sql = "blah blah blah";
$result = mysql_query($sql) or die(mysql_error()); <--forgot this

Comments

0
<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DATABASE", $con);

$sp=mysql_real_escape_string($_GET['file']);

$sql = "UPDATE TRACKDB SET WEIGHT=100000 WHERE PATH='$sp'"; 

$result = mysql_query($sql) or die(mysql_error());

mysql_close($con);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.