473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Need help Inserting/deleting from a database

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?


Thanks

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'];
  5.  
  6.  
  7.  
  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'].")";
  10.  
  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];
  17.  
  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. }
  25.  
  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. }
  34.  
  35. //else do nothing.  
  36. else
  37. {
  38.     //echo("Do nothing");
  39. }
  40.  
  41.  
.
Aug 16 '07 #1
11 1645
pbmods
5,821 Expert 4TB
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
orfiyus
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
pbmods
5,821 Expert 4TB
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>';
  13.  
Aug 17 '07 #4
orfiyus
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);
  10.  
  11. echo("<br><br>");
  12. //var_dump it because its just easier 
  13. var_dump($Describe_array);
  14.  
  15. echo("</table>");
  16.  
  17.  
Aug 17 '07 #5
pbmods
5,821 Expert 4TB
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
orfiyus
36
I changed it but I still get null outputted to the screen.
Aug 17 '07 #7
pbmods
5,821 Expert 4TB
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
orfiyus
36
Allright Thanks for trying man

Im gonna reevaluate the logic cuz I think the problem is in there
Aug 17 '07 #9
orfiyus
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
  1.  
  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. {
  7.  
  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);
  13.  
  14.  
  15.  
  16.  
  17.  
Aug 17 '07 #10
MMcCarthy
14,534 Expert Mod 8TB
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
orfiyus
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

Thanks
  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.

Thanks
Aug 23 '07 #12

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

Similar topics

10
by: Raghavendra Mahuli | last post by:
i need to store a lot of integers(say 10,000) in a datastructure. The constraints are: 1. It should be fast. 2. It should be orderded or sorted. 3.Insterting and deleting should be fast. 4. The...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
1
by: Arda Han | last post by:
Hi friends, I need a sample application for inserting,updating,deleting database rows. In this example I want learn creating MDI applications. Inserting,Deleting,Editing database rows on...
2
by: Clark Stevens | last post by:
Hi. This should be so easy, but I don't get it. Let say I have RichTextbox1 and I want to insert some text at the current insertion point, or at the beginning of selected text (if there is any). ...
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
1
by: yuchang | last post by:
Hi, Using the FormView control is very efficient. But I want to do some action,like showing a success message or redirect to another page, after inserting, updating and deleting. How do I get...
1
by: Nemisis | last post by:
hi guys, Currently converting an old classic asp system to a OOP asp.net application. We are building the new application using a 3 tier arcitecture and i was wondering about the following. ...
0
by: Kushwaha | last post by:
Hi, Any buddy help me to inserting multiple rows, Editing them, or deleting them in a any type of grid. I don't want to use any type of text box, combo box.
4
memoman
by: memoman | last post by:
Can any body help me in that program ??? mail me if anybody could reach any -helpfull- thing Write a C++ program that namely insert, delete, and search in a fixed record length file (containing...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.