php and mysql variables - php

i have a web called Infomundo and under the site i have a problem with php:
$c=1;
while($c!=17)
{ $fecha_semana$c=$_POST['fecha_semana$c'];
$interes_semana$c=$_POST['interes_semana$c'];
$capital_semana$c=$_POST['capital_semana$c'];
$recargos_semana$c=$_POST['recargos_semana$c'];
$iva_semana$c=$_POST['iva_semana$c'];
$pagado_semana$c=$_POST['pagado_semana$c'];
$c=$c+1;
}
but the variables $fecha_semana$c, $interes_semana$c, etc. are wrong how can i fix it?

You're using single quotes in the array dereference:
$_POST['fecha_semana$c'];
That will not evaluate the value of $c; use double quotes:
$_POST["fecha_semana$c"];
See also: string
Additionally, you need to use variable variables for the left hand of the assignment:
${"fecha_semana$c"} = $_POST["fecha_semana$c"];
Update
This problem would be easier if you'd use array syntax in your form fields:
<input name="fecha_semana[]" value="123" />
<input name="fecha_semana[]" value="456" />
<input name="fecha_semana[]" value="678" />
When that gets posted, you will have an array in PHP:
print_r($_POST['fecha_semana']);
// ["123", "456", "678"]

As an alternative option to Jack's solution, you can use concatention:
$_POST['fecha_semana'.$c];
I personally prefer concatenation as it is easier for me to see where variables are being used, but I would say it is largely a matter of preference.

Related

inserting array $_POST values to database using php

I have code which takes multiple values on post like`
<form id='students' method='post' class="form-horizontal no-margin" name='students' action='manage_shows.php'>
<input class="input-small" type="text" id='tamil' name='tamil[]' >
<input class="input-small" type="text" id='english' name='english[]' >
and so on..
When I try to insert db shows array values
include('config.php'); if(isset($_POST['submit'])) {
$tamil=$_POST['tamil'];
$english=$_POST['english'];
$sql = "INSERT INTO `shows` ( from , to , seats , cost , screen ) VALUES ('$fname','$lname','$tamil','$english','$computer')";
It inserts only array array into the database. How to add post values to the database ?
Thanks in advance
ameeth
You're using the [] notation on the field names. That tells PHP to treat those fields as multi-valued, and it will create a sub-array for each of them in $_POST. You will have to loop over those arrays and insert each value individually.
Also note that your code is vulnerable to SQL injection attacks.
you can use a foreach loop through the information collected as follows;
foreach ($_POST as $key => $value)
{
echo "$key = $value";
}
#Marc B's answer is correct, bu I'll take the liberty of adding a bit of detail. See, as already stated, the brackets you added to your inputs' names basically indicate that those inputs hold one of multiple values, i.e., that input is inside an array, so PHP is actually expecting an array of values for said post argument, like in this case:
<input class="input-small" type="text" id='tamil_1' name='tamil[]' />
<input class="input-small" type="text" id='tamil_2' name='tamil[]' />
<input class="input-small" type="text" id='tamil_2' name='tamil[]' />
When you send this, values will be parsed and placed in $_POST['tamil'], which will be an array with 3 values, the same happens to you, but you're sending only one value (as you understand, an array with one value, is still an array), so you have a structure that looks like this:
$_POST=array(
'tamil'=>array(...),
'english'=>array(...)
);
As stated by #Marc B, you can iterate through that array like so:
foreach($_POST['tamil'] as $k=>$v) {
//Do something with the value stored in $v or the key in $k
}
Now, you stated in a comment that you're new to PHP, so I'll clarify a bit regarding that behavior in which you insert rows with (array, array), when you try to place a variable inside a string, via concatenation or interpretation, like in your example (variables inside double quotes are interpreted by PHP before adding them to the string, as I suppose you're well aware since you used this syntax), said variables are first parsed into strings. Now, PHP is not the kind of fellow who questions what you're doing, so he will go with the flow most of the time and, instead of throwing an exception, he'll just cast your variable (whatever the type) into a string, how does he cast arrays into strings? by just returning 'array', of course.
So, in your code, PHP casts the arrays as strings and you end up having 'insert into table(col1,col2) values('.'array'.','.'array'.')'.

Simple syntax issue with $_POST

I want to concatenate a variable within the $_POST[] brackets for a while loop. My question is, is it possible. Here is the code.
$m=0;
$_POST['tier_' . $m];
Yes. However your form should probably be more like:
<input type="text" name="tier[]" />
Then $_POST['tier'] will be an array you can loop through.
It is possible, yes, but the code sample you provided would cause a notice because it would try to check an index that does not yet exist. The following would work:
$m=0;
$_POST['tier_' . $m] = 'something';

How do I parse an unknown number of inputs with PHP?

Lets say I have something like this:
http://gyazo.com/642987562175afc6d11a962762327744.png?1329570534
Basically, the user fills out this form, and if there are more people, pressing tab on the last form will add another input. I believe Quizlet does something similar.
How would I parse all these inputs with PHP? I obviously don't know how many inputs there will be.
Cheers
Just create every (similar) element of the form (in html) as an entry of an array (in php). See http://php.net/faq.html.php#faq.html.arrays
You can just iterate over the elements then
foreach ($_POST['MyArray'] as $element) { echo $element; }
Just to make it complete: If you click on the link above then you see, how your form elements should look like.
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
Make sure each input has a unique name by incrementing it for each additional field, eg:
<input type="text" name="field-1" />
<input type="text" name="field-2" />
<input type="text" name="field-3" />
Then when you submit it to your php script you have access to all form fields in the $_GET or $_POST arrays, depending on your form submission method.
You can do a simple while loop to iterate over $_GET['field-1'], $_GET['field-2'], $_GET['field-n'].
Make sense?
Variable-length argument lists
PHP has support for variable-length argument lists in user-defined functions. This is really quite easy, using the func_num_args(), func_get_arg(), and func_get_args() functions.
No special syntax is required, and argument lists may still be explicitly provided with function definitions and will behave as normal.
http://php.net/manual/en/functions.arguments.php :)

how to assign an element to a PHP assositive array?

How to assign an element to a PHP associative array?
For example, I attempt this but not working.
$testSCSI = array('test' => <input type="checkbox"> Test Results</input>);
echo json_encode($testSCSI);
When reading the json from my jquery code, it only gives me value of "Test Results" and not the checkbox.
Is this what you mean? Did you forget the quotes?
$testSCSI = array('test' => '<input type="checkbox"> Test Results</input>');
The problem is likely in your jQuery code, not the PHP.
The PHP should be outputting this:
{"test":"<input type=\"checkbox\"> Test Results</input>"}
And then in jQuery you want to make sure you're using html() rather than text() to assign it.
That really isn't how arrays work, you don't store HTML elements in them you store data elements. An example associative array might look something like:
$a = array("test1"=>"value1", "test2"=>"value2");
You could accomplish what you are trying to do by using properly quoted strings for your HTML elements. You could also use single quotes so you don't have to escape the quotes in type parameter.
$a = array("test"=>"<input type=\"checkbox\" />Test Results\n");
HTH.

Use $_POST values by number

i'm not sure how to explain it correctly, but i would like to use $_POST values like this $_POST[0] and not like this $_POST['form_field_name'].
Let's say i have 3 form fields so if i'd like to get data from post like this:
echo $_POST[0];
echo $_POST[1];
echo $_POST[2];
I hope you guys understand what i wanna do here.
Try it like this:
$values = array_values($_POST);
No idea why you would do such a thing though.
I would not recommend ever to refer to your $_POST values with indexes, as it is generally a bad idea.
You can access them by indexes if you do this:
$items = array_values($_POST);
$foo = $items[0];
$bar = $items[1]
You can also run through your values with a foreach loop, like this (which is better, but still bad!)
foreach($_POST as $item)
{
// do your thing here
}
$_POSTs variables depends on the name attribute of the form element as you can read in this link.
On the other hand the attribute name of the form elements according to W3C always must begin with a letter.
But I think you can prepare the $_POST variable before all your code (at the begining of your php script) with:
$arrPostVariables = array_values($_POST);
And then call them in the way as you want, but I think you will previously must detect the order of the array in order to avoid errors by not having the text of each variable.
This should work:
<input name="0" value="val0" />
<input name="1" value="val1" />

Categories