0

I would like to send data from my mysql database to a PHP page.

The server were I have this script classify the file that has been uploaded just before this php comes into action, then the server enters the data into the database.

The PHP script should show that data (the column where the interesting data is it's called "cond"). My code is the following:

 $uploaddir = '/opt/lampp/htdocs/u';
 $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
 $link = mysql_connect("localhost", "pepito", "dominguez");
 $nombre=basename($_FILES['userfile']['name']);
 $tamany=basename($_FILES['userfile']['size']);
 $sql="INSERT INTO `wordpress`.`uploads` (`autoinc`, `nombre`, `tamany`,`fecha`) VALUES (NULL, '$nombre', '$tamany', CURRENT_TIMESTAMP)";
 $sqls="SELECT `cond` FROM `cm` WHERE `id`='$numero'";


 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   //  echo "File is valid and it's fully uploaded";
     mysql_select_db('wordpress');
     mysql_query($sql);
     $numero=mysql_insert_id();
     $condicion=0;
     sleep(3);
     $condicion= mysql_query($sqls);
     $a = $condicion + 1;
     echo "$a";
     echo "$numero";

What I should get in te echo "$a" is a number between 0 to 2, but what I get is a random number that changes everytime a file is uploaded.

I also have tried to get the 'id' column instead the 'cond' column and it does exactly the same.

Extra data: I'm using wordpress, and a plugin which is called "allow-php-in-posts-and-pages". The other echo in the code works properly.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • please stop using `mysql_` connections. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – nomistic Apr 29 '15 at 00:11

1 Answers1

1

You need to fetch the result after executing the query. You also should have use the $numero once it is set, so move the $sqls into the conditional after the insert.

http://php.net/manual/en/function.mysql-query.php

mysql_select_db('wordpress');
mysql_query($sql);
$numero=mysql_insert_id();
$sqls= "SELECT `cond` FROM `cm` WHERE `id`='$numero'";
$result = mysql_query($sqls);
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}
$row = mysql_fetch_assoc($result);
$condicion = $row['cond'];

You should switch over from the mysql_ driver to the mysqli_ or PDO, PHP PDO and MySQLi.

Community
  • 1
  • 1
chris85
  • 23,846
  • 7
  • 34
  • 51