Page 2 of 2

Re: User "confirm-event" switch

Posted: Wed May 21, 2008 5:24 am
by jason87
You guys are helpful! I'm learning every minute. I'll be sure to check out the tutorials you mentioned, califdon, and thank you, dbemowsk, for the bit on code redundancy. I'm beginning to see some broader perspective and will definitely keep these things in mind when further developing my php.
As for the links, it seemed no matter how I broke them down with single-quotes, double-quotes, different variables, they just didn't accept the "$" sign in the variables. At least that's what my countless eliminations of various potential problems showed me. I went with the FORM/Submit solution, and this is working out fine except for one thing. When adding the confirmation data to my confirmation table, I get a syntax error. And I don't just go in here asking for help, I've tried every combination of quotes around the variables, parentheses, and what have you, but I still get the error. Here's some code:

Code: Select all

 
// confirm/deny date
if(isset($_GET['confirm']) AND isset($_GET['date_id'])) {
    $date_id=$_GET['date_id'];
    $confirm=$_GET['confirm'];
    $addconf = mysql_query("INSERT INTO oever_conf (confirmation, user_id, date_id) WHERE date_id = ".$date_id." AND user_id = ".$session_name." VALUES ('$confirm', '$session_name', '$date_id')") or die(mysql_error());
    }
// END confirm/deny date
 
Now, here's what I get from the server:

Code: Select all

 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE date_id = 5 AND user_id = Jason VALUES ('1', 'Jason', '5')' at line 1
 
Once again thank you for lending me your expertise.

Re: User "confirm-event" switch

Posted: Wed May 21, 2008 6:45 am
by dbemowsk
Just a quick reply as I have to leave for work soon.

You are correct about the single/double quotes. I wasn't even thinking. single quotes won't parse a variable.

try this.

Code: Select all

 
echo "<a href=\"practice_dates.php?confirm=1&date_id=$date_row['date_id']\">Confirm date</a>";
 
//or this using concatenation
 
echo '<a href="practice_dates.php?confirm=1&date_id=' . $date_row['date_id'] . '">Confirm date</a>';
 
That should fix it. In the first one, notice the \" instead of just ". This is escaping the double quote so it can be used within a double quote. Using the =' . $date_row['date_id'] . '" in the second one is considered concatenating your variable with the periods.

Re: User "confirm-event" switch

Posted: Wed May 21, 2008 7:06 am
by jason87
Hi,
Yeah, the links are fixed now, it worked perfectly. I still, however, get the syntax error from MySQL as posted earlier. I don't know what to do about that. I keep trying different combinations of quotes, concatenation and similiar things to get rid of the error, but I've run out of ideas. Can you see from my code snippet in my previous post what I might be doing wrong?

Thanks

Re: User "confirm-event" switch

Posted: Wed May 21, 2008 8:46 pm
by dbemowsk
Sorry, I should have caught that. An sql INSERT INTO statement does not have a where clause. It is typically

Code: Select all

INSERT INTO database_name.table_name (field1, field2, etc...) VALUES (value1, value2, etc...);
Try this.

Code: Select all

$addconf = mysql_query("INSERT INTO oever_conf (confirmation, user_id, date_id) VALUES ('$confirm', '$session_name', '$date_id')") or die(mysql_error());
Also, I noticed that you are actually using a name for your user_id. Do you have your users stored in a database table? If not, you might want to think about that for future expansion of your systems. This gets back into relational database theory. If you store your users in a database table, you can store information that relates to them in that table, such as where they live, what instrument(s) they play, and any other information that might relate to a band member. I would then use an auto increment primary key field and reference that key field ID as the user_id. Doing it that way, any time you reference a persons ID number, you can then get any of the information you have stored for that ID. In the database it takes up less space when you reference say, user_id 4 vs user_id Jason.

Remember to think logically when it comes to your data and code, and plan a bit for future expansion.