Jump to content

How do added single quotes to list


mclamais

Recommended Posts


//Example value for p: ?p=12345_1,A2345_1,B3456_2

if(isset($_GET["p"])){
$p = $_GET["p"]
}	

$items = array();

foreach (explode(",", $p) as $input) {
list($sku, $quantity) = explode("_", trim($input) {
}
$skulist = implode(",", array_keys($items));

$query1  = "SELECT product_name, price FROM products WHERE sku IN(".$skulist.")";

 

This works fine for me if the sku values are numeric, but if alpha the sql fails.  Can some tell me the best method for adding single quotes around each value in the array or list.

 

So rather than sending this 12345,A2345,B3456  I need to sent '12345','A2345','B3456' to the query.

 

Thanks,

 

Marc

Link to comment
https://forums.phpfreaks.com/topic/110884-how-do-added-single-quotes-to-list/
Share on other sites

By the way, right here:

list($sku, $quantity) = explode("_", trim($input) {

 

There is no ending ) for the explode (and I don't know what the last { is for), like so:

list($sku, $quantity) = explode("_", trim($input));

 

 

And at the bottom where it says:

$skulist = implode(",", array_keys($items));

 

All $items is is "array();".

<?php

require_once('conn_localhost.php');

mysql_select_db($database) or die('Could not connect: ' . mysql_error());

 

if(isset($_GET["p"])){

$p = $_GET["p"]

}

 

$items = array();

 

foreach (explode(",", $p) as $input) {

list($sku, $quantity) = explode("_", trim($input));

 

$items[$sku] = $quantity;

}

 

$skulist = implode(",", array_keys($items));

 

$query1  = "SELECT sku, product_name, price, num_of_decals FROM products WHERE sku IN(".$skulist.")";

$result1 = mysql_query($query1);

 

The OP asked

Can some tell me the best method for adding single quotes around each value in the array or list.

which non of the other responders answered.

 

The answer is ...

 

change

<?php 
$skulist = implode(",", array_keys($items));
?>

to

<?php
$skulist = "'" . implode("','", array_keys($items)) . "'";
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.