1

enter image description herei have store label values in array, pass this array using ajax to php. now i want to store each index value in different variables the store these value under one field of mysql table

function rs() {
  var arr = [];
  $('.cat').each(function() {

    arr.push($(this).text());
  });
  $.ajax({
    url: 'insert.php',
    data: {
      array: arr
    },
    type: 'POST',
    success: function() {
      alert("data has been sent");
      document.getElementById('exampleModal1').style.display = "none";
    }

  });
}

php file :

<?php

$con = mysqli_connect("localhost", "root", "", "test");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ".mysqli_connect_error();
}

// escape variables for security
//home tab
$cat1 = mysqli_real_escape_string($con, $_POST['array[0]']);

$cat2 = mysqli_real_escape_string($con, $_POST['array[1]']);

$cat3 = mysqli_real_escape_string($con, $_POST['array[2]']);

$cat4 = mysqli_real_escape_string($con, $_POST['array[3]']);

$cat5 = mysqli_real_escape_string($con, $_POST['array[4]']);

$sql1 = "INSERT INTO rs (Category)
VALUES('".$cat1."'), ('".$cat2."'), ('".$cat3."'), ('".$cat4."'), ('".$cat5."')
";

if (!mysqli_query($con, $sql1)) {
  die('Error: '.mysqli_error($con));
}

echo "1 record added";

mysqli_close($con);
?>

but the problem is that , the said is not passed. table shows blank fields. i do not know what i am doing wrong. i did not find any solution yet.

Sarah_Salar
  • 183
  • 2
  • 13
  • Are you getting any result for these `$cat1` variables? – SilentCoder Feb 19 '18 at 07:33
  • just store these variables in mysql and later retrive to show the entry – Sarah_Salar Feb 19 '18 at 07:35
  • Im not much expert in php. I just want know is, whether you are getting those posted values to you php file or not? – SilentCoder Feb 19 '18 at 07:37
  • seems this line missing two parentheses `INSERT INTO rs (Category) VALUES ('".$cat1."'), ('".$cat2."'), ('".$cat3."'), ('".$cat4."'),('".$cat5."')` – SilentCoder Feb 19 '18 at 07:38
  • Correct format is `INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ` – SilentCoder Feb 19 '18 at 07:39
  • 1
    format is correct as i want to store as individual entry – Sarah_Salar Feb 19 '18 at 07:49
  • The table has only 1 column ? – Daniel E. Feb 19 '18 at 08:02
  • table has two fields after execution result is like shown in figure above – Sarah_Salar Feb 19 '18 at 08:08
  • I thik you cant call `array[0]` in `$_POST`, can you? https://stackoverflow.com/questions/11676011/post-array-from-html-form Can you do a `echo $_POST['array[1]']`? – Nomeaning25 Feb 19 '18 at 08:29
  • then how to store array values under the same field individually? – Sarah_Salar Feb 19 '18 at 09:03
  • 1
    Try looping through the data in the PHP (foreach loop), this will also cut down the amount of code and allow for more than 5 entries, which then could be reused. So, for example `foreach($_POST as $key => $value)` and use `$value` as each category. It might be worth checking you have data there too. – Michael Emerson Feb 19 '18 at 09:19
  • Plus, put a space between `VALUES` and the first open parentheses. Also, surely it would be adding 5 records, not 1? – Michael Emerson Feb 19 '18 at 09:22
  • foreach($_POST['array'] as $key => $value) { $sql1="INSERT INTO rs (Category) VALUES ('".$value."')"; } like this??? – Sarah_Salar Feb 19 '18 at 09:31
  • it has the same result – Sarah_Salar Feb 19 '18 at 09:32
  • Do `print_r($_POST)` to see what is actually in the value of the post. Also, it may be advisable to avoid the word 'array' since it's reserved. Perhaps use `categories: arr` – Michael Emerson Feb 19 '18 at 09:39
  • Go to this codepen: https://codepen.io/anon/pen/jZZLja I formatted the ajax call in a more succinct and correct way. – Michael Emerson Feb 19 '18 at 09:46
  • Check this page: https://api.jquery.com/jquery.post/ and look at the examples, specifically the one about passing arrays. – Michael Emerson Feb 19 '18 at 09:49
  • foreach($_POST['categories'] as $key => $value) { $sql1="INSERT INTO rs (Category) VALUES ('".$value."')"; //echo "$value
    "; } this add value but only last value
    – Sarah_Salar Feb 19 '18 at 09:57
  • That's because you're overwriting each sql statement. Don't forget to use `.=` for every additional statement. – Michael Emerson Feb 19 '18 at 10:00
  • sorry, did not get it – Sarah_Salar Feb 19 '18 at 10:15
  • By writing `$sql = ""` you are overwriting each statement you write, so you will only ever get the last one in the variable `$sql`. So, you either run each one separately in the loop or use `$sql .= ""` for every additional statement which means "add this to the variable" instead of "overwrite this variable". It's the same as using `+=` in javascript. – Michael Emerson Feb 19 '18 at 10:27
  • yes ,i did that but unfortunately it does not add values. – Sarah_Salar Feb 19 '18 at 10:31
  • foreach($_POST['categories'] as $key => $value) { $sql1 ="INSERT INTO rs (Category) VALUES ('$value')"; //echo "$value
    "; } can anyone help? this only adds last value of the array into the field. $sql .="" does not help
    – Sarah_Salar Feb 20 '18 at 07:34
  • @MichaelEmerson kindly look at this problem https://stackoverflow.com/questions/48494904/html-drill-down-drop-down-selected-value-does-not-inserted-in-mysql/48494926?noredirect=1#comment83991507_48494926 – Sarah_Salar Feb 22 '18 at 04:55

0 Answers0