Jump to content

help with inserting data into mysql


dekon
Go to solution Solved by Jessica,

Recommended Posts

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>';  
            }  
        }  
    }  

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites


<?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);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.