Failing SQL insert PHP

Viewed 13

I am creating a social media profile page. when posting a message on someone's wall/profile it is failing to insert the SQL into the database.

The page looks like this enter image description here

My form looks like this

<form method="post" action="comment1.php">
    <textarea rows="2" id="message" name="message" placeholder="write something on their wall"></textarea>

    <div class="attachments">
        <ul>
            <li>
                <button type="submit" onclick="this.value='';">Post</button>
            </li>
            <li>
                <input name="name" type="hidden" id="name" value="<?php echo $_SESSION['SESS_FIRST_NAME'];?>"/>
                <input name="poster" type="hidden" id="name" value="<?php echo $_SESSION['SESS_MEMBER_ID'];?>"/>
                <input name="surname" type="hidden" id="name" value="<?php echo $_SESSION['SESS_LAST_NAME'];?>"/>
                <input name="comment_to_member_id" type="hidden" id="name" value="<?php echo $userid;?>"/>
            </li>
        </ul>
    </div>
</form>

comment1.php

    <?php include('connect.php');
    

    function debug_to_console($data) {
        $output = $data;
        if (is_array($output))
            $output = implode(',', $output);

        echo "" . $output . "";
    }

    function clean($str) {
        $str = @trim($str);

        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }

        $db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
        return mysqli_real_escape_string($db,$str);
    }

    $messages = clean($_POST['message']);
    $poster = clean($_POST['poster']);
    $comment_to_member_id = clean($_POST['comment_to_member_id']);

    $sql="INSERT INTO comment (comment_id, comment, date_created, member_id, comment_to_member_id) VALUES (null,'$messages', '" . strtotime(date("Y-m-d H:i:s")) . "', '$poster', '$comment_to_member_id')";


    $db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

    // Check connection
    if ($db -> connect_errno) {
        echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
        exit();
    }

    // Perform query
    if (!mysqli_query($db,$sql)){
        debug_to_console("failed");
        die('Error: ' . mysql_error());
    }

    header("location: friendprofile.php?action=view&id={$comment_to_member_id}");

    exit();

?>

The debugger function I have logs the query correctly. The failure at the end indicates that it went into my if statement and failed to insert the query.

If I try to insert it manually into the database it works fine. enter image description here

My database table structure

CREATE TABLE IF NOT EXISTS `comment` (
  `comment_id` int(11) NOT NULL AUTO_INCREMENT,
  `comment` text NOT NULL,
  `date_created` varchar(50) NOT NULL,
  `member_id` varchar(30) NOT NULL,
  PRIMARY KEY (`comment_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=174 ;

-- Adding this to track comments posted on each others profiles
ALTER TABLE comment ADD comment_to_member_id varchar(50);
0 Answers
Related