Jump to content

select menu send value to database


Mostafa
Go to solution Solved by Jacques1,

Recommended Posts

Hello 

 

i need update value in database , by choose a value in select menu , i try this code but didn't work

 



echo "<td>";
                echo "<select name=\"status[]\">"; 
                    echo "<option value=\"open\"> {$row['stu_status']} </option>";
                    echo "<option value=\"close\">closee</option>";
                echo "</select>";
            echo "</td>";


 

i need the display option  ' the one has already insert in database ' 

and another option 

 

i need 2 option 1- open 2- close

 

thank you

Edited by Mostafa
Link to comment
Share on other sites

yes i have the code 

    <?php require_once('../con/config.php'); ?>

<?php
 
    mysql_select_db($database_config, $config);
    mysql_query("set names 'utf8'");

    $query = "SELECT * FROM sec1octa";
    $result = mysql_query($query);
    ?>

<form method="POST" action="edit_data.php">

echo "<table>";
echo "<tr>";
    echo "<th>id</th>";
    echo "<th>name</th>";
    echo "<th>status</th>";
echo "</tr>";
if(mysql_num_rows($result)>0){
    while($row=mysql_fetch_array($result)){
        echo "<tr>";
            echo "<td><input type=\"hidden\" name=\"id[]\" value=\"{$row['stu_no']}\" size=\"15\"></td>";
            echo "<td>{$row['stu_name']}</td>"; 
            echo "<td>";
                echo "<select name=\"status[]\">"; 
                    echo "<option value=\"open\"> {$row['stu_status']} </option>";
                    echo "<option value=\"close\">closee</option>";
                echo "</select>";
            echo "</td>";
        echo "</tr>";
    }
}
?>
</table>
<input class="buttonstyle" type="submit" value="حفظ">

</form>
Edited by Mostafa
Link to comment
Share on other sites

mod.php code

  
    <?php require_once('../Con/config.php'); ?>
   

 <?php

echo '
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<center> ';
    
    
    mysql_select_db($database_config, $config);
    mysql_query("set names 'utf8'");

    $query = "SELECT * FROM sec1octa"; //You don't need a ; like you do in SQL
    $result = mysql_query($query);
    ?>

<form method="POST" action="edit_data.php">

<?php

echo "<table>";
echo "<tr>";
    echo "<th>id</th>";
    echo "<th>name</th>";
    echo "<th>status</th>";
echo "</tr>";
if(mysql_num_rows($result)>0){
    while($row=mysql_fetch_array($result)){
        echo "<tr>";
            echo "<td><input type=\"hidden\" name=\"id[]\" value=\"{$row['stu_no']}\" size=\"15\"></td>";
            echo "<td>{$row['stu_name']}</td>"; 
            echo "<td>";
                echo "<select name=\"status[]\">"; 
                    echo "<option value=\"open\"> {$row['stu_status']} </option>";
                    echo "<option value=\"close\">closee</option>";
                echo "</select>";
            echo "</td>";
        echo "</tr>";
    }
}
?>
</table>
<input class="buttonstyle" type="submit" value="حفظ">

</form>

edit_data.php

    <?php require_once('../Con/config.php'); ?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
    <?php

mysql_select_db('gohargro_student');
$db= mysqli_connect("localhost","****","*****","****");


    mysql_query("set names 'utf8'");
       
if(isset($_POST['id'])){
    $tally=0;

    // build all queries for the batch
    foreach($_POST['id'] as $index=>$id){
        $queries[]="UPDATE `goh`.`sec1octa` SET `stu_status`='".mysqli_real_escape_string($db,$_POST['status'][$index])."' WHERE `stu_no`='".mysqli_real_escape_string($db,$id)."'";
    }

    // run all queries
    if(mysqli_multi_query($db,implode(';',$queries))) {
        do{
            $tally+=mysqli_affected_rows($db);
        } while(mysqli_more_results($db) && mysqli_next_result($db));
    }

    // assess the outcome
    if($error_mess=mysqli_error($db)){
        echo "Syntax Error: $error_mess";
    }else{
        echo "$tally row",($tally!=1?"s":"")," updated";
    }
    mysqli_close($con);
}

?>

this is full code , 2 page.

Link to comment
Share on other sites

sorry i still understand you , i think you talk about thing i didn't see it , 

i just need to solve the problem about ' select menu ' that's all i need 

i need make 2 option in select menu , and insert (  or update ) the choose option in my database 

and when i return in page which i choose option form it , appear in select menu the option was already inserted in database and also the other option

 

Thank you . 

Link to comment
Share on other sites

i try but no error appear . 

echo "<select name=\"status[]\">"; 
                    echo "<option value=\"open\"> {$row['stu_status']} </option>";
                    echo "<option value=\"close\">closee</option>";
                echo "</select>";

i just need to edit this code , i need select menu have 2 options to insert the chosen options to my database

it's very hard to explain what happen when i click update button it's make update an return value , and if i make another update to another row , the past row also affected . 

Link to comment
Share on other sites

You are showing me your select which looks fine. Why Won't you show me your update code that is not working?????

 

PS try writing this code like this:

 

echo "<select name='status[]'>";
echo "<option value='open'>" . $row['stu_status'] . "</option>";
echo "<option value='close'>closee</option>";
echo "</select>";

 

A lot easier to read.

Link to comment
Share on other sites

  • Solution

The code is completely fudged up, and it seems you blindly copied and pasted it from Stackoverflow without understanding how it works.

 

If you want to be a programmer, you need to understand what you're doing. Writing down random code and relying on people on the Internet doesn't help.

 

The form you want looks like this:

<!-- Just send the data to the current script; no need for an extra file -->
<form method="post">
    <!-- TODO: add an anti-CSRF token-->
    <input type="hidden" value="<?= html_escape($csrf_token) ?>">
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>name</th>
                <th>status</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($items as $item): ?>
                <tr>
                    <td><?= html_escape($item['stu_no']) ?></td>
                    <td><?= html_escape($item['stu_name']) ?></td>
                    <td>
                        <select name="status[<?= html_escape($item['stu_no']) ?>]">
                            <option value="open" <?php if ($item['stu_status'] == 'open'): ?>selected<?php endif; ?>>open</option>
                            <option value="closed" <?php if ($item['stu_status'] == 'closed'): ?>selected<?php endif; ?>>closed</option>
                        </select>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <input type="submit">
</form>

Do you understand the structure? Each status is stored under the ID of the item. For example, the new status for the item #42 would be $_POST['status']['42']. There's no need for this hidden field stuff.

 

Depending on the current status in the database, one of the two options is preselected with the selected attribute.

 

Note that all dynamic input must be HTML-escaped to prevent cross-site scripting attacks, and you need an anti-CSRF token. Also note that if the only possible status values are “open” and “closed”, then you should have a boolean field instead.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.