2

I have a mySQL table with the fields:

  • preview_url
  • large_url

And I have an object that I submit with the following structure:

var $urls = {largeImg:[],preview:[]}

$urls.largeImg values have to be inserted into 'large_url' and

$urls.preview_url values have to be inserted into 'preview_url'

$urls.largeImg[0] has to go in the same mysql table row as $urls.preview[0],

$urls.largeImg[1] into the same row as $urls.preview[1] and so on.

my php:

$urls = $_POST['urls'];

function cache_urls($urls){
global $db;


foreach($urls as $url){
    $sql = "INSERT INTO cache ";
    $sql .= "(preview_url, large_url) ";
    $sql .= "VALUES (";
    $sql .= "'" . db_escape($db, $url['preview']) . "', ";
    $sql .= "'" . db_escape($db, $url['largeImg']) . "'";
    $sql .= ");";

    $result = mysqli_query($db, $sql);
}

And then I also tried this:

foreach($urls as $url){
    foreach($url as $key => $value){
        $sql = "INSERT INTO cache ";
        $sql .= "(preview_url, large_url) ";
        $sql .= "VALUES (";
        if($key==="preview"){
            $sql .= "'" . db_escape($db, $value) . "', ";
        }
        if($key==="largeImg"){
            $sql .= "'" . db_escape($db, $value) . "'";
        }

        $sql .= ");";

        $result = mysqli_query($db, $sql);
    }       
}

So I assume the SQL bit must be wrong but I'm really at the end of my knowledge! Any help much appreciated.

10
  • have you tried to echo $sql? Commented Sep 9, 2019 at 1:19
  • do you mean $sql or $result? Commented Sep 9, 2019 at 1:31
  • your $sql query string Commented Sep 9, 2019 at 1:33
  • I am not getting anything back, I am submitting this with ajax (jQuery). I tried to return $sql but I get nothing back. Commented Sep 9, 2019 at 1:39
  • well, try this echo "<script type='text/javascript'>alert(" + $sql + ")</script>"; Commented Sep 9, 2019 at 1:40

1 Answer 1

1

You should do it like this way,

$sql = "INSERT INTO cache (preview_url, large_url) values";
foreach($urls["largeImg"] as $index => $large_url){
    $preview_url = $urls["preview"][$index];
    $sql .= "('" . db_escape($db,$preview_url) . "','" . db_escape($db,$large_url) . "'),";
}
$sql = rtrim($sql,",");
Sign up to request clarification or add additional context in comments.

12 Comments

thanks a lot for your answer. i get this error back: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'cdn.pixabay.com/photo/2017/09/30/15/10/…' at line 1
the url above in the error message is the second value from the array urls.preview (urls.preview[1]) if that helps?
@Melvin can you show you sql and error with text, not an image here.
that's the error message i get back, including the image url.
what's the value of sql
|

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.