elentz Posted March 1, 2019 Share Posted March 1, 2019 I have a need to create a form that will get info from a mysql table, show that info, two fields one of which I want to be a checkbox that will need to update the table with either a 0 or a 1. I will later use that info. I have searched all over and haven't found what I am looking for. I can find tutorials for creating checkboxes but nothing what I need / want Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/ Share on other sites More sharing options...
cyberRobot Posted March 1, 2019 Share Posted March 1, 2019 There's a lot of potential steps in that description. Where are you stuck? Do you have any code? Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1564963 Share on other sites More sharing options...
ginerjm Posted March 1, 2019 Share Posted March 1, 2019 Do you currently know how to write any code, any language or are you truly a newbie? Your question leaves so many unanswered questions. Like - have you ever written an html form or a php script? Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1564964 Share on other sites More sharing options...
elentz Posted March 1, 2019 Author Share Posted March 1, 2019 I do have some code. $sql = "select * from extensions"; $result = mysqli_query($link,$sql) or die(mysql_error()); echo "<table border='3'> <tr> <th>Extension #</th> <th>Reboot</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['extension'] . "</td>"; echo "<td>'."<input type='checkbox' class='form' value='1' name='checkbox[" . $row['reset'] . "]' />".'</td>"; echo "</tr>"; } echo "</table>"; ?> I want to a. show the current value of the row reset and b. to have the ability to check the checkbox and change the value in the mysql table. I know I need to make the whole thing a form so that it can update the DB Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1564965 Share on other sites More sharing options...
ginerjm Posted March 1, 2019 Share Posted March 1, 2019 You are putting a value of '1' in every checkbox/row currently. Why not the reset value as the 'value' attribute instead? Then you could compare the value submitted against the original reset value (in a hidden array field)for each checkbox to see if they still match and if not use the new value in an update query. You will need to 'hide' the key of each record in a hidden input field using it as part of the name of that field such as "name='rowkey[]' " and probably rename the name of the checkbox to be 'rowreset[]' that both use the primary key as the index for each. I hope I'm clear - thinking on the fly here. Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1564966 Share on other sites More sharing options...
elentz Posted March 1, 2019 Author Share Posted March 1, 2019 I think I get what you are saying. I will see what I can do. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1564967 Share on other sites More sharing options...
ginerjm Posted March 1, 2019 Share Posted March 1, 2019 You could also do this with a separate form embedded into each row of the html table. Then you could skip the arrays and just use the same names on all rows with a submit button for each. That is if you wanted to do the updates on each row as they occur and not wait for the user to update everything... (which would NOT work in that case). Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1564968 Share on other sites More sharing options...
gizmola Posted March 1, 2019 Share Posted March 1, 2019 Does your extensions table have a primary key? It would be best to use that to find the row(s) you need to update. Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1564971 Share on other sites More sharing options...
elentz Posted March 2, 2019 Author Share Posted March 2, 2019 ginerjm That sounds like the way to go, I could have any number of rows and updating as each is checked would be best. Gizmola, I think I do have a primary key on that table, can't check from home tho. Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1564979 Share on other sites More sharing options...
elentz Posted March 4, 2019 Author Share Posted March 4, 2019 On 3/2/2019 at 8:10 AM, elentz said: Gizmola, I think I do have a primary key on that table, can't check from home tho. I do have a primary key assignedto the id field Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565000 Share on other sites More sharing options...
elentz Posted March 4, 2019 Author Share Posted March 4, 2019 On 3/1/2019 at 5:40 PM, ginerjm said: You could also do this with a separate form embedded into each row of the html table. Then you could skip the arrays and just use the same names on all rows with a submit button for each. That is if you wanted to do the updates on each row as they occur and not wait for the user to update everything... (which would NOT work in that case). Instead of a submit button could a checkbox work? What about a Yes/No dropdown? I want to select the phones I want to restart, update a DB table and then use a submit button to go to another page that will run a query to reset the phones. Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565001 Share on other sites More sharing options...
ginerjm Posted March 4, 2019 Share Posted March 4, 2019 Just use a form for each row with a submit button to update that row only. Then have a separate form from all the others (outside of the html table?) that has a submit to do your next task. Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565002 Share on other sites More sharing options...
elentz Posted March 4, 2019 Author Share Posted March 4, 2019 10 minutes ago, ginerjm said: Just use a form for each row with a submit button to update that row only. Then have a separate form from all the others (outside of the html table?) that has a submit to do your next task. Ah I think I understand now Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565003 Share on other sites More sharing options...
elentz Posted March 4, 2019 Author Share Posted March 4, 2019 I think I have the idea. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565004 Share on other sites More sharing options...
ginerjm Posted March 4, 2019 Share Posted March 4, 2019 HTH!! PS - using a checkbox (or something resembling one) does not make a good presentation for the user IMHO. A "submit" action should be some kind of button or link that the user is familiar with and recognizes as his course of action. A checkbox is just that - a check that should have an accompanying label indicating what the user is "checking" or "selecting" on. Certainly not a "form submittal". Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565006 Share on other sites More sharing options...
elentz Posted March 4, 2019 Author Share Posted March 4, 2019 4 hours ago, ginerjm said: HTH!! PS - using a checkbox (or something resembling one) does not make a good presentation for the user IMHO. A "submit" action should be some kind of button or link that the user is familiar with and recognizes as his course of action. A checkbox is just that - a check that should have an accompanying label indicating what the user is "checking" or "selecting" on. Certainly not a "form submittal". got it. I originally envisioned a bunch or rows with a checkbox on the end. check all the boxes you want, click a submit button to update the DB and run another page to reboot the phones. If I misspoke and indicated that I wanted to use the checkbox as a submit type of operation I apologize, not what I meant. I haven't had anytime to work on this at all today, maybe tomorrow Thanks Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565009 Share on other sites More sharing options...
ginerjm Posted March 4, 2019 Share Posted March 4, 2019 Perhaps my fault. I tend to make bad assumptions when trying to understand posts. I see how you describe you intent to use a checkbox would fit into that design. Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565010 Share on other sites More sharing options...
elentz Posted March 5, 2019 Author Share Posted March 5, 2019 Sometimes I might not make my needs as clear to others as it might appear in my head. Thanks fo ryour help Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565013 Share on other sites More sharing options...
Barand Posted March 5, 2019 Share Posted March 5, 2019 (edited) 11 hours ago, elentz said: I originally envisioned a bunch or rows with a checkbox on the end. check all the boxes you want, click a submit button to update the DB This does it with a single form <?php // // PROCESS POSTED DATA // if ($_SERVER['REQUEST_METHOD']=='POST') { $updt = $db->prepare("UPDATE extensions SET reset = ? WHERE id = ? "); foreach ($_POST['extid'] as $id) { $reset = $_POST['reset'][$id] ?? 0; // if checkbox wasn't posted then it is "0" $updt->execute( [ $reset, $id ] ); } } // // GET EXTENSIONS DATA // $exdata = ''; $res = $db->query("SELECT id , extension , reset FROM extensions "); foreach ($res as $r) { $chk = $r['reset']==1 ? 'checked':''; $exdata .= "<tr><td>{$r['extension']}</td> <td> <input type='hidden' name='extid[]' value='{$r['id']}'> <input type='checkbox' name='reset[{$r['id']}]' value='1' $chk> </td> </tr>\n"; } ?> <html> <head> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>test</title> </head> <body> <h1>Extensions</h1> <form method='post'> <table> <tr><th>Extension</th><th>Reset</th></tr> <?=$exdata?> </table> <input type='submit' name='btnSub' value='Submit'> </form> </body> </html> Edited March 5, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565016 Share on other sites More sharing options...
elentz Posted March 5, 2019 Author Share Posted March 5, 2019 Thank You Barand! Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565041 Share on other sites More sharing options...
elentz Posted March 6, 2019 Author Share Posted March 6, 2019 I have the script retrieving the info from the table so the connection is correct. The submit doesn't update the table. In the logs I am getting this: mysqli_stmt::execute() expects exactly 0 parameters, 1 given on line 20 $updt->execute( [ $reset, $id ] ); I also have to comment out this line in order for the page to work as above $reset = $_POST['reset'][$id] ?? 0; // if checkbox wasn't posted then it is "0" Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565068 Share on other sites More sharing options...
Barand Posted March 6, 2019 Share Posted March 6, 2019 The script is using PDO, not mysqli "??" is a PHPv7 operator Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565069 Share on other sites More sharing options...
elentz Posted March 7, 2019 Author Share Posted March 7, 2019 Hmm, My system is using php version 5.6 I cannot change it either. Would that be the reason that the http error log throws that error? Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565071 Share on other sites More sharing options...
Barand Posted March 7, 2019 Share Posted March 7, 2019 v5.6 equivalent would be $reset = isset($_POST['reset'][$id]) ? $_POST['reset'][$id] : 0; Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565077 Share on other sites More sharing options...
elentz Posted March 7, 2019 Author Share Posted March 7, 2019 got it, no errors now.. I didn't correctly connect to Mysql either. Had to fix that. No update to the DB yet It reads the table contents. Just to show what I am working with, mostly from barand. I do get the echo connected successfully Thanks // Create connection try { $db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); }if ($_SERVER['REQUEST_METHOD']=='POST') { $updt = $db->prepare("UPDATE extensions SET reset = ? WHERE id = ? "); foreach ($_POST['extid'] as $id) { //$reset = isset($_POST['reset'][$id]) ? $_POST['reset'][$id] : 0; // if checkbox wasn't posted then it is "0" $updt->execute; } } // // GET EXTENSIONS DATA // // Check connection if ($db->connect_error) { die("Connection failed: " . $db->connect_error); } $exdata = ''; $res = $db->query("SELECT id , extension , reset FROM extensions "); foreach ($res as $r) { $chk = $r['reset']==1 ? 'checked':''; $exdata .= "<tr><td>{$r['extension']}</td> <td> <input type='hidden' name='extid[]' value='{$r['id']}'> <input type='checkbox' name='reset[{$r['id']}]' value='1' $chk> </td> </tr>\n"; } ?> <html> <head> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>Reboot Phones</title> </head> <body> <h1>Extensions</h1> <form method='post'> <table> <tr><th>Extension</th><th>Reset</th></tr> <?=$exdata?> </table> <input type='submit' name='btnSub' value='Submit'> </form> <button type="button"><a href="restart.php">Click to reboot the phones</a></button> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/308411-tutorial-suggestion/#findComment-1565083 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.