0

I'm submitting a form and inserting the valuse into my mysql db. One of the form elements it a select with option values like 11#120#12 (id#cost#months).

I perform the following:

$plan = explode("#", $_POST['symb']);
$plan = $plan[0];
$cost = $plan[1];

and then submit to my db

echo $insertSQL = sprintf("INSERT INTO users (username, plan, cost) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['username'], "text"),
                       GetSQLValueString($plan, "text"),
                       GetSQLValueString($cost, "int"));

The problem is that $cost is getting the wrong value, instead of 120 it's getting 1 as a value. Where's the error? (the posted sql statement is only part of the actual query, for demonstration purposes only)

1
  • 10
    You are overwriting $plan in line 2 Commented May 29, 2012 at 9:31

3 Answers 3

4

Don't overwrite $plan with a string value before you try to extract the $cost from the array you originally stored in it.

Either change the first $plan in $plan = $plan[0]; (and change every reference to it later in the script) or move that line so it appears after $cost = $plan[1];.

(I'd recommend the former option for clarity).`

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! I guess I should take a break for not seeing the absolutely obvious... thanks for pointing it out for me.
0
$plan = explode('#',$_POST['symb']);
$p = $plan[0];
$cost = $plan[1];

Comments

0

On line 2, $plan is assigned "11" (which was $plan[0]), and on line 3, $plan[1] references the second character in the string which is '1'.

The comments above already have already told you how to fix it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.