Hello, please help me sold this issue. It can read the content on the MySql table but when I change the value on the input, then click on Submit, nothing is updated on the table. I don't know why.

<?php
        global $wpdb, $current_user;

        $user_id = get_current_user_id();

        $sql = 'SELECT `slot`, `id` FROM ab_staff WHERE `wp_user_id` = "' . (int)$user_id. '"';

        $member_slot = $wpdb->get_results( $sql );

        $member_slot_1=$member_slot[0];

        $d = get_object_vars($member_slot_1);

        $member_slot_2= $d['slot'];

        $member_slot_3= $d['id'];
?>
<?php
if(isset($_POST["submit"])) {
$slot_time = $_POST['member_slot'];
$command = "UPDATE ab_staff SET `slot` = '$slot_time' WHERE `id` ='$member_slot_3'";
$result = $wpdb->get_results($command); 
}
print_r ($slot_time);
?>
<label class="control-label" for="ab-staff-member"><?php _e( 'Time Slot', 'ab') ?></label>
<form action="<?php $_PHP_SELF ?>" method="post">
<select id="member_slot" name="member_slot">
<option value="900" <?php  if ( $member_slot_2 == 900) { echo 'selected=\"selected\"'; }; ?>>15 minutes</option>
<option value="1200" <?php  if ( $member_slot_2 == 1200) { echo 'selected=\"selected\"'; }; ?>>20 minutes</option>
<option value="1800" <?php  if ( $member_slot_2 == 1800) { echo 'selected=\"selected\"'; }; ?>>30 minutes</option>
<option value="2700" <?php  if ( $member_slot_2 == 2700) { echo 'selected=\"selected\"'; }; ?>>45 minutes</option>
<option value="3600" <?php  if ( $member_slot_2 == 3600) { echo 'selected=\"selected\"'; }; ?>>60 minutes</option>
</select>
<input name="submit" type="submit" value="<?php _e( 'Submit', 'xoousers' ); ?>">
</form>
</td>

Recommended Answers

All 6 Replies

Paul-lucas, I don't know which framework or cms you are using, but I think the get results method only selects information from the database.

Reason
Updating the database only affects rows but does not output any results.

So kindly look into your framework's or cms' docs and get the method/function used for updating the database.

Thank you, I'm using Wordpress. I'm trying to look for anothers method. But, do you know how to update the database without refreshing the page?

Yh, but you must use ajax. Here you setup a new php file which will process the form and then ajax will make the asynchronous / background work for you.

Replace

$command = "UPDATE ab_staff SET `slot` = '$slot_time' WHERE `id` ='$member_slot_3'";
$result = $wpdb->get_results($command); 

this code with

$wpdb->update('ab_staff', array( 'slot' => $slot_time), array( 'id' => $member_slot_3));

For reference Click Here

Thank you, but it doesn't work :(

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.