june_c21 Posted April 8, 2008 Share Posted April 8, 2008 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] = ""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/100075-insert-multiple-rows/ Share on other sites More sharing options...
ohdang888 Posted April 8, 2008 Share Posted April 8, 2008 $query = "INSERT INTO report (name), (name) VALUES('$b[]'),('$b[]')"; Quote Link to comment https://forums.phpfreaks.com/topic/100075-insert-multiple-rows/#findComment-511724 Share on other sites More sharing options...
june_c21 Posted April 8, 2008 Author Share Posted April 8, 2008 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[]')"; Quote Link to comment https://forums.phpfreaks.com/topic/100075-insert-multiple-rows/#findComment-511961 Share on other sites More sharing options...
KevinM1 Posted April 8, 2008 Share Posted April 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/100075-insert-multiple-rows/#findComment-511973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.