dekon Posted March 18, 2013 Share Posted March 18, 2013 could someone help me i can't seem to insert data into mysql table and don't know whats gone wrong //create_cat.php include 'mysql.php'; include 'header.php'; echo '<h2>Create a topic</h2>'; if($_SESSION['loggedIn'] == false) { //the user is not signed in echo 'Sorry, you have to be <a href="signin.php">signed in</a> to create a topic.'; } else { //the user is signed in if($_SERVER['REQUEST_METHOD'] != 'POST') { //dropdown is being used here where we'll retrieve the catagories from the database for use in the dropdown $sql = "SELECT id, name, description FROM catagories"; $result = mysql_query($sql); if(!$result) { //query did not work echo 'Error while selecting from database. Please try again later.'; } else { if(mysql_num_rows($result) == 0) { //there are no catagories, so a topic can't be posted if($_SESSION['user_level'] == 1) { echo 'You have not created catagories yet.'; } else { echo 'Before you can post a topic, you must wait for an admin to create some catagories.'; } } else { echo '<form method="post" action=""> Subject: <input type="text" name="subject" /> Category:'; echo '<select name="cat">'; while($row = mysql_fetch_assoc($result)) { echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>'; } echo '</select>'; echo 'Message: <textarea name="post_content" /></textarea> <input type="submit" value="Create topic" /> </form>'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/275813-help-with-inserting-data-into-mysql/ Share on other sites More sharing options...
Maq Posted March 18, 2013 Share Posted March 18, 2013 We need more information. What happens? Are there errors? Do you have error reporting turned on? Quote Link to comment https://forums.phpfreaks.com/topic/275813-help-with-inserting-data-into-mysql/#findComment-1419335 Share on other sites More sharing options...
dekon Posted March 18, 2013 Author Share Posted March 18, 2013 An error occured while inserting your data. Please try again later.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 'by) VALUES('session state help', ' at line 5 this is the error i keep getting Quote Link to comment https://forums.phpfreaks.com/topic/275813-help-with-inserting-data-into-mysql/#findComment-1419337 Share on other sites More sharing options...
Barand Posted March 18, 2013 Share Posted March 18, 2013 Echo the sql query string so we can see it Quote Link to comment https://forums.phpfreaks.com/topic/275813-help-with-inserting-data-into-mysql/#findComment-1419340 Share on other sites More sharing options...
Jessica Posted March 18, 2013 Share Posted March 18, 2013 Yeah I don't see any code that would generate SQL containing a "VALUES". All you have shown us is a SELECT. Quote Link to comment https://forums.phpfreaks.com/topic/275813-help-with-inserting-data-into-mysql/#findComment-1419354 Share on other sites More sharing options...
dekon Posted March 18, 2013 Author Share Posted March 18, 2013 <?php //create_cat.php include 'mysql.php'; include 'header.php'; echo '<h2>Create a topic</h2>'; if($_SESSION['loggedIn'] == false) { //the user is not signed in echo 'Sorry, you have to be <a href="signin.php">signed in</a> to create a topic.'; } else { //the user is signed in if($_SERVER['REQUEST_METHOD'] != 'POST') { //dropdown is being used here where we'll retrieve the catagories from the database for use in the dropdown $sql = "SELECT id, name, description FROM catagories"; $result = mysql_query($sql); if(!$result) { //query did not work echo 'Error while selecting from database. Please try again later.'; } else { if(mysql_num_rows($result) == 0) { //there are no catagories, so a topic can't be posted if($_SESSION['user_level'] == 1) { echo 'You have not created catagories yet.'; } else { echo 'Before you can post a topic, you must wait for an admin to create some catagories.'; } } else { echo '<form method="post" action=""> Subject: <input type="text" name="subject" /> Category:'; echo '<select name="cat">'; while($row = mysql_fetch_assoc($result)) { echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>'; } echo '</select>'; echo 'Message: <textarea name="post_content" /></textarea> <input type="submit" value="Create topic" /> </form>'; } } } else { //start the transaction $query = "BEGIN WORK;"; $result = mysql_query($query); if(!$result) { //Damn! the query failed, quit echo 'An error occured while creating your topic. Please try again later.'; } else { //the form has been posted, so save it //insert the topic into the topics table first, then we'll save the post into the posts table $sql = "INSERT INTO topics(subject, date, cat, by) VALUES('" . mysql_real_escape_string($_POST['subject']) . "', NOW(), " . mysql_real_escape_string($_POST['cat']) . ", " . $_SESSION['user_id'] . " )"; $result = mysql_query($sql); if(!$result) { //something went wrong, display the error echo 'An error occured while inserting your data. Please try again later.' . mysql_error(); $sql = "ROLLBACK;"; $result = mysql_query($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/275813-help-with-inserting-data-into-mysql/#findComment-1419360 Share on other sites More sharing options...
Solution Jessica Posted March 18, 2013 Solution Share Posted March 18, 2013 "by" is a reserved word in MySQL. You should either change the column name (preferable) or use backticks around it. Quote Link to comment https://forums.phpfreaks.com/topic/275813-help-with-inserting-data-into-mysql/#findComment-1419364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.