Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Depending on the likeliness of a "clean-url" being used multiple times, your approach may not be the best to roll with. Say there was "foo" to "foo-10" you'd be calling the database 10 times.</p> <p>you also don't seem to sanitize the data you shove into your SQL queries. Are you using <a href="http://se2.php.net/mysql_real_escape_string" rel="nofollow">mysql_real_escape_string</a> (or its mysqli, PDO, whatever brother)?</p> <p>Revised code:</p> <pre><code>public function prevent_double_cleanurl($cleanurl) { $cleanurl_pattern = '#^(?&lt;base&gt;.*?)(-(?&lt;num&gt;\d+))?$#S'; if (preg_match($cleanurl_pattern, $base, $matches)) { $base = $matches['base']; $num = $matches['num'] ? $matches['num'] : 0; } else { $base = $cleanurl; $num = 0; } // makes sure it doesnt check against itself if ($this-&gt;ID != null) { $and = " AND product_ID &lt;&gt; " . $this-&gt;ID; } $sql = "SELECT product_ID, titel_url FROM " . $this-&gt;_table . " WHERE titel_url LIKE '" . $base . "-%' LIMIT 1"; $result = $this-&gt;query($sql); foreach ($result as $row) { if ($this-&gt;ID &amp;&amp; $row['product_ID'] == $this-&gt;ID) { // the given cleanurl already has an ID, // so we better not touch it return $cleanurl; } if (preg_match($cleanurl_pattern, $row['titel_url'], $matches)) { $_base = $matches['base']; $_num = $matches['num'] ? $matches['num'] : 0; } else { $_base = $row['titel_url']; $_num = 0; } if ($base != $_base) { // make sure we're not accidentally comparing "foo-123" and "foo-bar-123" continue; } if ($_num &gt; $num) { $num = $_num; } } // next free number $num++; return $base . '-' . $num; } </code></pre> <p>I don't know about the possible values for your clean-urls. Last time I did something like this, my base could look like <code>some-article-revision-5</code>. That <code>5</code> being part of the actual bullet, not the duplication-index. To distinguish them (and allow the <code>LIKE</code> to filter out false positives) I made the clean-urls look like <code>$base--$num</code>. the double dash could only occur between the base and the duplication-index, making things a bit simpler…</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload