0

I have store labels and numeric values of same form in two arrays, pass these arrays using AJAX to php. now I want to store each array in two different fields of the same table mysql.

var arr = [];
var arr1 = [];

$('.cat').each(function() {         
   arr.push($(this).text());
});

$('.cat_value').each(function() {         
   arr1.push($(this).val());
});

$.ajax({
    url:'rs_insert.php',
    data:{categories: arr, cat_values:arr1},
    type:'POST',
    success:function() {
        alert("data has been sent");
  document.getElementById('exampleModal1').style.display = "none"; 
    }      
}); 

php file

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

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$values = array();
foreach($_POST['categories'] as $key => $name) { 
  $values[] = $name;
}

$sql1 = "INSERT INTO rs (Category) VALUES ";
foreach($values as $cat) {
  $sql1 .= "('" . $cat . "'),";
}

$sql1_trimmed = rtrim($sql1,',');

////

$values1 = array();
foreach($_POST['cat_values'] as $key => $value) { 
  $values1[] = $value;
}

$sql2 = "INSERT INTO rs (Value) VALUES ";
foreach($values1 as $val) {
  $sql2 .= "('" . $val . "'),";
}

$sql2_trimmed = rtrim($sql2,',');
if (!mysqli_query($con,$sql1_trimmed)) {
  die('Error: ' . mysqli_error($con));
}
 if (!mysqli_query($con,$sql2_trimmed)) {
   die('Error: ' . mysqli_error($con));
 }

the problem is, it does not inserted values parrallel. first it insert values in category field with 0 under value field. the filled value field with blank categories name. enter image description here

kapantzak
  • 11,610
  • 4
  • 39
  • 61
Sarah_Salar
  • 183
  • 2
  • 13
  • 1
    I'd suggest preparing/binding. – ezw Feb 21 '18 at 07:51
  • 1
    @Sarah_Salar before inserting in database. merge the both arrays { array_merge($a1,$a2) } then you have a single array. key will cat and value will be cat_value. If you still unable to understand i will write the answer. – Adnan Haider Feb 21 '18 at 08:08
  • @Adnanhaider can u look at this problem https://stackoverflow.com/questions/49043561/insert-two-array-and-variable-value-into-mysql-table-with-single-query i am stuck here do not understand wht i am doing wrong – Sarah_Salar Mar 05 '18 at 07:44
  • i have manually add data into the table. it was successfully inserted – Sarah_Salar Mar 05 '18 at 07:45

1 Answers1

2

you can try this:

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

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$values = array();
foreach($_POST['categories'] as $key => $name) { 
  $values[] = $name;
}

////

$values1 = array();
foreach($_POST['cat_values'] as $key => $value) { 
  $values1[] = $value;
}

$sql1 = "INSERT INTO rs (Category,Value) VALUES ";
for($i=0;$i<count($values);$i++) {
  $sql1 .= "('" . $values[$i] . "','". $values1[$i] ."'),";
}

$sql1_trimmed = rtrim($sql1,',');

if (!mysqli_query($con,$sql1_trimmed)) {
  die('Error: ' . mysqli_error($con));
}
Madhu
  • 326
  • 2
  • 7
  • thanks alot :) can u look at my problem here 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:54