Jump to content

Recommended Posts

I'm trying to insert an array into a database along with other information and finding it very difficult.  I want to insert a row into the database for each container that I get from "insert page 1".

 

I get the amount of containers needed

(insert page 1)

<input type="text" name="number_of_containers" size="2">

 

I use a for loop to make the necessary amount of form fields for two variables and then have two static ones.  What I want to do is enter in one row per container.

 

example for 2 containers

 

(row 1)

container_size, container_weight, container_number #1, seal_number #1

 

(row 2)

container_size, container_weight, container_number #2, seal_number #2

 

(insert page 2)

<?php

<input type="text" name="container_size" size="18">
<input type="text" name="container_weight" size="18">

$number_of_containers=$HTTP_POST_VARS['number_of_containers'];
for($i=0; $i < $number_of_containers; $i++){
echo "
<tr>
    <td>
    Container " .($i+1). "
    </td>
    <td>
    <input type='text' name='container_number[$i]' value='Container #' size='18'>
    </td>
</tr>";}

$number_of_containers=$HTTP_POST_VARS['number_of_containers'];
for($i=0; $i < $number_of_containers; $i++){
echo "
<tr>
    <td>
    Seal " .($i+1). "
    </td>
    <td>
    <input type='text' name='seal_number[$i]' value='Seal #' size='18'>
    </td>
</tr>";}
?>

 

Any help would greatly appreciated, thanx in advance!!!

 

 

Looking at 56.3. How do I create arrays in a HTML <form>? might help you.  You should be using $_POST instead of $HTTP_POST_VARS, too.

 

I don't think that would work because I'm using the for loop to generate the fields I need. 

 

Also I read that using the $HTTP_POST_VARS is better than using $_POST because it works on a broader range of operating systems (namely linux).

 

Thanx, Jim

 

 

From PHP Manual (emphasis added):

 

$_POST

    Variables provided to the script via HTTP POST. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).

 

What specifically, in your script, are you having trouble with?

Here is some sample code that may help (not tested):

 

Form page:

<?php

<input type="text" name="container_size" size="18">
<input type="text" name="container_weight" size="18">

$number_of_containers=$HTTP_POST_VARS['number_of_containers'];
for($i=1; $i <= $number_of_containers; $i++){
 echo "
 <tr>
   <td>Container $i</td>
   <td><input type='text' name='container_number[]' value='Container #' size='18'></td>
 </tr><tr>
   <td>Seal $i</td>
   <td><input type='text' name='seal_number[]' value='Seal #' size='18'></td>
 </tr>";
}

?>

 

 

Processing page:

<?php

$size = $_POST['container_size'];
$weight = $_POST['container_weight'];

if(isset($_POST['container_number']) && isset($_POST['seal_number'])) {


 for ($i=0; $i<count($_POST['container_number']); $i++) {

   if ($_POST['container_number'][$i]!='' && $_POST['seal_number'][$i]!='')

     $values[] = "('$_POST['container_number'][$i]', '$_POST['seal_number'][$i]')";
   }
 }

 $query = "INSERT table (container_size, container_weight, container_number, seal_number) "
 $query .= "(" . implode(',', $values) . ")";

 mysql_query($query);

}


?>

So using $_POST is better than using $HTTP_POST_VARS?  If so then the book I read lied to me :( !

 

What I'm trying to do is insert a row into a database for every container I have but with some static information.

 

Here's the script I'm trying to use to insert the data in the database

 

$query="INSERT INTO " . bookings . " VALUES
    (
    '" . $created . "',
    '" . $created . "',
            '',
            '" . $contact_id . "',
    '" . $company_id . "',
    '" . $booking_number . "',
    '" . $container_number . "',
    '" . $seal_number . "',
    '" . $container_size . "',
    '" . $container_weight . "',
            '" . $chassis_number . "',
    '" . $start_date . "',
    '" . $end_date . "')";
    $result = mysql_query($query) or die(mysql_error());
            mysql_close($connection);
    if ($result){
        echo 'Your information has been successfully entered into the database<br>Click <a href="add_booking_1.php">here</a> to add another company.';
    } else {
        echo '<font color="red">Your information could not be entered into the database</font><br>';}

 

So for every container_number (which I enter in the first page) I want to enter a row into the database.

 

My bookings table

`created` datetime NOT NULL,
`last_edited` timestamp NOT NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
`booking_id` int(11) unsigned NOT NULL auto_increment,
`contact_id` int(3) NOT NULL,
`company_id` int(3) NOT NULL,
`booking_number` varchar(12) collate latin1_general_ci NOT NULL,
`container_number` varchar(12) collate latin1_general_ci NOT NULL,
`seal_number` varchar(12) collate latin1_general_ci NOT NULL,
`container_size` varchar(12) collate latin1_general_ci NOT NULL,
`container_weight` varchar(12) collate latin1_general_ci NOT NULL,
`chassis_number` varchar(12) collate latin1_general_ci NOT NULL,
`start_date` datetime NOT NULL,
`end_date` datetime NOT NULL,
PRIMARY KEY  (`booking_id`)

 

So all entries will be the same except the container_number and the seal_number will be different.

 

Thanx for all the help, I really really appreciate it!!!

There was an error in my processing page script as I did not include the "static" variables into the values. I also added the additional static variables to the query, but I don't know where you are assigning those variables:

 

<?php

$size = $_POST['container_size'];
$weight = $_POST['container_weight'];

if(isset($_POST['container_number']) && isset($_POST['seal_number'])) {


  for ($i=0; $i<count($_POST['container_number']); $i++) {

    if ($_POST['container_number'][$i]!='' && $_POST['seal_number'][$i]!='')

      $values[] = "('$created', '$created', '', '$contact_id', '$company_id', '$booking_number',
                    '$_POST['container_number'][$i]', '$_POST['seal_number'][$i]',
                    '$container_size','$container_weight','$chassis_number','$start_date','$end_date')";
    }
  }

  $query = "INSERT INTO " . bookings . " VALUES "
  $query .= "(" . implode(',', $values) . ")";

  $result = mysql_query($query) or die(mysql_error());
  mysql_close($connection);
  if ($result){
    echo 'Your information has been successfully entered into the database<br>Click <a href="add_booking_1.php">here</a> to add another company.';
  } else {
     echo '<font color="red">Your information could not be entered into the database</font><br>';
  }

}

