lilman Posted November 14, 2006 Share Posted November 14, 2006 I am puzzled as to why this isn't work. What I am trying to do is this: I have a questions that members can fill out and store the answers in a database, but when a user has already entered information I want to delete the previous record and add the new information. I got it to execute the query, no problem there. I then wanted to check before it executed the query to see if they already had submitted a previous record, so I did that by adding this code above the INSERT query so it would check first (if found that they did, delete that record) then exit the if statement and then add the new information. However when I added this if statement it wouldn't insert the information, it just filled in 0s for everything. Here is both the check script (see if a user already entered something) and then below it, is the insert part. [code]// CHECK AND SEE IF A USER ALREADY ENTERED DATA$sql1 = "SELECT user_name FROM findafriend WHERE user_name='$user'";$runquery = mysql_query($sql1);$rownum = mysql_num_rows($runquery);if($rownum==1){ // DELETE CURRENT INFO $sqldelete = "DELETE FROM findafriend WHERE user_name='$user'"; $result = mysql_query($sqldelete);}// ADD NEW INFO $sql = "INSERT INTO findafriend VALUES('$userid'," ."'$music[1]', '$music[2]', '$music[3]', '$music[4]', '$music[5]', '$music[6]', '$music[7]', '$music[8]', " ."'$sports[1]', '$sports[2]', '$sports[3]', '$sports[4]','$sports[5]', '$sports[6]','$sports[7]', '$sports[8]', '$sports[9]', " ."'$movies[1]', '$movies[2]', '$movies[3]', '$movies[4]', '$movies[5]', '$movies[6]', " ."'$interests[1]', '$interests[2]', '$interests[3]', '$interests[4]', '$interests[5]', '$interests[6]', " ."'$personality[1]', '$personality[2]', '$personality[3]', '$personality[4]', '$personality[5]', '$personality[6]', '$user')"; $result = mysql_query($sql) or die("Error ". mysql_error. " with query ". $sql);[/code]When a user already entered information it will delete the record and add a new one, the problem is, the new record contains all 0s except for userid and username which are entered as they should.I hope I have made this clear so you know what it is that I am trying to accomplish and what it is that I have done to try and accomplish it. Any help or feedback as to why I might be having this problem would be greatly appreciated. Thank you Quote Link to comment Share on other sites More sharing options...
printf Posted November 14, 2006 Share Posted November 14, 2006 Where is ($music, $sports, $movies, $interests, $personality) being set? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 14, 2006 Share Posted November 14, 2006 If that was my script, I would add [code=php:0]print "SQL: $sql<br>";[/code] right after you set $sql for the insert. If zeroes are going into the database, then it's probably because the sql statement is putting zeroes into it :) Then go back through your script printing out the values of $music and so on, until you find where they are going wrong. Quote Link to comment Share on other sites More sharing options...
lilman Posted November 14, 2006 Author Share Posted November 14, 2006 When I do that, it shows up what should be entered into the databse but it doesn't add it, it is strange.Where is ($music, $sports, $movies, $interests, $personality) being set? On the previous page, it is from a form. I checked the values to make sure that there are values for them, it just doesn't put them into the database. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 14, 2006 Share Posted November 14, 2006 So the sql is correct, but the values are not in the database? There are still many possibilities.1. The query fails2. The query succeeds, but another query overwrites the values later3. The query never runs at all4. The query succeeds, but the code which reads the values is faulty, or is run at the wrong time.If the query never ran, then the print statement would never run. So it's very likely the query was run.If the query failed, you would get an error message, as you are checking for errors.That leaves 2 possibilities..Next question: How do you check the values in the database? Can you post the code? Or are you using a direct interface like phpmyadmin? Quote Link to comment Share on other sites More sharing options...
lilman Posted November 14, 2006 Author Share Posted November 14, 2006 All the variables are in similar format: $music[1] = isset($_POST['punk']) ? "1" : "0";When I view the query that is suppose to be executed and entered into the database, there are 1s but when I check the database there are just 0s Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.