Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012

    Unanswered: MySQL Insert issue


    I have a preloaded database that I would like for my users to be able check (via a form with checkboxes) the items they can use for their project, while copying those columns to a different table for future reference, and meanwhile deleting the columns from the first table so they are not displayed in the initial form the next time a user checks availability of the items.

    The code I'm currently using will delete the columns from the first table, but will not copy them to the second table. Please let me know what I could try to correct the following code.

    $dbconn = mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
        if($_POST['delete']) // from button name="delete"
        $checkbox = $_POST['checkbox']; //from name="checkbox[]"
        $countCheck = count($_POST['checkbox']);
        $del_id = $checkbox[$i];
        $sql = "INSERT INTO table2 (col1, col2, col3, col4) SELECT col1, col2, col3, col4 FROM table1 WHERE id='$del_id'";
        $sql = "delete from table1 where id = $del_id";
        $result = mysql_query($sql, $dbconn);
    Thank in advance for any help you can give!

  2. #2
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    if 'its not working' is all you know check what you SQL actually is, and make certain its dong what you think it is
    so I'd suggest using the debugging options assumi g you are using a debug aware IDE such as Eclipse or Netbeans, or put
    PHP Code:
    Die $sql 
    after your code that builds the SQL

    or of course you could try and execute the SQL before overwriting the value with 'delete....'
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Oct 2012
    By commenting out the "delete" function, I am able to make it copy the columns to the 2nd table.

    What could I do to make it run the "delete" function afterwards?

  4. #4
    Join Date
    Sep 2009
    San Sebastian, Spain
    I think that answers your question. Assigning a query to a variable does not make it execute in the database. After the insert statement has been assigned to $sql then you should run mysql_query.
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    Follow me on Twitter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts