简体   繁体   中英

MySQL INSERT query works in Phpmyadmin but not in PHP

Very strange. I can't see what's going wrong here. The connection to the MySQL database has been made but it won't INSERT from PHP. It's fine if I run the query in Phpmyadmin.

$rawquery = "
    INSERT INTO $log_table_name
        (ref, timestamp, txn_id, email, item_name, item_number, custom, mc_gross, mc_currency, paypal_message)
    VALUES
        (NULL, CURRENT_TIMESTAMP, '$txn_id', '$payer_email', '$item_name', '$item_number', '$custom', '$payment_amount', '$payment_currency', 'INVALID');
";
echo $rawquery;
$query = mysql_query($link, $rawquery) or die('Could not access table');

Produces:

INSERT INTO wp_ipn_log
    (ref, timestamp, txn_id, email, item_name, item_number, custom, mc_gross, mc_currency, paypal_message)
VALUES
    (NULL, CURRENT_TIMESTAMP, '', '', '', '', '', '', '', 'INVALID');Could not access table

I'm expecting the INVALID message, I just want it to be inserted into the database.

Is the problem the format of the query, or is there an issue with the database, or something else?

ADDITIONAL INFO (as requested by vinodadhikary):

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db(DB_NAME, $link) or die ("Could not open db ".mysql_error());

This is working fine.

When trying to debug code, it is helpful to use mysql_error() / mysqli_error($link) in your die() rather than a generic string - die('Could not access table') .

Also, the order of query/link in mysql/mysqli is not the same

mysql_query is mysql_query(query,link) , so your code should be

$query = mysql_query($rawquery,$link) or die(mysql_error()); 

while mysqli_query is mysqli_query(link, query) , so your code should be

$query = mysqli_query($link, $rawquery) or die(mysqli_error($link));

Need to check few things on this,

1 - is NULL allowed for ref field name ?
2 - paypal_message check is this varchar or text and check length also in database ?
3 - can you try removing '' from each values.
4 - last check empty is allowed ?

you can try doing with some sample values instead of empty. check if that query works, so you can go near around to solution.

Hope these things helped you

thanks

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