连接php foreach中的字符串(concatenate string in php foreach)

编程入门 行业动态 更新时间:2024-10-24 06:34:22
连接php foreach中的字符串(concatenate string in php foreach)

我应该能够解决这个问题,但我会继续前进。 我有一个带复选框的表单。 选中复选框后,标记为删除。

我用foreach处理那些选中的复选框:

foreach (array_keys($_POST['checkboxselect']) as $k) { $str .= $k.','; }

这是我将视频列表放在一起的蹩脚尝试。 我正在使用的sql删除是:

DELETE FROM `videos` WHERE `video_name` IN ($str)

所以我在这里遗漏了很多东西。 $str需要:

只返回一个复选框,因此返回带引号的字符串(即“myvideo.mp4”) 选中多个复选框,因此使用引号和逗号构建一个字符串(即“myvideo.mp4”,“myvideo2.mp4”,“myvideo3.mp4”)

谢谢您的帮助。

I should be able to figure this out, but I keep going in circles. I have a form with checkboxes. When the checkboxes are selected they tagged to be deleted.

I process those selected checkboxes with a foreach:

foreach (array_keys($_POST['checkboxselect']) as $k) { $str .= $k.','; }

This is my lame attempt to put the list of videos together. The sql delete I'm using is:

DELETE FROM `videos` WHERE `video_name` IN ($str)

So I'm missing lots of things here. The $str needs to have:

Only one checkbox is returned so return string with quotes (i.e. "myvideo.mp4") Multiple checkboxes are selected so build a string with quotes and commas (i.e. "myvideo.mp4","myvideo2.mp4","myvideo3.mp4")

Thanks for the help.

最满意答案

尝试使用PHP的内爆 :

// Init links $mysqliLink = mysqli_connect('host', 'user', 'password') or die('Could not connect: ' . mysqli_error()); $mysqlLink = mysql_connect('host', 'user', 'password') or die('Could not connect: ' . mysql_error()); //----- // Prep values for query //----- // Only pick one of the following depending upon the mysql library you are using // If using mysql, passing the 2nd argument is not necessary, but for // clarity, adding it $deleteNames = array_map('mysql_real_escape_string', array_keys($_POST['checkboxselect']), array_fill(0, count($_POST['checkboxselect']), $mysqlLink)); // If using mysqli // This will ensure that $mysqliLink will be passed in as first arg for // every iteration satisfying the function signature $deleteNames = array_map('mysqli_real_escape_string', array_fill(0, count($_POST['checkboxselect']), $mysqliLink), array_keys($_POST['checkboxselect'])); //----- // Because you are passing strings, we need to ensure each is wrapped in quotes $deleteNames = "'" . implode("','", $deleteNames) . "'"; // Construct query using implode $sql = sprintf('DELETE FROM `videos` WHERE `video_name` IN (%s)', $deleteNames);

- 更新 -

使用Joomla API:

$db = &JFactory::getDBO(); // Localize and sanitize each individual value foreach (array_keys($_POST['checkboxselect']) as $element) { $deleteNames[] = $db->quote($element); } // Because you are passing strings, we need to ensure each is wrapped in quotes // No longer true because $db->quote taking care of quoting out each name for us $deleteNames = implode(',', $deleteNames); // Construct query using implode $sql = sprintf('DELETE FROM `videos` WHERE `video_name` IN (%s)', $deleteNames);

Try using PHP's implode:

// Init links $mysqliLink = mysqli_connect('host', 'user', 'password') or die('Could not connect: ' . mysqli_error()); $mysqlLink = mysql_connect('host', 'user', 'password') or die('Could not connect: ' . mysql_error()); //----- // Prep values for query //----- // Only pick one of the following depending upon the mysql library you are using // If using mysql, passing the 2nd argument is not necessary, but for // clarity, adding it $deleteNames = array_map('mysql_real_escape_string', array_keys($_POST['checkboxselect']), array_fill(0, count($_POST['checkboxselect']), $mysqlLink)); // If using mysqli // This will ensure that $mysqliLink will be passed in as first arg for // every iteration satisfying the function signature $deleteNames = array_map('mysqli_real_escape_string', array_fill(0, count($_POST['checkboxselect']), $mysqliLink), array_keys($_POST['checkboxselect'])); //----- // Because you are passing strings, we need to ensure each is wrapped in quotes $deleteNames = "'" . implode("','", $deleteNames) . "'"; // Construct query using implode $sql = sprintf('DELETE FROM `videos` WHERE `video_name` IN (%s)', $deleteNames);

-- Update --

Using Joomla APIs:

$db = &JFactory::getDBO(); // Localize and sanitize each individual value foreach (array_keys($_POST['checkboxselect']) as $element) { $deleteNames[] = $db->quote($element); } // Because you are passing strings, we need to ensure each is wrapped in quotes // No longer true because $db->quote taking care of quoting out each name for us $deleteNames = implode(',', $deleteNames); // Construct query using implode $sql = sprintf('DELETE FROM `videos` WHERE `video_name` IN (%s)', $deleteNames);

更多推荐

$str,mp,myvideo,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-08-06 06:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   foreach   php   concatenate   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!