Output problem

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Output problem

Post by John Cartwright »

Code: Select all

<?php
 <?php 

include("mysql.php"); 
include("header.php"); 

echo "<div align="center">"
	."<form action="{$_SERVER['PHP_SELF']}"\n"
	."<p>Type your Date here: eg. 2004-01-31 (y-m-d)<br/>\n" 
	."<input type="text" name="date" cols="40"><br>\n"
	."<p>Is this Upcoming or Past eg. Upcoming OR Past<br/>\n" 
	."<input type="text" name="uppast" cols="40"><br>\n"	
	."<p>Please enter the filename of the flyer eg. flyer24.jpg<br/>\n" 
	."<input type="text" name="pic" cols="40"><br>\n"
	."<p>Type your Description here here:<br/>\n" 
	."<textarea name="desc" rows="10" cols="40" wrap></textarea><br/>\n" 
	."<input type="submit" name="submitevents" value="SUBMIT" /></p>\n" 
	."</form>\n"
	."</div>";
echo "<div align="center"><strong>NOTE - </strong>To go to the next line type in < br > (without the spaces between < ) </div><br><br>";

//adding to the database
if ($submitevents == "SUBMIT") { 
  $sql = "INSERT INTO events SET date='$date', desc='$desc'";
if (@mysql_query($sql)) { 
    echo("<div align="center">Your Event has been added.</div>"); 
  } else { 
    echo ("<div align="center">Error adding submitted Event: " . mysql_error() . "</div>"); 
  } 
}
//deleting from the database
if (isset($deleteevents)) { 
      $sql = "DELETE FROM events WHERE id=$deleteevent"; 
      if (@mysql_query($sql)) { 
        	echo("<div align="center">The event has been deleted.</div>"); 
      }else{ 
        echo("<p>Error deleting event: " . mysql_error() . "</p>"); 
  } 
} 
//preparing data from database
$result = @mysql_query("SELECT * FROM events ORDER BY `date` ");

while ($row = mysql_fetch_array($result)) { 
   
	$newdate = explode("-",$row["date"]); 
	$month = $newdate[1]; // eg. February 
    $day = $newdate[2]; // eg. 04 
    $year = $newdate[0]; // eg. 2004 
    $id =  $row["id"];
	$desc = $row["desc"];

echo "<table width="668" border="0" cellpadding="0" cellspacing="0">\n"
    ."<tr>\n"
    ."<td width="22%" height="20">Date</td>\n"
    ."<td width="26%">Description</td>\n"
    ."<td width="32%">Location</td>\n"
    ."<td width="20%">Flyer</td>\n"
    ."</tr>\n"
	."<tr>\n"
    ."<td width="22%" height="20">".$month.$year."</td>\n"
    ."<td width="26%">".$desc."</td>\n"
    ."<td width="32%">".$location."</td>\n"
    ."<td width="20%">".$pic."</td>\n"
    ."</tr>\n"
    ."</table>\n"
	."<br><br>\n";
 } 
?>
?>
I'm getting the error:
Error adding submitted Event: 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 'desc='fdsfd'' at line 1
This is what's causing the problem ( I believe )

Code: Select all

<?php  $sql = "INSERT INTO events SET date='$date', desc='$desc'";
?>
But the thing is I have the same code on other pages without causing any problems :S
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

desc is reserved word. You need to quote it with backticks (`) if you're going to use it as field name.

Code: Select all

INSERT INTO events SET date='$date', `desc`='$desc'
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

replace "INSERT INTO events SET date='$date', desc='$desc'" with

Code: Select all

"INSERT INTO users (date, desc) VALUES('$date', '$desc')"
that should do it
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

wow I'm really stupid... I starred at that code for about 30 min going wtf is wrong with it :S......................

Much appreciated guys :D
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

glad it works.....no prob.....
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

dull1554 wrote:replace "INSERT INTO events SET date='$date', desc='$desc'" with

Code: Select all

"INSERT INTO users (date, desc) VALUES('$date', '$desc')"
that should do it
That didn't work...but the first one did .. TY again
Post Reply