homepage Welcome to WebmasterWorld Guest from 54.226.0.225
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
List items are not populated in database
mvaz




msg:4626680
 10:10 am on Nov 29, 2013 (gmt 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

 

JD_Toims




msg:4626812
 8:51 pm on Nov 29, 2013 (gmt 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.

penders




msg:4626886
 1:01 pm on Nov 30, 2013 (gmt 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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved