www.webdeveloper.com
Results 1 to 12 of 12

Thread: Troublesome query statement

  1. #1
    Join Date
    Oct 2013
    Posts
    26

    Troublesome query statement

    I have been working on a page to insert a message into a forum, i am taking the user_ID from the user submitting the post from my table "users" and that query is working properly and i am able to get the ID using the current user name, but the same syntax to insert the forum POST variables into the database for the new topics it is giving me an error that just says query wasnt successful and gives no information about the error.

    here is the code
    PHP Code:

    require_once 'header.php';
    // If the form was submitted, scrub the input (server-side validation)
    // see below in the html for the client-side validation using jQuery






    if (isset($_POST['first_name'])
        && isset(
    $_POST['last_name'])
        && isset(
    $_POST['user_email'])
        && isset(
    $_POST['state'])
        && isset(
    $_POST['user_name']) 
        && isset(
    $_POST['user_pass']) 
        && isset(
    $_POST['user_pass_check'])
    ) {
        
    $first_name $db->real_escape_string($_POST['first_name']);
        
    $last_name $db->real_escape_string($_POST['last_name']);
        
    $user_name $db->real_escape_string($_POST['user_name']);
        
    $user_pass $db->real_escape_string($_POST['user_pass']);
        
    $user_pass_check $db->real_escape_string($_POST['user_pass_check']);
        
    $user_email $db->real_escape_string($_POST['user_email']);
        
    $state $db->real_escape_string($_POST['state']);


        
    $query "INSERT INTO users(first_name, last_name, user_email, mailing_address, user_name, user_pass) VALUES('$first_name', '$last_name', '$user_email', '$state', '$user_name', '$user_pass')";

        if (!
    $db->query($query))
            echo 
    "INSERT failed: $query<br />" mysql_error() . "<br /><br />";
        else {
            echo 
    "Thank you for signing up, <a href='index.php'>Click Here</a> to continue to the home page and sign in.";
            
    $_SESSION['user'] = $user_name;
            
    $_SESSION['pass'] = $user_pass;

        }



        
    $errors = array();
     
      
    } else {
       
    //Display the form
    echo <<<_END
    <br>
       <form action="signup.php" method="post" id="register-form" novalidate="novalidate">
       <pre>
       First Name:       <input type="text" name="first_name" />


       Last Name:        <input type="text" name="last_name" />


       Email Address:    <input type="text" name="user_email" />


       State:            <input type="text" name="state" />


       User Name:        <input type="text" name="user_name" />


       Password:         <input type="password" name="user_pass" />


       Confirm Password: <input type="password" name="user_pass_check" />


                         <input type="submit" value="Sign Up" />

       </pre>
      </form>

    _END;


  2. #2
    Join Date
    Oct 2013
    Posts
    26

    That is not the code, This is the code

    PHP Code:
    require_once 'header.php';

    if (!
    $loggedin) die("You must be logged in to view the Forum.");
    $user $_SESSION['user'];


    if (isset(
    $_POST['submit'])
    ) {
        if (!
    $result mysqli_fetch_row($db->query("SELECT * FROM users where user_name='$user'"))) 
        echo 
    "failed";

        

        
    $topic_subject $db->real_escape_string($_POST['topic_subject']);
        
    $topic_date date("Y-m-d H:i:s");
        
        
    $topic_cat $db->real_escape_string($_POST['topic_category']);
        
    $query "SELECT * FROM users where user_name='$user'";
        
        if (!
    $result mysqli_fetch_row($db->query($query))) {
            echo 
    "QUERY failed: $query<br />" mysql_error() . "<br /><br />";
        } else {
            
    $user_id $result[0];
        
        
        }
        
        
    $content $db->real_escape_string($_POST['body']);

        
    $image '';

        
         
    $query "INSERT INTO forum_topics ('topic_subject', 'topic_date', 'topic_cat', 'topic_by', 'content') values ('$topic_subject', '$topic_date', '$topic_cat', '$user_id', '$content')";

        if (!
    $result $db->query($query)) {
            echo 
    "INSERT failed: $query<br />" mysqli_error() . "<br /><br />";
        } else {
            echo 
    "Thank you for sharing, <a href='forum.php'>Click Here</a> to refresh the page.";
        }

    } else {


        echo 
    "<div id='ForumWindow'>";
        echo <<<_END
    <form action="forum.php" method="post">
    <div class="toc">
    <ul class="menu" style="border: 1px solid black;">
        <a><li class="topicHeading">Create New Topic</li></a>
        <ul class="drop">
            <li><input type="text" name="topic_subject" value="Topic Subject" /></li>
            <li class="radio"><input type="radio" name="topic_category" value="Perenials" /></li>Perenials
            <li class="radio"><input type="radio" name="topic_category" value="Vegetables" /></li>Vegetables
            <li class="radio"><input type="radio" name="topic_category" value="Annuals" /></li>Annuals
            <li><input type="textarea" name="body" value="Write a comment here." col="30" row="5" /></li>
            <li><input type="submit" name="submit" value="Post Topic" />
        </ul>
    </div>
    _END;


        echo 
    "</form></ul>";

        echo 
    "</div>";

        }
    require_once 
    'footer.php';
    ?> 

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    750
    ok - which code should we be looking at? And exactly WHICH query is failing you? And what does the call to MySQL/mysqli error give you?

  4. #4
    Join Date
    Oct 2013
    Posts
    26
    your looking at the second one i copied the wrong code the first time and could not figure out how to edit it, the insert statement is the one that is failing and the error is "
    INSERT failed: INSERT INTO forum_topics ('topic_subject', 'topic_date', 'topic_cat', 'topic_by', 'content') values ('Topic Subject', '2014-05-22 16:16:39', 'Annuals', '1', 'Write a comment here.')
    "

  5. #5
    Join Date
    Apr 2009
    Location
    Leeds, England
    Posts
    101
    It's a while since I've used MySQL, and I've used SQL Server a lot since then, so might be getting confused, but I think the problem is the quotes in your field list. You don't need the quotes around the field names ('topic_subject', 'topic_date', 'topic_cat', 'topic_by' and 'content'); you just need them around the values ('Topic Subject', '2014-05-22 16:16:39', 'Annuals', '1' and 'Write a comment here.'). So I think that line should be :

    Code:
    $query = "INSERT INTO forum_topics (topic_subject, topic_date, topic_cat, topic_by, content) values ('$topic_subject', '$topic_date', '$topic_cat', '$user_id', '$content')";

  6. #6
    Join Date
    Oct 2013
    Posts
    26
    Thanks for the reply, i tried that and it still gives me the same error, minus the single quotes in the field names.

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    750
    What goes on in your method "query"? Not getting a message from the mysqli_error call is odd. Unless you do some other mysqli call inside that method.

  8. #8
    Join Date
    Oct 2013
    Posts
    26
    $db->query is not my method its the MySQLi OOP default, i have several insert statements in the project working with the same syntax i'm starting to think it might be an SQL error instead.

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    750
    $db appears to be an object. It appears to be calling a method called query. Show us that method or even the whole class if you have to. If i'm wrong then show me where $db gets defined.

  10. #10
    Join Date
    Oct 2013
    Posts
    26
    i just realized i was forgetting my error reporting the whole time and that is why i wasn't getting a MySQLi error. thanks.

  11. #11
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    750
    I don't think so. If there is an error in a call to mysqli_*, then that function will show it. PHP error reporting is separate if that is what you are referring to.

    I'd really like to see how $db gets created. Have no idea what you mean about "oop default".

  12. #12
    Join Date
    Oct 2013
    Posts
    26
    i figured it out, i had to change the type of one of my fields in the database, accidentally had it set to int instead of varchar.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles