Mostafa Posted April 9, 2017 Share Posted April 9, 2017 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 Link to comment Share on other sites More sharing options...
benanamen Posted April 9, 2017 Share Posted April 9, 2017 It is not updating the database because you have no code to insert into the DB, or did you just forget to show us the relevant code to your post? That sure is a lot of unnecessary echos. You wouldnt have to escape all the attributes if you used single quotes. Link to comment Share on other sites More sharing options...
Mostafa Posted April 9, 2017 Author Share Posted April 9, 2017 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> Link to comment Share on other sites More sharing options...
Mostafa Posted April 9, 2017 Author Share Posted April 9, 2017 also the update database is work , but i ask about select menu . when i choose option it's work and update database but the value didn't correct . i can't explain but i wish you can help me Link to comment Share on other sites More sharing options...
ginerjm Posted April 9, 2017 Share Posted April 9, 2017 Does this actually produce a proper looking html document? Have you looked at the Source in your browser? If it is correct, then you need to show us the code that does the update! Link to comment Share on other sites More sharing options...
Mostafa Posted April 9, 2017 Author Share Posted April 9, 2017 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 More sharing options...
ginerjm Posted April 9, 2017 Share Posted April 9, 2017 If you turned on php error checking you would get some idea of the problems you are having. You Can't Mix MySQL with mysqli methods. Switch to mysqli and STOP USING THE MYSQL FUNCTIONS. Link to comment Share on other sites More sharing options...
Mostafa Posted April 9, 2017 Author Share Posted April 9, 2017 thank you i will use mysqli , but no error happen in select menu that's my main problem now how can i solve it ? you can check 2 files if you have time and know what i talk about because no error appear . Link to comment Share on other sites More sharing options...
ginerjm Posted April 9, 2017 Share Posted April 9, 2017 Why don't you echo out all of the queries you are creating and see what they look like? Link to comment Share on other sites More sharing options...
Mostafa Posted April 9, 2017 Author Share Posted April 9, 2017 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 More sharing options...
ginerjm Posted April 9, 2017 Share Posted April 9, 2017 If the update is not working print out the queries variable to see if the query statements are correct. that is what I would do It is called "basic debugging". Did you turn on error checking yet (see my signature) Link to comment Share on other sites More sharing options...
Mostafa Posted April 9, 2017 Author Share Posted April 9, 2017 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 More sharing options...
ginerjm Posted April 9, 2017 Share Posted April 9, 2017 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 More sharing options...
Jacques1 Posted April 9, 2017 Share Posted April 9, 2017 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 More sharing options...
Mostafa Posted April 10, 2017 Author Share Posted April 10, 2017 Thank you , i will try the code and also try to learn it , * i make the form but doesn't work with me , that's why i ask here or in stackover to learn but i face some difficult to understand . Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.