Jump to content

looking for help please


kcm2611

Recommended Posts

I am trying to write a script to add multiple lines into the database with one submit, this is my effort - can anyone suggest anything better that works

 

Many Thanks

 

<?php

 

error_reporting (E_ALL);

include "include/z_db.php";

 

//

$query = "INSERT INTO table(name,surname,age)VALUES";

 

foreach ($_POST["name"] as $key => $value) {

$query = "('$key','$_POST[surname][$key]','$_POST[age][$key]')";

$result = mysql_query($query)or die(mysql_error());

 

if (isset($_POST['addto'])) {

$name = $_POST['name'];

$surname = $_POST['surname'];

$age = $_POST['age'];

if ($age == '0' && $surname == '0') {

echo "0";

} else {

$queries = array();

 

for ($i = 0; $i < count($name); $i++) {

if (!get_magic_quotes_gpc()) {

$name[$i] = addslashes($name[$i]);

$surname[$i] = addslashes($surname[$i]);

$age[$i] = addslashes($age[$i]);

# etc

}

$queries[] = "('$userid ','$name[$i]','$surname[$i]','$age[$i]')";

}

 

if (count($queries) == 0) {

# Nothing passed

# exit

}

$piece = implode(", ", $queries);

$query = "INSERT INTO 'TABLE' (id,name,surname,age) VALUES $piece";

$result = mysql_query($query) or die(mysql_error());

}

}

 

?>

 

 

<form action='result.php'method="POST">

<table>

<tr>

<td>name<input type="text" name="name[]"></td>

<td>surname<input type="text" name="qty[]"></td>

<td>age<input type="text" name="qty2[]"></td>

</tr>

<tr>

<td>name<input type="text" name="name[]"></td>

<td>surname<input type="text" name="qty[]"></td>

<td>age<input type="text" name="qty2[]"></td>

</tr>

<tr>

<td>name<input type="text" name="name[]"></td>

<td>surname<input type="text" name="qty[]"></td>

<td>age<input type="text" name="qty2[]"></td>

</tr>

<tr>

<td>name<input type="text" name="name[]"></td>

<td>surname<input type="text" name="qty[]"></td>

<td>age<input type="text" name="qty2[]"></td>

</tr>

<tr>

<td>mane<input type="text" name="name[]"></td>

<td>surname<input type="text" name="qty[]"></td>

<td>age<input type="text" name="qty2[]"></td>

</tr></table>

 

<td><input type="submit" name="submit" value="add">

 

Link to comment
Share on other sites

I'd name the form fields like so

<form action='result.php'method="POST">
<table>
<tr>
<td>name<input type="text" name="person[0][name]"></td>
<td>surname<input type="text" name="person[0][surname]"></td>
<td>age<input type="text" name="person[0][age]"></td>
</tr>
<tr>
<td>name<input type="text" name="person[1][name]"></td>
<td>surname<input type="text" name="person[1][surname]"></td>
<td>age<input type="text" name="person[1][age]"></td>
</tr>
</table>

<input type="submit" name="submit" value="add">
</form>

 

Now all data from the form will be grouped together in the $_POST['person'] array. Now you can easily loop through the data

 

if(isset($_POST['submit']))
{
    foreach($_POST['person'] as $person)
    {
        $name    = mysql_real_escape_string($person['name']);
        $surname = mysql_real_escape_string($person['surname']);
        $age     = (int) $person['age'];
        
        $values[] = "('$name', '$surname', $age)";
    }
    
    $query = "INSERT INTO table(name,surname,age) VALUES " . implode(', ', $values);
    $result = mysql_query($query);
}

Link to comment
Share on other sites

Thanks Wildteen88 for your help, I am not sure if I have still got something wrong but still doesnt work any suggestions

 

thanks

 

 

<?php

 

error_reporting (E_ALL);

include "include/z_db.php";

include "include/session.php";

 

 

 

if(isset($_POST['submit']))

{    foreach($_POST['person'] as $person)   

{        $name    = mysql_real_escape_string($person['name']);   

    $surname = mysql_real_escape_string($person['surname']);     

$age    = (int) $person['age'];               

$values[] = "('$name', '$surname', $age)";    }     

      $query = "INSERT INTO plus_signup (name,surname,age) VALUES "    . implode(', ', $values);

  $result = mysql_query($query);

  }

 

 

?>  

 

 

<form method="POST" action='multi.php' name="multi">

<table>

<tr>

<td>name<input type="text" name="prrson[0][name]"></td>

<td>surname<input type="text" name="person[0][surname]"></td>

<td>age<input type="text" name="person[0][age]"></td>

</tr>

<tr>

<td>name<input type="text" name="person [1][name]"></td>

<td>surname<input type="text" name="person[1][surname]"></td>

<td>age<input type="text" name="person[1][age]"></td>

</tr>

<tr>

<td>name<input type="text" name="person [2][name]"></td>

<td>surname<input type="text" name="person[2][surname]"></td>

<td>age<input type="text" name="person[2][age]"></td>

</tr>

</table>

 

<td><input type="submit" name="submit" value="add">

</form>

 

 

Link to comment
Share on other sites

Umm, Maybe there is an error

$result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error());

 

 

