Search code examples
phpmysqlphpmyadminsql-update

PHP, mysql_query just NOT working


I'm ready to pull my hair out...

This works perfectly:

$link = mysql_connect('localhost', 'prototype_wp_usr', 'password') or die("Error: " . mysql_error());
mysql_select_db('prototype_wp');
$query = "INSERT INTO wp_zsession_capture
        (id, session_id, user_login, first_name, last_name, user_email, phone_number, last_conviction)
        VALUES (
            'NULL', 
            '". session_id() ."',
            '". $_POST['user_login'] ."',
            '". $_POST['first_name'] ."',
            '". $_POST['last_name'] ."',
            '". $_POST['user_email'] ."',
            '". $_POST['phone_number'] ."',
            '". $_POST['last_conviction'] ."'
        )";

mysql_query($query) or die ('Error updating database: ' . mysql_error());
mysql_close($link);

This does NOT work:

$link = mysql_connect('localhost', 'prototype_wp_usr', 'password') or die("Error: " . mysql_error());
mysql_select_db('prototype_wp');
$query = "UPDATE wp_zsession_capture SET removed = 1 WHERE session_id = '$sessionid'";

mysql_query($query) or die ('Error updating database: ' . mysql_error());
mysql_close($link);

These two pieces of code are within a form... if there are errors in the form, the FIRST sql query is run to save whatever is in the session at that point. If there are no errors, and after some other stuff goes on, the SECOND sql query is run to UPDATE the 'removed' status within those previously saved entries.

I have been tinkering with a million alternative syntaxes and I cannot get those rows to UPDATE. When I execute raw SQL within phpMyAdmin, it works fine. There is nothing logical about this... or maybe I'm just so fantastically tired.

FYI: $sessionid = session_id();

-- UPDATE --

I really do appreciate everyone's concern, but let me address some things:

  1. Yes, there is a column in my table named 'removed' ... are you serious? :P
  2. I am of course using session_start();
  3. Yes, I am checking the $sessionid to make sure it is invoked. It's echoed on the page at all times and is also within any die statements I execute.
  4. The FIRST query is responsible for creating the rows that the SECOND query is supposed to be updating... so the data is there.
  5. mysql_error() does not produce anything ... there's simply no error.

Again, I appreciate your concern, but so far all suggestions have been tried and tried again.

enter image description here


Solution

  • Use mysql_error() to obtain the error message from the database, like this:

    mysql_query($query) or die ('Error updating database: '.mysql_error());
    

    The error could be one of many things, like for example maybe the user does not have the DELETE permission. If you don't use mysql_error() there's no way to know what happened.

    Edit The data in your session_id column looks suspiciously short. Check how long the strings returned by session_id are and check the column length in the database. Inserting data that is too long for a column is not an error but it does crop the data.