pjmil
pjmil

Reputation: 2097

php retrieve filename from database

I'm trying to retrieve a filename from a database from table HORSE and column HORSE_IMAGE

$path = "horse_images/".$row["HORSE_IMAGE"];
$query = "DELETE FROM HORSE WHERE HORSE_ID=".$_GET["horseno"]." AND HORSE_IMAGE=".$row["HORSE_IMAGE"];

Currently HORSE_IMAGE returns nothing, but if I change it to HORSE_ID or HORSE_NAME, it works.

i.e. If i echo $row["HORSE_NAME"] it returns "You Beauty".

If I look up the row in the database I can see the filename shop-1.jpg is there.

Any ideas?

Upvotes: 0

Views: 270

Answers (2)

ToBe
ToBe

Reputation: 2681

If you want to change a single column of a row, use an UPDATE statement. DELETE is for removing complete rows only. And yes, escape anything before using it in your query.

"UPDATE HORSE SET HORSE_IMAGE = NULL WHERE HORSE_ID=".(int)$_GET["horseno"];

Upvotes: 1

Patrick G
Patrick G

Reputation: 486

As far as I understood the question, ToBe's answer might be correct, but you really should consider using PDO for MySql queries, in order to prevent Sql injection. Try:

<?php

  $query = "UPDATE HORSE SET HORSE_IMAGE = NULL WHERE HORSE_ID = ?";

  $db = new PDO(dsn, username, password);
  $prep = $db->prepare($query);

  $res = $prep->execute(array((int)$_GET["horseno"]));

?>

Take a look at the documentation: http://php.net/manual/de/book.pdo.php

Upvotes: 1

Related Questions