Strange SQL Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Strange SQL Error

Post 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 }
										
																	}
	 														}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post 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.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

Cool thanks,
Is there a way to automatically clear this table each week?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Cron jobs, or if you are using a MySQL version of 5 or better, triggers.
Post Reply