By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,034 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,034 IT Pros & Developers. It's quick & easy.

Need help Inserting/deleting from a database

P: 36
whats good

I am working on a php script. It takes in user input from a text field and then queries the database for it. If the input is found in the database then script performs a delete query to get that input out of the database. If it does not exist in the db then script inserts it. The problem I am having is concerns the variable that is used to store the users input from the text field. If I input something it inserts it into the database however when I try to insert something else right after the previously inserted input gets deleted in favor of the new input. This script is designed to only be able to delete what it has inserted. So data entered into the same db thru sql commands, other scripts etc. cant be touched. Can someone give me an idea on what I can do correct this bug?


Heres some code pieces (the script is too big to post the whole thing):

Expand|Select|Wrap|Line Numbers
  1. //create text field
  2. echo("<input type=text name=opt_qty value='".$_SESSION['opt_qty_vals']."' onBlur='submit();'  class='qty_input'> ");
  3. //variable to store user input
  4. $user_input_strike = $_REQUEST['opt_qty'];
  8. //Counts number of times input appears in db depending on specified details.
  9. $query_count = "SELECT COUNT(RAW_VOLS.STRIKE) FROM SMIRK_USR.RAW_VOLS RAW_VOLS WHERE (RAW_VOLS.STRIKE = $user_input_strike) AND (RAW_VOLS.ASOF_DATE=".$_SESSION['aod_val'].") AND (RAW_VOLS.SRC=".$_SESSION['src_val'].") AND (RAW_VOLS.SHARE_NAME=".$_SESSION['undl_val'].") AND (RAW_VOLS.CALL_PUT='".$_SESSION['opt_cp_vals']."') AND (RAW_VOLS.EXPIRE_DATE=".$_SESSION['opt_exd_vals'].")";
  11. //code to retrieve actual result of the query aka the count
  12. $parsed1 = ociparse($db_conn, $query_count);
  13. $succ1 = ociexecute($parsed1);
  14. //store count in array
  15. $result = oci_fetch_array($parsed1);
  16. $count_result = $result[0];
  18. //If count returns zero insert into DB.
  19. if ($count_result < 1)
  20. {
  21.     $insert_query = "insert into SMIRK_USR.RAW_VOLS (ASOF_DATE, SHARE_NAME, EXPIRE_DATE, CALL_PUT, STRIKE, OPEN_INT, SRC, PROCESSED) values($AOD_value, $UNDL_value, $expire_date_value, '$call_put_value',  $user_input_strike, $open_interest, $Source_value, '$processed_column')";
  22.     $parsed_insert = ociparse($db_conn, $insert_query);
  23.     $succ_insert = ociexecute($parsed_insert);
  24. }
  26. //else if count is greater then zero and the input has been inserted by this script
  27. //delete the input.  
  28. elseif (($count_result > 0) && ($rset_results_element0 < 0))
  29. {
  30.     $delete_query = "DELETE FROM SMIRK_USR.RAW_VOLS WHERE ( (ASOF_DATE = $AOD_value) AND (SHARE_NAME = $UNDL_value) AND (EXPIRE_DATE = $expire_date_value) AND (CALL_PUT = '$call_put_value') AND (STRIKE = $user_input_strike) AND (OPEN_INT = $open_interest) AND (SRC = $Source_value) )";
  31.     $parsed_delete = ociparse($db_conn, $delete_query);
  32.     $succ_delete = ociexecute($parsed_delete);
  33. }
  35. //else do nothing.  
  36. else
  37. {
  38.     //echo("Do nothing");
  39. }
Aug 16 '07 #1
Share this Question
Share on Google+
11 Replies

Expert 5K+
P: 5,821
Heya, orfiyus.

Actually, we usually like it better when people don't post their entire scripts. It shows that:
  1. You actually tried to fix the problem yourself first before asking for help, and
  2. You're knowledgeable enough about your own code to know where the problem should be.

In regards to your problem, does your table have a primary key? Try SELECTing that when you first check to see if the entry exists, then use it to DELETE the existing row if necessary.
Aug 16 '07 #2

P: 36
Itd probly help if I further explained what this script does. It queries the db and outputs a list of numbers. Then it comes out with the input field. When the user inputs a number that isnt currently in the database the page refreshes and the new number is added to the list. The problem I am having is that when I try to input another number that doesnt exist the page refreshes and the first input number is gone and replaced with the second input number. I think its a problem with either the variable I am using to store the user input or its a problem with my logic for the Inserting/deleting part. I tried using unset() to destroy the variable after its done being used but this didnt work. I dont know what the pk is because I dont have a db schema and I dont have direct access to the database anyway. I can only do things to the db through php. Any one else have any suggestions?

