Page 1 of 1

Strange SQL Error

Posted: Tue Mar 13, 2007 3:27 pm
by thiscatis
I'm creating a simple vote script where the system logs the IP in a column,
if you vote it logs your IP in a column in the vote table and when you try to vote again
it checks if your IP is already in the table.

But I keep getting:


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 '.253.130' at line 1

my code:

Code: Select all

<?php 
	
	 if (isset($_GET['vote'])) {	
	 
	 $m_id = preg_replace('/[^0-9\_]/', '', $_GET['vote']);
	 $ip = $_SERVER['REMOTE_ADDR'];
	 
	 $result = mysql_query("SELECT * FROM votes WHERE ip=$ip ") or die(mysql_error());
	 $num_rows = mysql_num_rows($result);
	 		
			 if ($num_rows > 0) { echo "<div id=voterr>You already voted</div>"; }
			 
			 else {
					
					$result = mysql_query("SELECT * FROM users where m_id=$m_id ") or die(mysql_error());
					
							while($row = mysql_fetch_array($result)) {
											
											$m_votes = $row['m_votes'];
					
											// code logging the vote }
										
																	}
	 														}

Posted: Tue Mar 13, 2007 3:36 pm
by volka
strings have to be quoted for mysql

Code: Select all

WHERE x=4
but

Code: Select all

WHERE x='mary had a little lamb'
Your ip address is a string for mysql.

Posted: Tue Mar 13, 2007 3:36 pm
by crazytopu
In this case I would always, echo my sql. take off the error handling code first and then try to print your sql.

My guess is you probably have to use quote

Code: Select all

   


"SELECT * FROM votes WHERE ip='".$ip.'" " 

But could be sure if you had shown your table structure, which field is what type etc.

Posted: Tue Mar 13, 2007 4:17 pm
by thiscatis
Cool thanks,
Is there a way to automatically clear this table each week?

Posted: Tue Mar 13, 2007 5:31 pm
by RobertGonzalez
Cron jobs, or if you are using a MySQL version of 5 or better, triggers.