?>

Thanx so much for your help!!!!!!

 

The code I used is:

 

<?php
$connection=mysql_connect("$hostname", "$username", "$password") or die("Could not establish connection");
mysql_select_db($database_name, $connection) or die ("Could not select database");

$created=date('Y:m:d H:i');
$contact_id=$_POST['contact_id'];
$company_id=$_POST['company_id'];
$booking_number=$_POST['booking_number'];
$container_number=$_POST['container_number'];
$seal_number=$_POST['seal_number'];
$container_size=$_POST['container_size'];
$container_weight=$_POST['container_weight'];
$chassis_number=$_POST['chassis_number'];
$start_date=$_POST['start_date'];
$end_date=$_POST['end_date'];

if(isset($_POST['container_number']) && isset($_POST['seal_number'])) {
    for ($i=0; $i<count($_POST['container_number']); $i++) {
        if ($_POST['container_number'][$i]!='' && $_POST['seal_number'][$i]!='')     
$values[]="('$created','$created','','$contact_id','$company_id','$booking_number',
'$_POST['container_number'][$i]','$_POST['seal_number'][$i]',            // line 57
'$container_size','$container_weight','$chassis_number','$start_date','$end_date')";
     }
}
$query = "INSERT INTO " . bookings . " VALUES "
$query .= "(" . implode(',', $values) . ")";           // line 62
$result = mysql_query($query) or die(mysql_error());
mysql_close($connection);
if ($result){
    echo 'Your information has been successfully entered into the database<br>Click <ahref="add_booking_1.php">here</a> to add another company.';
} else {
    echo '<font color="red">Your information could not be entered into the database</font><br>';
}
}
?>

 

Once I ran the code I got the error:

 

"Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\xampp\www\truck\admin\add_booking_3.php on line 57"

 

If I take out the single quotes from '$_POST['container_number'][$i]','$_POST['seal_number']' from inside the array like this '$_POST[container_number][$i]','$_POST[seal_number]'  , it will skip that error on go on to say:

 

"Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\xampp\www\truck\admin\add_booking_3.php on line 62"

 

 

So any further help would be greatly appreciated, thanx for the help already!!!

 

Jim

If you read my signature you will see that I do not always test my code and there may be syntax errors. Especially in a situation as this wher it would take quite a bit of setup to properly test.

 

You were right to remove those single ticks OR you could have appended the variables outside the double quote. But, the second error is on line 61(where there is no ending semicolon):

 

$query = "INSERT INTO " . bookings . " VALUES "

 

Total code with corrections:

<?php
$connection=mysql_connect("$hostname", "$username", "$password") or die("Could not establish connection");
mysql_select_db($database_name, $connection) or die ("Could not select database");

$created=date('Y:m:d H:i');
$contact_id=$_POST['contact_id'];
$company_id=$_POST['company_id'];
$booking_number=$_POST['booking_number'];
$container_number=$_POST['container_number'];
$seal_number=$_POST['seal_number'];
$container_size=$_POST['container_size'];
$container_weight=$_POST['container_weight'];
$chassis_number=$_POST['chassis_number'];
$start_date=$_POST['start_date'];
$end_date=$_POST['end_date'];

if(isset($_POST['container_number']) && isset($_POST['seal_number'])) {
    for ($i=0; $i<count($_POST['container_number']); $i++) {
        if ($_POST['container_number'][$i]!='' && $_POST['seal_number'][$i]!='')     
$values[]="('$created','$created','','$contact_id','$company_id','$booking_number',
'".$_POST['container_number'][$i]."','".$_POST['seal_number'][$i]."',
'$container_size','$container_weight','$chassis_number','$start_date','$end_date')";
     }
}
$query = "INSERT INTO " . bookings . " VALUES ";
$query .= "(" . implode(',', $values) . ")";
$result = mysql_query($query) or die(mysql_error());
mysql_close($connection);
if ($result){
    echo 'Your information has been successfully entered into the database<br>Click <ahref="add_booking_1.php">here</a> to add another company.';
} else {
    echo '<font color="red">Your information could not be entered into the database</font><br>';
}
}
?>

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.