Forum Moderators: coopster

Message Too Old, No Replies

Select option in php

         

JuicyScript

7:29 am on Feb 4, 2011 (gmt 0)

10+ Year Member



hello
Am having this issue that i hope someone can solve for me
The issue is i have select field.
When you select english/french/social std and select the grade option
the grade value goes in the english/french/social field in the database field
<select >
<option value="ENGLISH">A</option>
<option value="FRENCH">A1</option>
<option value="SOCIAL STUDIES">B</option>
<option value="MATHS">B2</option>
<option value="GEOGRAPHY">B3</option>
<option value="HISTORY">C</option>
</select>

<select name=grade >
<option value="A">A</option>
<option value="A1">A1</option>
<option value="B">B</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="C">C</option>
<option value="C4">C4</option>
<option value="C5">C5</option>
<option value="C6">C6</option>
<option value="D">D</option>
<option value="D7">D7</option>
<option value="E">E</option>
<option value="E8">E8</option>
<option value="F">F</option>
<option value="F9">F9</option>
</select>

jecasc

7:39 am on Feb 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shouldn't the first select field not also have a "name" tag?

Check the PHP source code what field name your php script is expecting and then rename your select field. Perhaps you could post the relevant PHP code also.

Matthew1980

9:35 am on Feb 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>Shouldn't the first select field not also have a "name" tag?

Yes. Checking the $_POST array after submission would have brought your attention to that.

Cheers,
MRb

JuicyScript

11:38 am on Feb 4, 2011 (gmt 0)

10+ Year Member



That was just an example.
What i want is to set a variable which will check which field it is supposed to send the value to.

Matthew1980

12:31 pm on Feb 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>What i want is to set a variable which will check which field it is supposed to send the value to.

I'm confused enough today by laws of physics defying electronics, could you clarify your issue a little bit, or am I missing a point somewhere...

Cheers,
MRb

rocknbil

5:45 pm on Feb 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm confused enough today by laws of physics defying electronics


You dropped your iPod and can't figure out why it's not working any more? :-P

---------------

What is the task?

If you're trying to associate the class codes with their plain names, wouldn't this work?

<option value="P">PHP 101</option>
<option value="A">ENGLISH</option>
<option value="A1"> FRENCH</option>

id|student_id|fname|lname|...(etc.)
1|1234|Joe|Schmoe| ....

id|class_code|class_name|student_id|semester|class_grade
1|P|PHP 101|1234|1|F
1|A|ENGLISH|1234|1|D

JuicyScript

9:27 pm on Feb 4, 2011 (gmt 0)

10+ Year Member



<option value="P">PHP 101</option>
<option value="A">ENGLISH</option>
<option value="A1"> FRENCH</option>

id|student_id|fname|lname|...(etc.)
1|1234|Joe|Schmoe| ....

id|class_code|class_name|student_id|semester|class_grade
1|P|PHP 101|1234|1|F
1|A|ENGLISH|1234|1|D

Hello rocknbil and Matthew1980 is it possible to to have this

$basic = addslashes($_POST['basic']);
$school = addslashes($_POST['school']);

var = $variable;
comment: where $variable can be $basic or $school or any value posted by the form.

$sqlQuery = "UPDATE members SET basic='$variable',school='$variable'
WHERE member_id = ".$_SESSION['SESS_MEMBER_ID'];
$result = @MYSQL_QUERY($sqlQuery);

Matthew1980

10:24 pm on Feb 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there JuicyScript,

Firstly: Rocknbil >>You dropped your iPod and can't figure out why it's not working any more? :-P

iPod? I have a radio & stereo as you are aware from a previous thread, can't beat a vinyl for quality.

I digress.

Your issue:-

$sqlQuery = "UPDATE `members` SET `basic` = '".$variable."', `school` = '".$variable."' WHERE `member_id` = ".$_SESSION['SESS_MEMBER_ID'];
$result = mysql_query($sqlQuery) or die(mysql_error());

That's tidied up your query, why people still put the error suppressor (@) in front of the query is beyond me. Php.net only advocate this for a couple of functions, off hand I can't think of them, but I think they are XML functions.. When developing you NEED to see anything that is giving an error - it really helps you out.

WRT your addslashes() that's fine, but you should run them through mysql_real_escape_string() as this is essentially the same thing (uses slashes to escape apostrophise)

Have a play, see what you come up with, feel free to post back, and we can advise from there.

Have fun,

Cheers,
MRb

JuicyScript

12:17 pm on Feb 5, 2011 (gmt 0)

10+ Year Member



Thanks Matthew1980

$sqlQuery = "UPDATE `members` SET `basic` = '".$variable."', `school` = '".$variable."' WHERE `member_id` = ".$_SESSION['SESS_MEMBER_ID'];
$result = mysql_query($sqlQuery) or die(mysql_error());

YOU CORRECTED MY SCRIPT.BUT WHAT I REALLY WANT TO KNOW IS THAT HOW WILL I USE ONE VARIABLE TO DETERMINE WHERE THE APPROPRIATE VALUE SHD GO IN THE DATABASE FIELD.

rocknbil

6:01 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OIC. So you have a single value, and it will go in one or the other depending on what's selected. Presuming this is a radio button, or a single option select? If it's "basic" you want to enter into basic, if it's "school" you want to enter into school, is that right?

$update = null;

if (isset($_POST['basic']) or isset($_POST['school'])) {
$update = (isset($_POST['basic']))?" basic='" . stripslashes($_POST['basic']) . "'":" school='" . stripslashes($_POST['school']) . "'";
}

if ($update) {
// otherwise there would be no need to execute
$sqlQuery = "UPDATE members SET $update WHERE member_id = ".$_SESSION['SESS_MEMBER_ID'];
mysql_query($sqlQuery);
}

Like that?