0

Using the MVC Framework, I have made some code that is meant to add information to the Database.

In Index.php (views)

<form method="post" action="<?php echo URL;?>note/updatetopic">
    <select>
    <option value="1">Coasts</option>
    <option value="2">Energy Demand</option>
    </select>
    <input type="submit" name="topic_selection" value="Choose" />
</form>

The Code above is meant to post information about what a user would like to revise.

In note.php (controllers)

public function updatetopic()   {

    if (isset($_POST['topic_selection'])) {
            $note_model = $this->loadModel('Note');
            $note_model->updatetopic($_POST['topic_selection']);
        }
    header('location: ' . URL . 'note');
}

The Code above is meant to get information from the code in index.php and forward it onto the note_model where it runs a function.

In note_model.php (models)

public function updatetopic($topic_selection)
{
$topic_selection = strip_tags($topic_selection);

$sql = "INSERT INTO users (topic_revising) VALUES (:topicselected) WHERE user_id=:user_id";
$query = $this->db->prepare($sql);
$query->execute(array(':topicselected' => $topic_selection, ':user_id' => $_SESSION['user_id']));

$count =  $query->rowCount();
if ($count == 1) {
    return true;
} else {
    $_SESSION["feedback_negative"][] = TOPIC_UPDATE_FAILED;
}
// default return
return false;
}

This is meant to validate the submitted information and insert it into the database but it outputs:

$_SESSION["feedback_negative"][] = TOPIC_UPDATE_FAILED;

Can someone please help me and tell me what im doing wrong? Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

1

You are not choosing any name for your select tag:

try like this:

 <select name="topic">
     <option value="1">Coasts</option>
    <option value="2">Energy Demand</option>
 </select>

And now get the the value of yuor drop down list:

if (isset($_POST['topic'])) {
            $note_model = $this->loadModel('Note');
            $note_model->updatetopic($_POST['topic']);
 // $_POST['topic'] == 1 or 2 depends upon selection
        }

you can change it like:

 <select name="topic">
     <option value="coasts">Coasts</option>
    <option value="Energy_Demand">Energy Demand</option>
 </select>

now u'll get the real value of selected list item and can move further....

Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50
1

Your SQL query will not work as you have tagged mysql which does not support this kind of syntax:

INSERT INTO users (topic_revising) VALUES (:topicselected) WHERE user_id=:user_id

Basically there is no WHERE in MySQL (MySQL Insert Where query)

You should check for duplicated either in select query, or if there are unique keys - use ON DUPLICATE KEY. Also take a look at the linked topic

Community
  • 1
  • 1
Royal Bg
  • 6,988
  • 1
  • 18
  • 24