EDIT: Just noticed this. Your form fields are mistyped, prrson[0] should be person[0]. Remove the space for person [1] and person [2]. PHP will treat those three names differently.

Link to comment
Share on other sites

getting closer I feel

 

I now get this error when I fill all fields in

 

 

Error!

Query: INSERT INTO plus_signup (name,surname,age) VALUES ('n0', 's0', a0), ('n1', 's1', a1), ('n2', 's2', a2)

Error: Unknown column 'a0' in 'field list'

 

 

I have a field 'age'

 

 

<?php

 

error_reporting (E_ALL);

include "include/z_db.php";

include "include/session.php";

 

 

 

if(isset($_POST['submit']))

{    foreach($_POST['person'] as $person)   

{        $name= mysql_real_escape_string($person['name']);   

        $surname= mysql_real_escape_string($person['surname']);     

    $age= mysql_real_escape_string($person['age']);               

      $values[] = "('$name', '$surname', $age)";    }     

      $query = "INSERT INTO plus_signup (name,surname,age) VALUES "    . implode(', ', $values);

  $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error());

  }

 

 

?>  

 

 

<form method="POST" action='multi.php' name="multi">

<table>

<tr>

<td>name<input type="text" name="person[0][name]"></td>

<td>surname<input type="text" name="person[0][surname]"></td>

<td>age<input type="text" name="person[0][age]"></td>

</tr>

<tr>

<td>name<input type="text" name="person[1][name]"></td>

<td>surname<input type="text" name="person[1][surname]"></td>

<td>age<input type="text" name="person[1][age]"></td>

</tr>

<tr>

<td>name<input type="text" name="person[2][name]"></td>

<td>surname<input type="text" name="person[2][surname]"></td>

<td>age<input type="text" name="person[2][age]"></td>

</tr>

</table>

 

<td><input type="submit" name="submit" value="add">

</form>

 

 

Link to comment
Share on other sites

hopefully this will be better to read now, have added those quotes & now get this error

 

Error!

Query: INSERT INTO plus_signup (name,surname,age) VALUES ('n0', 's0', 'a0'), ('n1', 's1', 'a1'), ('n2', 's2', 'a2')

Error: Duplicate entry '' for key 1

 

 

<?php

error_reporting (E_ALL); 
include "include/z_db.php";
include "include/session.php";



if(isset($_POST['submit']))
{    foreach($_POST['person'] as $person)    
{        $name= mysql_real_escape_string($person['name']);    
         $surname= mysql_real_escape_string($person['surname']);       
     $age= mysql_real_escape_string($person['age']);                
      $values[] = "('$name', '$surname', '$age')";    }       
      $query = "INSERT INTO plus_signup (name,surname,age) VALUES "    . implode(', ', $values); 
	  $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); 
	  }
  

?>	   


