简体   繁体   中英

Mysql fails in php but works in phpmyadmin

I've made this a lot of times but now I can't :(

The insert allways return false but if I execute the same SQL script (taked from the output) it inserts in the database without any problem. I'm connected to the database because some values are fetched from another table.

This is my code:

     $query = "INSERT INTO normotensiones(fecha,macropera,pozo,equipo_pmx,equipo_compania,paciente,sexo,edad,id_compania,otra_compania,puesto,ta,tum,ove,coordinador) 
                                VALUES('$fecha','$macropera','$pozo','$equipo_pmx','$equipo_compania','$paciente','$sexo',$edad,$id_compania,'$otra_compania','$puesto','$ta','$tum','$ove','$coordinador')";
        if (mysql_query($query,$connection)){
            //OK
        } else {
            $errno = mysql_errno();
            $error = mysql_error();
            mysql_close($connection);
            die("<br />$errno - $error<br /><br />$query");
            exit;
        }

The output is:

0 -

INSERT INTO normotensiones(fecha,macropera,pozo,equipo_pmx, equipo_compania,paciente,sexo,edad,id_compania, otra_compania,puesto,ta,tum,ove,coordinador) 
                VALUES('20111001','P. ALEMAN 1739','P. ALEMAN 1715','726', 'WDI 838','SERGIO AYALA','M',33,21, '','','110/70','ROBERTO ELIEL CAMARILLO','VICTOR HUGO RAMIREZ','LIC. PABLO GARCES')

Looks like there are no error, but allways execute the code in the else part of the if instruction. Any idea? Thanks in advance.

I think the issue might be you are missing the mysql_select_db line after the connection.

After the connection with the database is established you need to select a DB. Please make sure you have selected the Database that your desired table resides in.

And you can even use the following snippets to get some useful informated through mysql_errors.

    $connection = mysql_connect('localhost', 'root', 'password');

    if (!$connection) {
        die('<br>Could not connect: ' . mysql_error());
    }

    if (!mysql_select_db('db_name')) {
       die('Could not select database: ' . mysql_error());
    }

And try you insert query after these lines of code. All the best.

I agree with the others concerning the column types. INT is one of the only data types that do not require single quotes.

There are two blank strings. There is a possibility that the variables are not defined, and therefore giving you a PHP exception (not even in the MySql yet) but that requires stricter-than-normal exception settings. I would personally look into the $connection variable. Before the SQL query statement, put this and send us the cleaned results:

echo '<pre>'.var_dump($connection, true).'</pre>';

Additionally, on your mysql_connect function call, put

OR die('No connection')

afterwords. Do the same thing with the mysql_select_db function, changing it to 'No DB Select' obviously.

Ultimately, we will need more information. But changing to mysqli is very desirable.

Oh! And make sure the permissions for the user you are connecting as are not changed. Sometimes I find people who connect to PhpMyAdmin using one user account but a different account in their PHP code. This is problematic, and will lead to problems eventually, as you forget the different accounts, at times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM