Note that there are some explanatory texts on larger screens.

plurals
  1. POImproving this longwinded and repetative sql script
    primarykey
    data
    text
    <p>I've been learning about DRY code and my code isn't DRY...</p> <p>For example, I have a custom CMS and I save basically a name, content and a publish status for a few things... like an article, a user, a event. To submit a form, I submit to a file (process.php) which has a switch in it like so:</p> <pre><code>switch($_POST['process']) { case 'speaker': if($_POST['speaker_id']) { $sql = ' UPDATE speakers SET speaker_name="' . mysql_escape_string($_POST['speaker_name']) . '", speaker_content="' . mysql_escape_string($_POST['speaker_content']) . '", speaker_status="' . $_POST['speaker_status'] . '" WHERE speaker_id="' . $_POST['speaker_id'] . '" LIMIT 1 '; } else { $sql = ' INSERT INTO speakers SET speaker_name="' . mysql_escape_string($_POST['speaker_name']) . '", speaker_content="' . mysql_escape_string($_POST['speaker_content']) . '", speaker_status="' . $_POST['speaker_status'] . '" '; } mysql_query($sql); if($_POST['speaker_id']) { header('Location: speakers?speaker_id=' . $_POST['speaker_id']); } else { header('Location: speakers?speaker_id=' . mysql_insert_id); } break; case 'event': if($_POST['event_id']) { $sql = ' UPDATE events SET event_name="' . mysql_escape_string($_POST['event_name']) . '", event_content="' . mysql_escape_string($_POST['event_content']) . '", event_status="' . $_POST['event_status'] . '" WHERE event_id="' . $_POST['event_id'] . '" LIMIT 1 '; } else { $sql = ' INSERT INTO events SET event_name="' . mysql_escape_string($_POST['event_name']) . '", event_content="' . mysql_escape_string($_POST['event_content']) . '", event_status="' . $_POST['event_status'] . '" '; } mysql_query($sql); if($_POST['event_id']) { header('Location: events?event_id=' . $_POST['event_id']); } else { header('Location: events?event_id=' . mysql_insert_id); } break; case 'article': if($_POST['article_id']) { $sql = ' UPDATE articles SET article_name="' . mysql_escape_string($_POST['article_name']) . '", article_content="' . mysql_escape_string($_POST['article_content']) . '", article_status="' . $_POST['article_status'] . '", article_modified="' . $_POST['article_modified'] . '" WHERE article_id="' . $_POST['article_id'] . '" LIMIT 1 '; } else { $sql = ' INSERT INTO articles SET article_name="' . mysql_escape_string($_POST['article_name']) . '", article_content="' . mysql_escape_string($_POST['article_content']) . '", article_status="' . $_POST['article_status'] . '" '; } mysql_query($sql); if($_POST['article_id']) { header('Location: articles?article_id=' . $_POST['article_id']); } else { header('Location: articles?article_id=' . mysql_insert_id); } break; } </code></pre> <p>Despite some basic variations, like different table names and column names, and perhaps there sometimes being a couple more or less columns to populate, the code is literally the same and programming like this feels more like data entry than creativity.</p> <p>I imagine there's a way to create a class for this so that all the below code could be achieved in 1/3 the amount. Is there some sort of streamlined mysql insert / update method/strategy?</p> <p>In my head, I'm thinking if I name all my inputs the same as they are in the table, ie if the column is called 'speaker_name' and the input is..</p> <pre><code>&lt;input type="text" name="speaker_name" /&gt; </code></pre> <p>...I wonder if I could have a function which went through the $_POST array and simply updated the appropriate fields. Is this sound logic?</p> <p>Perhaps I would have a hidden input in the form which was the 'table' variable which let the function know which table to update and it takes care of the rest.</p> <p>Excuse me while I just thought out-loud. Any ideas would be really cool!</p> <p><strong>My newbie solution</strong> Here's what I have i got working</p> <pre><code>if($_POST['id']) { $sql = 'UPDATE '; } else { $sql = 'INSERT INTO '; } // number of rows in array $total = count($_POST); // number of commas = total of values minus 1 $commas = $total - 1; // starting number $count = 1; foreach ($_POST as $key =&gt; $value) { if($count == 1) { $sql .= mysql_real_escape_string($value) . ' SET '; } else { if( $count &lt; $total ) { $sql .= $key . '="' . mysql_real_escape_string($value) . '"'; if($count != $commas) { $sql .= ', '; } } elseif( $_POST['id'] ) { $sql .= ' WHERE ' . $key . '="' . mysql_real_escape_string($value) . '"'; } } $count = $count + 1; } mysql_query($sql); if($_POST['id']) { header('Location: ' . $_POST['process'] . '?id=' . $_POST['id'] . ''); } else { header('Location: ' . $_POST['process'] . '?id=' . mysql_insert_id()); } </code></pre> <p>To do this means my form designs need to have a pretty strict setup ie the first hidden input holds the table name, the last input is the id number of the row in the table being edited (if it exists).</p> <p>I know its far from good... but a lot better than the hundreds of lines it previously took.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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