Jump to content

[SOLVED] PHP multi-dimension disaster topped with mysql dilemmas


spitfire1945

Recommended Posts

Fear not, the problem is not as dramatic as the title suggests

 

I have a few input textfields in a form. I am just having a bit of a coder block as to how to get these variables into the database.

 

Ill start with the variables:

 

$_POST['a_sf']

$_POST['a_be']

$_POST['a_bb']

 

$_POST['b_sf']

$_POST['b_be']

$_POST['b_bb']

 

$_POST['c_sf']

$_POST['c_be']

$_POST['c_bb']

 

and I have a db table called table with columns id, sf, be, and bb

 

I want the table to store data like this

 

+---+------+-----+-----+----+

| id  | type  |  sf  |  be  | bb  |

+---+------+-----+-----+----+

| 1  |  a    | 12  |  78  |  54 |

+---+------+-----+-----+----+

| 2  |  b    | 98  |  45  |  37 |

+---+------+-----+-----+----+

 

so on and so forth..

 

any coding ideas as to how I can achieve this? I have tried several for loops and foreach loops et al but since the post variables are alphabets and not numerical its a little bit of a pain in the bum. or maybe a better way to setup the variable names for the text fields perhaps?

Link to comment
Share on other sites

I'm not sure what you're asking?  Since you only have 9 elements, why not just insert them using mysql?

<?php
  $a_sf = intval($_POST['a_sf']);
  $a_be = intval($_POST['a_be');
//etc..
  $b_sf = intval($_POST['b_sf']);
//etc..

$sql = "INSERT INTO yourTable (id,type,sf,be,bb) VALUES 
('','a','$a_sf','$a_be','$a_bb'),
('','b','$b_sf','$b_be','$b_bb'),
('','c','$c_sf','$c_be','$c_bb')";
?>

 

However, if you're saying you have more than this..  Perhaps a foreach($_POST as $key => $val)

..splitting the key name on "_" and maybe putting them into an array based on $splitresult[0]... depends..

Link to comment
Share on other sites

or maybe a better way to setup the variable names for the text fields perhaps?

 

Yup. Use arrays for the names of the text fields. I'd go with two dimensions like this:

 

<input type="text" name="data[a][sf]" /><br />
<input type="text" name="data[a][be]" /><br />
<input type="text" name="data[a][bb]" /><br />

<input type="text" name="data[b][sf]" /><br />
<input type="text" name="data[b][be]" /><br />
<input type="text" name="data[b][bb]" /><br />

//etc

 

Then use PHP like this:

 

$temp = array();
$temp2 = array();
foreach($_POST['data'] as $type => $stats){
    $temp2[] = "('".mysql_real_escape_string($type)."'";
    foreach($stats as $stat){
        $temp2[] = "'".mysql_real_escape_string($stat)."'";
    }
    $temp[] = join(',',$temp2).')';
    $temp2 = array();
}
$sql = join(',',$temp);
$sql = "INSERT INTO yourtable SET (type,sf,be,bb) VALUES $sql";
mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.