Jump to content

My PHP code will not delete and replace data in mysql database


confused and dazed

Recommended Posts

Hello internet I am pulling out my hair trying to figure this one out. Below is the only code I have in the php file. What I am trying to do is delete the only line of data in the database table and replace it with a new line of data. The data will delete but it will not do the insert function to put the new data in. I started out trying an UPDATE function but that did not work either. Separately each set of <?php ?> code works if I put them in separate files but together they do not work. Why? How can I achieve the desired effect? Please help. <?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);mysql_query("DELETE FROM ViewIt WHERE misc='1'");mysql_close($con)?> <?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')";mysql_close($con)?>

Link to comment
Share on other sites

for Delete Query

mysql_query("DELETE *  FROM ViewIt WHERE misc='1'");  // or  misc = '$id' (it's will get if you clik button delete)

for Update Query

mysql_query("UPDATE *  FROM ViewIt WHERE misc='1'");  // misc = '$id'

Edited by fikiwan
Link to comment
Share on other sites

justsomeguy - O.K. so heres the problem... I get what you are saying but if I have the following code all by itself in a PHP file it works just fine. <?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')";mysql_close($con)?> But when I place the DELETE action with the INSERT action for some reason it does not work...

Edited by confused and dazed
Link to comment
Share on other sites

Well, "works" is sort of ambiguous. That code will execute, sure, but it's not going to do anything with your database other than connect and disconnect. I mean, just look at the code line by line. You connect, then select a database, then define a variable, then disconnect. It never sends any SQL to the database, regardless of where you put that code. Moving the code around isn't going to change what it does.

Link to comment
Share on other sites

justsomeguy - I always appreciate your feedback so dont think I am trying to be difficult. Please understand what I am trying to get across to you. If the only thing in the php file is the following code it will send the data to the database everytime. (I check my database and it does have the information in it)<?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')";mysql_close($con)?>If i place that same code along with the DELETE code the INSERT action will NOT work... I just can't understand why...

Link to comment
Share on other sites

No it will not send the data evrytime. unless you are using mysql_query() it wont send the query to database. $sql is now just a bunch of string and according to your code you are not executing the INSERT query. If the posted code is exact identical to your original code there is no way to happen INSERT

Link to comment
Share on other sites

justsomeguy - birbal - thanks for taking the time to respond to my posts. What I dont understand is why Code "A" will insert data into my database EVERYTIME and I mean EVERYTIME - This is the only code in the PHP file and it will place data in my database EVERYTIME. BTW I always make sure there is only one data entry in my database before I run the INSERT action file. CODE "A"<?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')";mysql_close($con)?>If I put have CODE "B" as the only code in the PHP file it will delete the data but will not insert any new data.CODE "B"<?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);mysql_query("DELETE FROM ViewIt WHERE misc='1'");mysql_close($con)?><?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')";mysql_close($con)?> So I played around with the code a little and I was able to make CODE "C" work and get the desired effect but CODE "D" & CODE "E" will not work. CODE "C"<?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);}mysql_select_db("cp2000", $con);mysql_query("DELETE FROM ViewIt WHERE misc='1'");mysql_query("INSERT INTO ViewIt (misc, CC) VALUES ('1', '2')");mysql_close($con)?>CODE "D"<?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);}mysql_select_db("cp2000", $con);mysql_query("DELETE FROM ViewIt WHERE misc='1'");$sql="INSERT INTO ViewIt (misc, CC) VALUES ('1', '2')";mysql_close($con)?> CODE "E"<?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);mysql_query("DELETE FROM ViewIt WHERE misc='1'");mysql_close($con)?><?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);mysql_query("INSERT INTO ViewIt (misc, CC) VALUES ('1', '2')");mysql_close($con)?> I am left wondering why CODE "A" will work by itself but not with any other code - this is what is confusing me the most. I am also confused as to why CODE "C" will work and CODE "E" will not. I am guessing that has something to do with opening and shutting a connection.

Link to comment
Share on other sites