Thanks for reading though all of this.
Aug 17 '07 #3

Expert 5K+
P: 5,821
Heya, orfiyus.

Try running a DESCRIBE query on the table to determine its structure:
Expand|Select|Wrap|Line Numbers
  1. $_res = mysql_query('DESCRIBE `SMIRK_USR`.`RAW_VOLS`);
  2. echo '<table>';
  3. while($row = mysql_fetch_row($_res))
  4. {
  5.     echo '<tr>';
  6.     foreach($row as $val)
  7.     {
  8.         echo "<td>$val</td>";
  9.     }
  10.     echo '</tr>';
  11. }
  12. echo '</table>';
Aug 17 '07 #4

P: 36
I am using a oracle database so I basically I converted it to the best of my ability. The result I get for the output says its null. Which is weird because I actually see numbers on the screen and i am able to work with my insert and delete code portions.

Here is wut I said for the describe
Expand|Select|Wrap|Line Numbers
  1. echo("<table>");
  2. //describe query
  3. $query_describe = "DESCRIBE SMIRK_USR.RAW_VOLS";
  4. //parse the sql code
  5. $Describe_parse = ociparse($db_conn, $query_describe);
  6. //execute it just incase
  7. $Describe_execute = ociexecute($Describe_parse);
  8. //store the results of the execute in array
  9. $describe_array = oci_fetch_array($Describe_parse);
  11. echo("<br><br>");
  12. //var_dump it because its just easier 
  13. var_dump($Describe_array);
  15. echo("</table>");
Aug 17 '07 #5

Expert 5K+
P: 5,821
Heya, orfiyus.

Ah. Well then. I think in Oracle, the proper syntax is 'DESC `TableName`' instead of MySQL's 'DESCRIBE `TableName`'.
Aug 17 '07 #6

P: 36
I changed it but I still get null outputted to the screen.
Aug 17 '07 #7

Expert 5K+
P: 5,821
Heya, orfiyus.

I'm going to go ahead and move this thread to the Oracle forum, where our resident Experts will be better able to help you out.
Aug 17 '07 #8

P: 36
Allright Thanks for trying man

Im gonna reevaluate the logic cuz I think the problem is in there
Aug 17 '07 #9

P: 36
Mangaged to fix the insert part. But now the delete has the same problem as before.

Here a code sample maybe someone can see what is wrong with it. Maybe its logic or syntax cant figure it out. I tried using unset on the variable which stores the text field input but the whole script just went blank whenever I did that. Any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  2. //else if the query for the count is 1 or greater and the
  3. //option number is less then 0
  4. //Delete the number out of the db with appropriate parameters.
  5. elseif (($count_result > 0) && ($rset_results_element0 < 0))
  6. {
  8.         $open_interest = -1;
  9.         $delete_query = "DELETE FROM SMIRK_USR.RAW_VOLS WHERE ( (ASOF_DATE = $AOD_value) AND (SHARE_NAME = $UNDL_value) AND (EXPIRE_DATE = $expire_date_value) AND (CALL_PUT = '$call_put_value') AND (STRIKE = $user_input_strike) AND (OPEN_INT = $open_interest) AND (SRC = $Source_value) )";
  10.         $parsed_delete = ociparse($db_conn, $delete_query);
  11.         $succ_delete = ociexecute($parsed_delete);
  12.         var_dump($succ_delete);
Aug 17 '07 #10

Expert Mod 10K+
P: 14,534
Ar you trying to delete one field value from a larger table? If so you will need to use the UPDATE command instead of the DELETE command.

DELETE is only used when deleting full records from the table.
Aug 21 '07 #11

P: 36
I am trying to delete it like this. The script queries with a count to see if the input exists. If the count is greater than zero and another part of the record is negative 1 then delete the whole thing where the user input number is.

For example take this make shift table

  1. Number::other_field1::other_field2
  2. 136::-1::buddy
  3. 137::65::whooo

The first line is the name of each row seperated by "::"
The lines after are what is in the db.

If I type in 136 into the webpage. I want everything deleted that is next to it. The -1 and the buddy. I want the whole record for 136 deleted. If I type 137 then it wont delete cuz the other field isnt -1.

More or less its doing what I want. The problem is I had to split it into two text fields. One text field does the inserting and the other text field does the deleting. I need the one text field to do both.

Hope you can help me out.

Aug 23 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.