user114671
user114671

Reputation: 532

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.

Upvotes: 0

Views: 1972

Answers (2)

Zulqurnain abbas
Zulqurnain abbas

Reputation: 732

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

Upvotes: 0

Sean
Sean

Reputation: 12433

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));

Upvotes: 2

Related Questions