Jump to content

PHP and MySQL Problem


lilman

Recommended Posts

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

So the sql is correct, but the values are not in the database?  There are still many possibilities.

1.  The query fails
2.  The query succeeds, but another query overwrites the values later
3.  The query never runs at all
4.  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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.