Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJen Ollivierre
929 PointsWordpress Custom PHP Form not updating Mysql Database
I am currently building a plugin in wordpress. I have successfully created the admin menu page. The plugin I am building is sports related and will require complex computation to generate stats, which means I have to build custom forms and add new mysql tables as there will be tons of data to input and the "wp_options" table will not be a wise move when it comes to retrieving the data to then compile the excess and sometimes complex stats. This part is no worry as I am pretty darn good at PHP, maths & logics, but I am using a custom built form to store data into the mysql database.
The form is adding the data to the table I created in database (which is basically "INSERT INTO table (column names) values (value names)") - This part functions good and does insert the data. Now the problem is when I am going to edit that data, there is some problem, and I am going to explain it more now.
In the form value attributes, I am using php to draw the current data from the database to plug the form value attribute. Therefore, the user can see what the current data that they are editing is. Now, if the data is changed, the form is editted beautifully and database updated with no fuss ( "UPDATE table SET column_name='edit_value' WHERE id='old_id'"). If I don't change any of the value in the form fields (input fields, select fields etc), then the data does not update in the database, and this is very strange. If I change the data in the form, it edits. If I leave the old data in the form, it does not edit. I cannot understand this.
I have done numerous forms like this before outside of wordpress and they all edit. I am wondering if wordpress have any features behind the scenes that stops the database from updating the same information that was already in the database.
I really would appreciate any and all help.
2 Answers
Jen Ollivierre
929 PointsThis is the form code below
if ( $_GET[ 'cp_edit' ] && $_GET[ 'id' ] ) {
$format_id = preg_replace( '/[^0-9]/', '', $_GET[ 'id' ] ); // captures database id to get current data //
$format_query = "SELECT * FROM wp_cp_format WHERE id='$format_id'"; // retrieves data from table //
$fq = $wpdb->get_results( $format_query ); // wordpress mysql database query to get results in array //
?>
<h1 class="just-testing-this">Edit Format</h1>
<form class="cp-club-form" method="POST" enctype="multipart/form-data" action="<?PHP echo admin_url( 'admin.php?page=cricket-press-club-format' ); ?>">
<?PHP
foreach ( $fq as $cp_value ) {
$name = $cp_value->name;
$overs = $cp_value->overs;
$days = $cp_value->days;
$inns = $cp_value->inns;
$type = $cp_value->type;
?>
Format Name: <input name="cp-club-format-name" type="text" value='<?PHP echo $name; ?>'>
Overs: <input name="cp-club-format-overs" type="text" value='<?PHP echo $overs; ?>'>
Days:
<select name="cp-club-format-days">
<?PHP
for ( $a = 1; $a <= 5; $a++) {
echo '<option value='.$a.' ';
if ( $a == $days ) {
echo 'selected='.$days;
}
echo '>'.$a.'</option>';
}
?>
</select>
Inns:
<select name="cp-club-format-inns">
<?PHP
for ( $b = 2; $b <= 4; $b++) {
echo '<option value='.$b.' ';
if ( $b == $inns ) {
echo 'selected='.$inns;
}
echo '>'.$b.'</option>';
}
?>
</select>
Type:
<select name="cp-club-format-type">
<?PHP
$season_array = array(
'test' => 'Test', 'one-day' => 'One Day', 't20' => 'T20'
);
foreach ( $season_array as $sa_key => $sa_value ) {
echo '<option value='.$sa_key.' ';
if ( $sa_value == ucwords( $type ) ) {
echo 'selected='.$sa_key;
}
echo '>'.$sa_value.'</option>';
}
?>
</select>
<?PHP
}
?>
<input type="hidden" name="cp_hidden" value='<?PHP echo $format_id; ?>'>
<input type="submit" name="cp-club-edit-format-submit" value='Save'>
</form>
<?PHP
}
HERE IS THE CODE TO UPDATE DATABASE
$update_array = array(
'name' => ucwords( stripslashes( $_POST[ 'cp-club-format-name' ] ) ),
'overs' => stripslashes( $_POST[ 'cp-club-format-overs' ] ),
'days' => stripslashes( $_POST[ 'cp-club-format-days' ] ),
'inns' => stripslashes( $_POST[ 'cp-club-format-inns' ] ),
'type' => str_replace ( '-', ' ', stripslashes( $_POST[ 'cp-club-format-type' ] ) )
);
$where_array = array( 'id' => stripslashes( $_POST[ 'cp_hidden' ] ) );
$edit_format_create = $wpdb->update( 'wp_cp_format', $update_array, $where_array );
foreach ( $update_array as $aa => $aav ) {
echo $aav."<br>";
}
if ( ! $edit_format_create ) {
echo "<p>Error: could not edit format, go back and try again. If problem persists, contact your website
administrator!</p>";
} else {
echo "<p>Format ".htmlspecialchars( $name )." was successfully editted!</p>";
}
}
Jen Ollivierre
929 PointsNevermind folks, I just did a simple sql query to check to see if the updated form contained old data, if it did, I just echo a update message without actually updating the database. Simple as that to bypass this nonsense that I don't know is happening. See, logics really is my domain. #PHPmesay