I am a beginner and what i say i could have wrong. I think they are saying you have stored you statement in a variable. but you have not run the query. you have to run the query the built in words mysql_query then store the value in variable. ( A variable will have a dollar sign ) mysql_query("INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')); // you have a vairiable$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')";

Link to comment
Share on other sites

I guess I will never get to the bottom of it...hansolo - I get what you are saying and what everyone else is saying. The problem is that no matter how many ways anyone tries to explain $sql="INSERT... is not running a query and therefore cannot send any information to mysql it’s still not accurate. It IS sending information to my database every time. The issue is that if I couple that code with other code at that point it will NOT work and NOT send data to my database.

Link to comment
Share on other sites

The issue is that if I couple that code with other code at that point it will NOT work and NOT send data to my database.
Start adding debug statements, echoing mysql error. If it is not executing the query it would say why. does code A is exact copy of the code you are executing?
Link to comment
Share on other sites

and does the code 'A' inserting new rows?

Link to comment
Share on other sites

??? it should not insert, there is no mysql_query($sql); to insert the code, so IF you have one record in database as you say, because it is not deleted, and there is no mysql_query() to insert a new record, you should still just have the original record. You should only have a single

$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);

at the top, with a single

mysql_close($con)

at the end of the page to finally close the connection.

Link to comment
Share on other sites

dsonesuk - could it be the way I have mysql database set up? Honestly I don’t want to waste anymore of anyone’s time on this one. I thought that by understanding why it is doing this I could learn from it and use that knowledge later. I guess I will have to leave it alone. The reason why I open and close connections like this because at one point I needed to do this for a site I was working on and I guess I just kept doing it. I will re-evaluate my use of opening a $con and closing it the way I do. Thanks.

Link to comment
Share on other sites

I'm sorry, but this code will not change your database:

<?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')";mysql_close($con)?>

If you don't believe me, then paste exactly that code in a file, change the connection details, and run it on an empty database. There is no function call there to send the query to the database. PHP will not do that automatically. The one and only way to use the mysql extension to send commands to the database is to use either the mysql_query function, or mysql_unbuffered_query. You are not using those functions, therefore you are not sending any queries to the database. If you're under the impression that the above code works, then you're wrong. I don't know how else to say it, it's just wrong. If you have that code in a file that does actually insert to a database, then look at the code in that file and you will see that it uses mysql_query to send the query to the database. The code above will never, under any circumstances, send the query to MySQL. I'm willing to stake my entire reputation on that claim. That is what I was trying to get you to notice, that you were not using mysql_query. Add a call to mysql_query and you will see that code work wherever you decide to copy it to (although there's no reason to connect and disconnect to the same database more than once on a page). If you're trying to interact with a database using the mysql extension, the mysql_query function is not optional. PHP isn't magic, things happen for specific reasons. Your code does not "work" because you are not sending the query to the database. That is the only reason.

Link to comment
Share on other sites

It’s not about believing you. I'm sure what you are saying is correct. I am trying to figure out why its letting me enter data into the database this way.... It must be that there is some other code doing this that I just am not aware of. BTW since I was able to accomplish the goal I was reaching for I have since deleted the file I claim was doing this... I don’t have the code any longer to reference. I'm sure what you are saying is correct - I did make a file with just this code again and it does not send data... very interesting. There must have been other code allowing it or doing it.

Link to comment
Share on other sites

post your full code and then maybe some one can highlight in red. To show what you have missed.The red part will stand out mysql_query("INSERT INTO ViewIt (misc, CC)VALUES ('1', '2'));// you have a vairiable$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')"; this the letters you missed out i have shown them in red mysql_query you had it stored in variable called $sqlwrite this mysql_query

Link to comment
Share on other sites

<?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);mysql_query("DELETE FROM ViewIt WHERE misc='1'");mysql_close($con)?> <?php$con = mysql_connect("xxx","xxx","xxx");mysql_select_db("xxx", $con);$sql="INSERT INTO ViewIt (misc, CC)VALUES ('1', '2')";mysql_close($con)?> then you need to submit the values. Ask some guy for the code

Edited by hansolo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...