Jump to content

Insert Multiple Rows


june_c21

Recommended Posts

i trying to insert data into multiple rows. when i run the code i got this error when run this code  " Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\example.php on line 18 "

 

please help. thanks in advance

 

 

 

Html code

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="example.php">
  <label></label>
  <p>
    <label></label>
  Name
  <label></label>
</p>
  <table width="200" border="1">
    <tr>
      <td><input type="text" name="b[]" /></td>
    </tr>
    <tr>
      <td><input type="text" name="b1[]"  /></td>
    </tr>
    <tr>
      <td><input type="text" name="b2[]"  /></td>
    </tr>
    <tr>
      <td><input type="text" name="b3[]"  /></td>
    </tr>
    <tr>
      <td><input type="text" name="b4[]"  /></td>
    </tr>
  </table>
  <p>
    <label></label>
  </p>
  <p>
    <label></label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
</p>
</form>
</body>
</html>

 

php code

<?php

$host = 'localhost';
$user = 'root';
$password = '';
$dbase = 'example';

$dblink = mysql_connect($host,$user,$password);
mysql_select_db($dbase,$dblink);

$b[]    = $_POST['name'];
/*$b1[]    = $_POST['name'];
$b2[]    = $_POST['name'];
$b3[]    = $_POST['name'];
*/

# Create the query
$query = "INSERT INTO report (name) VALUES('$b[]')";

# Add each text box value to the query
foreach($_POST['myTextboxArray'] as $text) {
  $query .= " ('". addslashes($text) ."'),";
}

# Remove the last comma from the query
$query[strlen($query)-1] = "";

?>

Link to comment
Share on other sites

i still facing the same problem

error :

Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\example.php on line 19

 

 


$query= "INSERT INTO report (name),(name),(name),(name),(name) VALUES ('$b[]'),('$b1[]'),('$b2[]'),('$b3[]'),('$b4[]')";

Link to comment
Share on other sites

A few things:

 

1. In your HTML, the names of your inputs are b[], b1[], etc.  Yet, in your PHP, you refer to them all as $_POST['name'].  In short, you're reversing the assignment in the PHP.

 

2. It's clear that you're not comfortable using arrays.  Why attempt sticking the names in separate arrays when you need only one array?

 

3. Again, another array issue.  Arrays have the format of:

<?php
   $array[$key] = $value;
?>

 

It looks like you're sticking the key next to the name, rather than inside the brackets.

 

Try something like...

 

HTML:

<input type="text" name="b[]" /><br />
<input type="text" name="b[]" /><br />
.
.
.

 

PHP:

<?php
   $b = $_POST['b[]'];
?>

 

Inserting the values into the DB depends on how your DB is structured.

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.