0

First of all let me say, I am new to this and my code is probably laughable to those of you who know what you're doing...

I am trying to retrieve data from my mysql database and display the information in a matching dropdown list. I have this part working with some ugly basic code.

$url = $_SERVER["REQUEST_URI"];
$url_path = parse_url($url, PHP_URL_PATH);
$devid = pathinfo($url_path, PATHINFO_BASENAME);

$con = mysqli_connect("localhost","xxxxxx","xxxxxx","xxxxxxxx");
  //Run a query
  $result = mysqli_query($con,"SELECT type FROM `xxxxx`.`xxxxxx` WHERE device_id='$devid'");


$pulldown1 = '<select name="extension_type">';

while($row = mysqli_fetch_array($result))
{
if($row['type'] == "Unlimited")
{
$pulldown1 .= "<option selected value=\"Unlimited\">Unlimited Extension</option>
<option value=\"Metered\">Metered Extension</option>
<option value=\"Virtual\">Virtual Extension</option>
\n";
}
if($row['type'] == "Metered"){
$pulldown1 .= "<option selected value=\"Metered\">Metered Extension</option>
<option value=\"Unlimited\">Unlimited Extension</option>
<option value=\"Virtual\">Virtual Extension</option>
\n";
}
if($row['type'] == "Virtual"){
$pulldown1 .= "<option selected value=\"Virtual\">Virtual Extension</option>
<option value=\"Unlimited\">Unlimited Extension</option>
<option value=\"Metered\">Metered Extension</option>
\n";
}

}
$pulldown1 .= '</select>';

echo $pulldown1;

mysqli_close($con);

Now I'd like to be able to CHANGE the value and have it save the corresponding value and update that column in the database. This is where I am stuck. Can someone guide me in the right direction, please?

4
  • You want to change the value of what? Commented Apr 22, 2013 at 20:09
  • Specify what value you want to change, and also you might wanna look into protecting your SQL queries from SQL-injection. Commented Apr 22, 2013 at 20:11
  • this is how to save dropdownlist stackoverflow.com/questions/16125893/… . To update a list , you need to populate in a form. Commented Apr 22, 2013 at 20:11
  • Change the dropdown from it's current selected option to a different option and update the value in the db. Commented Apr 22, 2013 at 20:12

2 Answers 2

3

You could use jQuery to send an AJAX post request when the select box has it's value changed. In turn this could update your mysql database entry. If this is the kind of thing your wanting let me know and I'll expand on my answer further with a code example.

JS Code example

$("select").change(function () {
    // Send ajax request
    $.ajax
    ({
       type: "POST",
       url: "/change-value.php",
       dataType: 'json',
       data: { location: $(this).val() },
       cache: false,
       success: function(data)
       {
       }
    });

});

// PHP change-value.php example

<?php

// Get the changed value
$value = $_POST['value'];

// Code to update you mysql database entry would go here...
Sign up to request clarification or add additional context in comments.

4 Comments

That would be helpful, please elaborate.
I'll need more to go on than that?
If you want $_POST['extype'] to be accessible from within your change-value.php, you'll have to send it through in you ajax request. So where you have data { location: $(this).val() } you'll want to add the applicable code to grab the value of extype. E.g. data{location: $(this).val(), extype: $('#extype').val() }
Are you able to select if any of these answers provided to you were correct :)
0

Modified from the PHP documentation on php.net:

<?php
if ( !empty($_POST['extension_type']) )
{
   // Your MySQL connection code should be at the top of the script.
   $query = "UPDATE `xxxxx` SET `extension_type` = ? WHERE `[your_id_field_here]` = ?";
   /* Prepared statement, stage 1: prepare */
   if ( !($stmt = $mysqli->prepare($query) ) {
       echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
   }
   /* Prepared statement, stage 2: bind and execute */
   if ( !$stmt->bind_param("s", $_POST['extension_type']) || !$stmt->bind_param("i", intval($id)) ) {
       echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
   }
   if ( !$stmt->execute() ) {
       echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
   }
}
?>

As noted by @Simon Carlson, you also need to account for SQL injection. If you have a set list of extension types, you could improve on the code above by checking the $_POST value against known valid values.

3 Comments

I tried this exact method and I get "Unexpected T_VARIABLE on "$query = "UPDATE xxxxx SET extension_type = ? WHERE ID = ?";" "
Right, you have to fill in your information for the MySQL connection, table, etc. You can't just copy and paste this and expect it to work. This is just an example to show you in general terms how to do it. I can be more specific if you give us more specifics.
I have already specified the connection, table, info etc, see above. So long as I do this before the "mysqli_close($con);" it should inherit what I have already defined when I execute mysqli_query($con,$query), right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.