Click here to Skip to main content
14,216,557 members
Rate this:
Please Sign up or sign in to vote.
Hello

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

<?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>


i need the display option be ' the one has already insert in database '
and another option .
*the update database is work , but i ask about select menu . when i choose option it's work and update database and get value but when i make any another change in another row the value will changed
i need 2 option 1- open 2- close

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);
}

?>


What I have tried:

try to added three option ( one which has in database already and another 2 to choose but not work )
Posted
Updated 9-Apr-17 9:46am
v3
Comments
CHill60 9-Apr-17 14:33pm
   
Not clear. And there is nothing in the code you have supplied that either queries, nor inserts to, the database.
Member 13116863 9-Apr-17 15:00pm
   
i'm update my question . thank you

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

Just some code improvements
Add name attribute to button
<input class="buttonstyle" type="submit" value="حفظ" name="حفظ">

<?php
//handle the submit request
if (filter_input(INPUT_POST, 'حفظ', FILTER_SANITIZE_STRING)) {
    //get the posted values...change code accordingly
    $post_name = filter_input(INPUT_POST, 'name');
}
?>


I think you should separate the sql queries in another file

Last thing is the multiple echo, maybe you can try
$html = 
'<table>' . 
'<tr>';

 if(mysql_num_rows($result)>0){
    while($row=mysql_fetch_array($result)){  
    $html .= 'other html';
    }
}
//finally echo the html variable
echo $html;
   
Comments
Member 13116863 9-Apr-17 15:48pm
   
i'm update the question add edit_data.php page , thank you but i didn't understand the code or how can i use it in my pages ? specially to avoid multiple echo , and i'm still wait for solve my main problem about select menu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100