0

Here is the database

CREATE TABLE IF NOT EXISTS `student` (
  `id` int(11) NOT NULL,
`name` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `student`
ADD PRIMARY KEY (`id`);
ALTER TABLE `student`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Here is the HTML part: jsFiddle

And Here is the PHP part inserting data to mysql:

<?php
$connect = mysql_connect('localhost','root','');
if (!$connect) {
   echo mysql_error();
}
$db = mysql_select_db('demo');
if (!$db) {
  echo mysql_error();
} 
$all_names ="";
if(isset($_POST["mytext"])){   
   foreach($_POST["mytext"] as $key => $text_field){
      $all_names .= $text_field .", ";
 }
}

$result = "INSERT INTO student ( name ) VALUES( $all_names )";
$insert_row  = mysql_query($result);
if(!$insert_row){
   echo $result;
}

Getting Error inserting value to database:

INSERT INTO student ( name ) VALUES( dffd, )
7
  • why Question with jQuery in title is not tagged with jQuery tag? and has no jQuery code included? Commented Oct 14, 2015 at 18:52
  • change echo mysql_error(); to echo $sql; and show us results Commented Oct 14, 2015 at 18:56
  • @GrzegorzAdamKowalski It shows sql Commented Oct 14, 2015 at 18:58
  • @nikolas, of course it shows sql query. This is exactly what I need to see. Commented Oct 14, 2015 at 18:59
  • @nikolas, edit your question and paste this resulting query. Commented Oct 14, 2015 at 19:00

2 Answers 2

2

I don't see whole picture yet. What is the real value returned by $_POST["mytext"]?

But my guess you should at least change your loop to:

foreach($_POST["mytext"] as $key => $text_field){
      if ($all_names == '') {
          $all_names .=  "( '".mysql_real_escape_string($text_field) ."') ";
      } else {
          $all_names .=  ",( '".mysql_real_escape_string($text_field) ."') ";
      }
 }

and your query to:

 $sql = "INSERT INTO student ( name ) VALUES $all_names ";
Sign up to request clarification or add additional context in comments.

3 Comments

Same error: INSERT INTO student ( name ) VALUES ,( 'dffd')
Comma is creating problem.
my bad typo in if ($all_names == '') {
1

Change

   foreach($_POST["mytext"] as $key => $text_field){
      $all_names .= $text_field .", ";
   }

to

$all_names = "'" . implode("'), ('", $_POST["mytext"]) . "'";

6 Comments

If you'd like to sanitize input then you could use array_map and mysql_real_escape_string.
is it like $all_names = "'" . implode("'), ('", mysql_real_escape_string($_POST["mytext"])) . "'";
No, something like $all_names = "'" . implode("'), ('", array_map('mysql_real_escape_string', $_POST["mytext"])) . "'";
Many Many thanks again. Another question: if I have another single value with it. How to save that field.
I do not understand. You want to INSERT not one, but two values into the table? Then probably it would be easier for you to modify @Alex code, but you could also continue using implode with earlier call to array_walk..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.