Morthian Posted April 15, 2007 Share Posted April 15, 2007 I am a bit of newbie to PHP. I am trying to create a file upload script and just learn as I code it. Don't be surprised if there are a lot of errors in my code. This is the specific error I am having problems with: 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 'name, website url, genre, comment) VALUES ('Vitalize!','320','240','TestGame' at line 1 My code: <?php $con = mysql_connect("localhost", "angryclo_user", "********"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("angrycloud", $con); $sql = "INSERT INTO game_data (type, width, height, title, website name, website url, genre, comment) VALUES ('$_POST[type]','$_POST[width]','$_POST[height]','$_POST[title]','$_POST[website_name]','$_POST[website_url]','$_POST[genre]','$_POST[comment]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } { if (($_FILES["file_source"]["type"] != "application/x-shockwave-flash") && ($_FILES["file_source"]["type"] != "application/x-cnc")) { die("Invalid file type." . "<br />"); } if ($_FILES["file_source"]["size"] > 500000) { die("File is too large." . "<br />"); } if ($_FILES["file_source"]["error"] > 0) { die("Return Code: " . $_FILES["file_source"]["error"] . "<br />"); } else { echo "Upload: " . $_FILES["file_source"]["name"] . "<br />"; echo "Type: " . $_FILES["file_source"]["type"] . "<br />"; echo "Size: " . ($_FILES["file_source"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file_source"]["tmp_name"] . "<br />"; move_uploaded_file($_FILES["file_source"]["tmp_name"], "upload/" . $_FILES["file_source"][$mysql_insert_id()]); echo "Stored in: " . "upload/" . $_FILES["file_source"][$mysql_insert_id()] . "<br />"; } } echo "Upload successful."; mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/47147-mysql-syntax-error/ Share on other sites More sharing options...
papaface Posted April 15, 2007 Share Posted April 15, 2007 should be: $sql = "INSERT INTO game_data (type, width, height, title, website name, website url, genre, comment) VALUES ('".$_POST['type']."','".$_POST['width']."','".$_POST['height']."','".$_POST['title']."','".$_POST['website_name']."','".$_POST['website_url']."','".$_POST['genre']."','".$_POST['comment']."')"; Link to comment https://forums.phpfreaks.com/topic/47147-mysql-syntax-error/#findComment-229920 Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 Avoid spaces in column names. If you really must have them, use `..` around the name INSERT INTO game_data (type, width, height, title, `website name`, `website url`, genre, comment) .... Link to comment https://forums.phpfreaks.com/topic/47147-mysql-syntax-error/#findComment-229944 Share on other sites More sharing options...
Morthian Posted April 15, 2007 Author Share Posted April 15, 2007 Yes, the spaces must have been the problem. Thanks for the help. But now I keep getting this error: Error: No database selected I updated my code a bit since my first post: <?php $con = mysql_connect('localhost', 'angryclo_user', '********'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('angryclo_angrycloud', $con); $sql = "INSERT INTO game_data (type, width, height, title, website_name, website_url, genre, comment) VALUES ('".$_POST['type']."','".$_POST['width']."','".$_POST['height']."','".$_POST['title']."','".$_POST['website_name']."','".$_POST['website_url']."','".$_POST['genre']."','".$_POST['comment']."')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } { if (($_FILES["file_source"]["type"] != "application/x-shockwave-flash") && ($_FILES["file_source"]["type"] != "application/x-cnc")) { die("Invalid file type." . "<br />"); } if ($_FILES["file_source"]["size"] > 500000) { die("File is too large." . "<br />"); } if ($_FILES["file_source"]["error"] > 0) { die("Return Code: " . $_FILES["file_source"]["error"] . "<br />"); } else { echo "Upload: " . $_FILES["file_source"]["name"] . "<br />"; echo "Type: " . $_FILES["file_source"]["type"] . "<br />"; echo "Size: " . ($_FILES["file_source"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file_source"]["tmp_name"] . "<br />"; if ($_FILES["file_source"]["type"] == "application/x-shockwave-flash") { $filename = ($mysql_insert_id() . ".swf"); } if ($_FILES["file_source"]["type"] == "application/x-cnc") { $filename = ($mysql_insert_id() . ".ccn"); } move_uploaded_file($_FILES["file_source"]["tmp_name"], "upload/" . $_FILES["file_source"][$filename]); echo "Stored in: " . "upload/" . $_FILES["file_source"][$filename] . "<br />"; } } echo "Upload successful."; mysql_close($con) ?> The database name is shown just as it is in PHPMyAdmin. Link to comment https://forums.phpfreaks.com/topic/47147-mysql-syntax-error/#findComment-229967 Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 you could try running this to check database names <?php $con = mysql_connect('localhost', 'angryclo_user', '********'); $query = "SHOW DATABASES"; $result = mysql_query($query); while ($row = mysql_fetch_row($result)) { echo $row[0], '<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/47147-mysql-syntax-error/#findComment-229978 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.