Viewed   338 times

Update: I forgot to mention that echo $matstring outputs '65.70', 'Coles','34 days','14' - which would appear to be the right syntax?

I'm a php/mysql newbie, and I think this is fairly basic, but having read all of the other questions on this topic and fiddling with different versions of my code for several hours I can't understand what I'm doing wrong. Would very much appreciate any help/suggestions.

Aim: pass data from my php array ($matrix) into a mysql table

( [0] => 65.70 [1] => Coles [2] => 34 days [3] => 14 )

( [0] => 62.70 [1] => Coles [2] => 13 days [3] => 14 )

( [0] => 12.70 [1] => Safeway [2] => 43 days [3] => 14 )


mysql_query('INSERT INTO Australia (Price, Company, Days, Weight) VALUES ('$matstring')');



Corrected code:


mysql_query("INSERT INTO Australia (Price, Company, Days, Weight) VALUES ('$matstring')");

(i.e. delete the second line from original code and put double quotes around the argument of mysql_query)

Appreciate user1847757's help - as s/he pointed out, $matstring itself was correct, but the single quotes inside of VALUES(' ') were being joined to the single quotes added to $matstring in the 2nd line of my original code, resulting in VALUES(''65.70','Coles','34 days','14'')

Thanks all for your help & suggestions

Monday, December 19, 2022

I would say just build it yourself. You can set it up like this:

$query = "INSERT INTO x (a,b,c) VALUES ";
foreach ($arr as $item) {
  $query .= "('".$item[0]."','".$item[1]."','".$item[2]."'),";
$query = rtrim($query,",");//remove the extra comma
//execute query

Don't forget to escape quotes if it's necessary.

Also, be careful that there's not too much data being sent at once. You may have to execute it in chunks instead of all at once.

Saturday, November 5, 2022

You should loop from $QryRow['Year_From'] to $QryRow['Year_To'] and collect them all before inserting them.

$years= array();
for ($x = $QryRow['Year_From']; $x <= $QryRow['Year_To']; $x++) 
    $years[] = $x;    

$query_insert = "INSERT INTO diff_yearstbl(SKU,Product_Name,Model,Make,Year) VALUES('".$SKU."','".$Product_Name."','".$Model."','".$Make."','".implode(',',$years)."')";
$s_insert = mysql_query($query_insert, $connect ); 

mysql_* is deprecated please use mysqli_* or PDO

Friday, September 2, 2022

there is another way

     while($row = mysql_fetch_array($selected)){
           $familyId = $row[0];
           $familyName = $row[1];



 $insertInfo = "INSERT INTO `family_info`.`info` 
            (`name`, `info`, `family`)
            VALUES (
            '$name', '$info', '$family');";
Saturday, October 22, 2022

When you're gathering the file information you're overwriting $imagename on every loop so it will be assigned to the last one. Try attaching it to the $files variable (hopefully this doesn't mess with the upload class you're using).

foreach ($l as $i => $v)
    if (!array_key_exists($i, $files))
    $files[$i] = array();
    $files[$i][$k] = $v;
    $files[$i]['imagename'] = $_POST['keyword'][$i];

Then update your $sql string to reference that

$sql = 'INSERT INTO cover (id, img, keyword, category_name, cat_id) 
     VALUES ("", "'.$this_upload['image'].'", "'.$file['imagename'].'", 
         "'.$category_name.'", "'.$category.'")';
Wednesday, November 16, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :