Submitting data to database error

Discussion in 'PHP' started by sbaldam1992, Mar 5, 2010.

  1. #1
    Hi
    It seems on my php script whenever i submit data to the database it shows this error
    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 'show, height, width ) VALUES ( 'ep name', 'ep descriptio' at line 7
    Code (markup):

    The php is as follows:
    <?php
    $host = "localhost"; // The host for your server, this is usually localhost.
    $user = "xnalarac"; // Your username for the SQL.
    $database = "xnalarac_scifi"; // This is the database you created for this script, this is usually username_databasename
    $password = "*******"; // The password for the SQL.
    
    $conn = mysql_connect("{$host}","{$user}","{$password}");
    mysql_select_db("{$database}", $conn);
    
    function p($s){
    	return mysql_real_escape_string(htmlspecialchars($s));
    }
    
    if($_POST['input']){
    	$name = p($_POST['name']);
    	$desciption = p($_POST['desciption']);
    	$swf = p($_POST['swf']);
    	$episode = p($_POST['episode']);
    	$series = p($_POST['series']);
    	$show = p($_POST['show']);
    	$height = p($_POST['height']);
    	$width = p($_POST['width']);
    	
    	$q = mysql_query("INSERT INTO Episode (
    					name,
    					desciption,
    					swf,
    					episode,
    					series,
    					show,
    					height,
    					width
    				) VALUES (
    					'{$name}',
    					'{$desciption}',
    					'{$swf}',
    					'{$episode}',
    					'{$series}',
    					'{$show}',
    					'{$height}',
    					'{$width}'
    				)") or die (mysql_error());
    	echo ($q == false) ? "An error occured!" : "Success! yay";
    }else{?>
    	<form method="post">
    		<input type="hidden" name="input" value="1"/>
    		Episode Name:
    		<input type="text" name="name" />
    		<br />
    		Episode Description:
    		<input type="text" name="desciption" />
    		<br />
    		Episode SWF:
    		<input type="text" name="swf" />
    		<br />
    		Episode Number
    		<input type="text" name="episode" />
    		<br />
    		Series:
    		<input type="text" name="series" />
    		<br />
    		Show:
    		<select name="category"><?
    		$c = mysql_query("SELECT * FROM Shows");
    		while($a = mysql_fetch_assoc($c)){
    			echo "<option value='{$a['id']}'>{$a['showname']}</option>";
    		}?>
    		</select>
    
    
    		<br />
    		Height:
    		<input type="text" name="height" />
    		<br />
    		Width:
    		<input type="text" name="width" />
    		<br />
    		
    		
    		</select>
    		<br />
    		<input type="submit" name="input" value="Submit" />
    	</form> 
    <?}?>
    PHP:
    The database is fine and the login details are fine. but we can't find any errors in the script :p

    Thank you for your help.
    -sbaldam1992
     
    sbaldam1992, Mar 5, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi,

    Show is MySQL reserved word and you use it as column name!
    try `show` instead:
    
    $q = mysql_query("INSERT INTO Episode (
                        name,
                        desciption,
                        swf,
                        episode,
                        series,
                        `show`,
    ............................
    
    PHP:
    Regards :)
     
    koko5, Mar 5, 2010 IP
    calcalmx123 likes this.
  3. jimmy4feb

    jimmy4feb Peon

    Messages:
    56
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    just change the name of your "$show" variable to something else & run the script, then you will be fine. But make sure that you will not again choose any reserved MySql keyword :)

    Thanks,

    Jimmy
     
    jimmy4feb, Mar 5, 2010 IP
  4. sbaldam1992

    sbaldam1992 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you both for your reply's.
    It seems the problem is fixed. If only MySql gave better errors :p
     
    sbaldam1992, Mar 5, 2010 IP