Hi all i need to update some iframe values in my videos table, embed column. Can I achieve this with this kind code below ? I tried already but i got syntax errors. Please help me. here is the code

type of column embed is text.

UPDATE videos
SET embed RAND('<iframe src="http://example.com" height="504" width="921" 
                        frameborder="0" scrolling="no">
               </iframe>',
              '<iframe src="http://example.com" height="504" width="921" 
                       frameborder="0" scrolling="no">
               </iframe>',
               '<iframe src="http://example.com height="504" width="921" 
                        frameborder="0" scrolling="no">
               </iframe>')
WHERE id in(5727,5723,5665)

Here is the error code; #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RAND ('

Dani AI

Generated

The #1064 syntax error in the original post was caused by two things: treating RAND like a function that accepts a list of alternative values and a malformed SET clause (the query was missing the =). HTML pasted as HTML-encoded entities (for example &lt;iframe&gt;) will also be stored literally unless decoded first. reported the error; was correct that a single UPDATE assigning one HTML string is the simplest fix when every row should get the same embed.

If different rows need different iframe HTML, use a deterministic mapping (CASE ... WHEN ... THEN ... END) or choose randomly per row with ELT(FLOOR(RAND()*N)+1, ...). Example patterns (replace the placeholder URLs and attributes as needed):

-- per-id assignment
UPDATE videos
SET embed = CASE id
  WHEN 5727 THEN '<iframe src="https://videos.example/1" height="360" width="640" frameborder="0"></iframe>'
  WHEN 5723 THEN '<iframe src="https://videos.example/2" height="360" width="640" frameborder="0"></iframe>'
  WHEN 5665 THEN '<iframe src="https://videos.example/3" height="360" width="640" frameborder="0"></iframe>'
  ELSE embed
END
WHERE id IN (5727,5723,5665);
-- random selection per updated row
UPDATE videos
SET embed = ELT(FLOOR(RAND()*3)+1,
  '<iframe src="https://videos.example/1" ...></iframe>',
  '<iframe src="https://videos.example/2" ...></iframe>',
  '<iframe src="https://videos.example/3" ...></iframe>'
)
WHERE id IN (5727,5723,5665);

For maintainability, follow 's suggestion: store the source URL and attributes in columns (for example src, height, width) and generate the final <iframe> in server-side code or a view. This avoids large HTML updates if dimensions or attributes change. Always test on a backup or a single row first, use transactions where possible, and prefer parameterized/prepared statements when assembling HTML to avoid quoting errors and injection risks.

Recommended Answers

All 2 Replies

UPDATE videos
SET embed = '<iframe src="http://example.com" height="504" width="921" frameborder="0" scrolling="no"></iframe>'
WHERE id IN (5727,5723,5665)
Member Avatar for Member #120589

The options for the random content that you provided are all the same. If this is right, then there is not randomness and you should use pritaeas' sql. If all your iframes have the same basic structure, perhaps it's easier to just store the src in one column and build the iframe with php on the fly. This is easier than having to update loads of records if you decide that you want the width to change to 674 instead (for example).

It's possible that you'd want to create a function to display the iframe so that you can have customisable dimensions (and other attributes/properties):

function createIframe($src, $h=NULL,$w=NULL)
{
    $parts = array();
    $parts[] = "<iframe src='$src'";
    if($h) $parts[] = "height='$h'";
    if($w) $parts[] = "width='$w'";
    $parts[] = "frameborder='0' scrolling='no'></iframe>";
    return implode(" ", $parts);
}

echo createIframe($row['src'],$height,$width);   

If you're hardcoding dimensions (as in DB values or above), then you could lose responsiveness.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.