<form method="POST" action='multi.php' name="multi">
<table>
<tr>
<td>name<input type="text" name="person[0][name]"></td>
<td>surname<input type="text" name="person[0][surname]"></td>
<td>age<input type="text" name="person[0][age]"></td>
</tr>
<tr>
<td>name<input type="text" name="person[1][name]"></td>
<td>surname<input type="text" name="person[1][surname]"></td>
<td>age<input type="text" name="person[1][age]"></td>
</tr>
<tr>
<td>name<input type="text" name="person[2][name]"></td>
<td>surname<input type="text" name="person[2][surname]"></td>
<td>age<input type="text" name="person[2][age]"></td>
</tr>
</table>

<td><input type="submit" name="submit" value="add">
</form>



Link to comment
Share on other sites

Error!

Query: INSERT INTO plus_signup (name,surname,age) VALUES ('n0', 's0', 'a0'), ('n1', 's1', 'a1'), ('n2', 's2', 'a2')

Error: Duplicate entry '' for key 1

 

This error is generated from the database.  Your query is good, only you have one of those columns named a primary, or unique key.  This means you cannot put duplicate values in that column.  Your script is good.  Post your database structure, and we can help.

 

Link to comment
Share on other sites

You'll have to perform some validation within the foreach loop, eg

if(isset($_POST['submit']))
{
    $values = array();
    foreach($_POST['person'] as $person)    
    {
        // performing some basic validation
        // checking to see if name and surname is not empty
        // and that age is a number
        if(!empty($_POST['name']) && !empty($_POST['surname']) && is_numeric($_POST['age']))
        {
            $name    = mysql_real_escape_string($person['name']);    
            $surname = mysql_real_escape_string($person['surname']);       
            $age     = mysql_real_escape_string($person['age']);                
    
            $values[] = "('$name', '$surname', '$age')";
        }
    }       

    if(count($values) > 0)
    { 
        $query = "INSERT INTO plus_signup (name,surname,age) VALUES "    . implode(', ', $values); 
        $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); 
    }
}

Link to comment
Share on other sites

Thanks again for all your help

How can I now add the userid='$_SESSION[userid] to the table that i am using for my multiple line form, from the table that the user logs onto . i.e the person loged in then populates the multiple form & they all have the loged in users id in there table

 

I have tried combining the multiple line form table with the login table but I cant seem to get it to work, but if i have 2 different tables it works ok

 

ie inserting 'plus_signup' instead of 'plus_user'

 

$query = "INSERT INTO plus_user (name,surname,age) VALUES "    . implode(', ', $values);

 

<?

include "include/session.php";

include "include/z_db.php";

// check the login details of the user and stop execution if not logged in
require "check.php";

// If member  has logged in then below script will be execuated. 
// let us collect all data of the member 
$row=mysql_fetch_object(mysql_query("select * from plus_signup where userid='$_SESSION[userid]'"));



if(isset($_POST['submit']))
{    foreach($_POST['person'] as $person)  

{        
        $name= mysql_real_escape_string($person['name']);    
         $surname= mysql_real_escape_string($person['surname']);       
     $age= mysql_real_escape_string($person['age']);                
      $values[] = "($name', '$surname', '$age')";    }       
      $query = "INSERT INTO plus_user (name,surname,age) VALUES "    . implode(', ', $values); 
	  $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); 
	  
	  
	  }
  


?>	   


<form method="POST" action='multi.php' name="">
<table>
<tr>
<td>Name<input type="text" name="person[0][name]"></td>
<td>surname<input type="text" name="person[0][surname]"></td>
<td>age<input type="text" name="person[0][age]"></td>
</tr>
<tr>
<td>name<input type="text" name="person[1][name]"></td>
<td>surname<input type="text" name="person[1][surname]"></td>
<td>age<input type="text" name="person[1][age]"></td>
</tr>
<tr>
<td>name<input type="text" name="person[2][name]"></td>
<td>surname<input type="text" name="person[2][surname]"></td>
<td>age<input type="text" name="person[2][age]"></td>
</tr>
</table>

<td><p>
  <input type="submit" name="submit" value="add">
</p>
  
  <p> </p>|   <a href=update-profile.php>back</a>|
</form>



<?

require "bottom.php";


?>
<center>
<br>|   <a href=update-profile.php>back</a>|<br></center> 

</body>

</html>




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.