Jump to content
confused and dazed

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

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)?>

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I mean you're not running the query. You define a variable called $sql, but you don't do anything with it.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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...

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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')";

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

and does the code 'A' inserting new rows?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...