FarrisFahad
FarrisFahad

Reputation: 372

How to get the id in the URL using PHP and MYSQL?

<form name="update" method="post" action="ex_update.php?get_id=<?php echo $_GET['id']; ?>">
          <p><strong>Enter Name:</strong>
            <input type="text" name="name">
            <br />
            ID: 
            <label for="select"></label>
            <select name="id">
            <?php 
                $query = "SELECT * FROM test";
                $run = mysql_query($query);
                while($output = mysql_fetch_array($run)){
                echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
            ?>
            </select>
          </p>
          <p>
            <input type="submit" name="submit" value="Update!">
          </p>
        </form>

This is the form that I submit through. I am pretty sure that the problem is in the form because every time I submit it I get index.php?id= || index.php?id=0 So the problem is that I want to pick-up the id which I select in the select menu

Thanks a lot FarrisFahad

Upvotes: 0

Views: 1384

Answers (1)

Bot
Bot

Reputation: 11845

Change method to GET and remove the stuff from action. It will automatically put the id into the get. I also fixed your label for you.

<?php 

$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test"); 
$id = (int)$_POST['id'];
?>

<table bordercolor="#0099FF" width="1000" border="5" align="center" cellpadding="5" cellspacing="5">
  <tr>
    <td><form name="get_id" method="GET" action="Test.php">
        <label for="select">ID:</label>
        <select name="id" id="select">
            <?php 
                $query = "SELECT * FROM test";
                $run = mysql_query($query);
                while($output = mysql_fetch_array($run)){
                echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
            ?>
        </select>
        <input type="submit" id="button" value="Submit"></form>
    </td>

If you must use both get and post try this

<?php 

$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test"); 
$id = (int)$_POST['id'];
?>
<script type="text/javascript">
    function changeAction() {
        document.get_id.action = 'Test.php?id=' + document.get_id.options[document.get_id.selectedIndex].value;
    }
</script>
<table bordercolor="#0099FF" width="1000" border="5" align="center" cellpadding="5" cellspacing="5">
  <tr>
    <td><form name="get_id" method="POST" action="Test.php">
        <label for="select">ID:</label>
        <select name="id" id="select" onchange="changeAction">
            <?php 
                $query = "SELECT * FROM test";
                $run = mysql_query($query);
                while($output = mysql_fetch_array($run)){
                echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
            ?>
        </select>
        <input type="submit" id="button" value="Submit"></form>
    </td>

Upvotes: 2

Related Questions