Welcome to WebmasterWorld Guest from 54.227.72.69

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

List items are not populated in database

     
10:10 am on Nov 29, 2013 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 0


Guys, here is my form from where I need to identify the option value for DB input.

<form name="list_input" method="post" action="">
<select name="report_category">
<option class="report_category" value="1">1 - A</option>
<option class="report_category" value="2">2 - B</option>
<option class="report_category" value="3">3 - C</option>
<option class="report_category" value="4">4 - D</option>
<option class="report_category" value="5">5 - E</option>
</select>
<br />
<input type="submit" name="send" value="submit" />
</form>

Now here is the php code that should pick values between 1 and 5, depending on the user choice and insert this value in the db as an integer.

if(isset($_POST['send'])) {
$cat_id = $_POST['report_category'];
include_once ('db_conx.php'); (if not connected, an error is shown saying connection failed).

$query = "INSERT INTO `list_inpurt` (cat_id) VALUES($cat_id)";

I expect the above script to get values between 1 and 5 in to the table, but it just doesn't do this.

Any ideas where I have gone wrong? Is it my html or the php code that is incorrect?

Please could you review the codes and advise me.

Regards


Melwyn
8:51 pm on Nov 29, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:July 19, 2013
posts:1097
votes: 0


# First, always best to check and make sure you're getting what you should
# prior to "doing anything" with it, so let's check and make sure report_category
# is numeric.

if(isset($_POST['send'] && is_numeric($_POST['report_category'])) {

# Second, "external variables", such as POST, GET, COOKIE are "type cast" as strings,
# not integers, even if they're a number, so let's make sure we cast the number in
# report_category as an integer.
$cat_id = (int) $_POST['report_category'];

include_once ('db_conx.php');

# Third, the ` [back tick != single quote] are not only unnecessary around the
# table name, but ` [back tick] in PHP is an execution operator for shell commands,
# so avoiding them, unless there's a specific reason not to, is usually best -- [php.net...]
$query = "INSERT INTO list_inpurt (cat_id) VALUES($cat_id)";

# For testing, let's see wtf is going on if there's an error.
mysql_query($query) or die(mysql_error());

# Make sure to remove the preceding or edit in a way as to not show errors on the page for
# any live version of code.
1:01 pm on Nov 30, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Just to confirm,
$_POST['report_category']
should hold the value "1", "2", "3", etc. as you suggest. This is obviously something to check - by echo'ing to the page (or something) in order to debug.

INSERT INTO `list_inpurt` (cat_id) VALUES($cat_id)


Is this a typo, coding error, or by design?! Your form is called "list_input".

Also, I'm rather curious as to your table structure. You seem to be inserting a single non-unique value. Even with an auto incrementing primary key I'm struggling to see its usefulness, or even how this works?
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members