Josef E Posted November 20, 2009 Share Posted November 20, 2009 I bought a second hand copy of "Build your own Database Driven Website using PHP and MySQL" by Kevin Yank I'm new to PHP so bear with me, I'll try my best to say what my problem is. I never got the code with it (as it was second hand). I've got to Chapter 6 and I am now stuck. You have to build a basic CMS. I'm trying to update information that is already in the db using a form. I cant get the checkboxes to check with the data, I got text box and select box to show the data from tthe db Here is the code I've done so far for the editjoke page. My problem is the categories section of code....I have commented what I tried to do but it didnt work. I hope some one can help me. Many thanks <!-- editjoke.php --> <html> <head> <title> Edit Joke </title> </head> <body> <?php // gets info that was sent from jokelist.php $id = $_REQUEST['id']; $jokeText = htmlspecialchars($_REQUEST['jokeText']); $eaid = htmlspecialchars($_REQUEST['aid']); $ecid= htmlspecialchars($_REQUEST['cid']); // make sure info has been sent echo "$id<br />$jokeText<br />$eaid<br />$ecid<br /><br />"; if (isset($_POST['submit'])): // An edit to a joke has been entered // using the form below. $dbcnx = mysql_connect('localhost', 'root', ''); mysql_select_db('jokes'); $aid = $_POST['aid']; $joketext = $_POST['joketext']; $cats = $_POST['cats']; if ($aid == '') { die('<p>You must choose an author ' . 'for this joke. Click "Back" ' . 'and try again.</p>'); } $sql = "UPDATE Jokes SET JokeText='$joketext', JokeDate=CURDATE(), AID='$aid' WHERE ID='$id'"; if (@mysql_query($sql)) { echo('<p>Joke amended</p>'); } else { echo('<p>Error editing joke: ' . mysql_error() . '</p>'); } // mysql_insert_id returns the number assigned to // the last-entered entry by the AUTO_INCREMENT // feature in MySQL $jid = mysql_insert_id(); // SET up an array for the categories that have been picked if ($cats == '') { $cats=array(); } $numCats=0; foreach ($cats as $catID) { $sql="INSERT IGNORE INTO JokeLookup SET JID=$jid, CID=$catID"; $ok=@mysql_query($sql); if ($ok) { $numCats=$numCats+1; } else { echo("<p>Error inserting joke into category $catID: " . mysql_error() . '</p>'); } } ?> <p><a href="jokes.php">Return to Joke Search</a></p> <?php else: // Allow the user to amend a new joke $dbcnx = mysql_connect('localhost', 'root', ''); mysql_select_db('jokes'); $authors=mysql_query('SELECT ID, Name FROM Authors'); $cats=mysql_query('SELECT ID, Name FROM Categories'); // to get cat id's for the joke $chcats=mysql_query('SELECT JID, CID FROM jokelookup WHERE JID="$id"'); $chcat = mysql_fetch_array($chcats); ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <p>Amend the seleceted joke:<br /> <textarea name="joketext" rows="5" cols="45" wrap><?=$jokeText?> </textarea></p> <p>Author: <select name="aid" size="1"> <option selected value="author">Select One</option> <option value="">---------</option> <?php while ($author = mysql_fetch_array($authors)) { $aid = $author['ID']; $aname = htmlspecialchars($author['Name']); if ($aid == $eaid) { // Select the author of the joke echo("<option value='$aid' selected>$aname</option>\n"); } else { //echo ("ID is empty"); echo("<option value='$aid'>$aname</option>\n"); } } ?> </select></p> <p>Place in categories:<br /> <?php while ($cat = mysql_fetch_array($cats)) { $cid = $cat['ID']; $cname = htmlspecialchars($cat['Name']); // need to see if cat is in the database link to joke // for ($i=0;$i<count($chcat);$i++) { // if ($chcat[$i] == $cid) { // echo("<input type='checkbox' checked='checked' name='cats[]'"); // } // else { // echo("<input type='checkbox' name='cats[]' value='$cid' />" . //"$cname<br />\n"); // } //} // if(); // echo("<input type='checkbox' checked='checked' name='cats[]' // value='$cid' />" . "$cname<br />\n"); // else { echo("<input type='checkbox' name='cats[]' value='$cid' />" . "$cname<br />\n"); //} }?></p> <input type="hidden" name="id" value="<?=$id?>" /> <input type="submit" name="submit" value="SUBMIT" /></p> </form> <p><a href="jokes.php">Return to Jokes Search</a></p> <p align="center"><a href="admin.html">Return to Front Page</a> </p> <?php endif; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/182262-cant-get-the-checkbox-to-check/ Share on other sites More sharing options...
rarebit Posted November 20, 2009 Share Posted November 20, 2009 1. This should be in the HTML forum I think... 2. Using code tags to surround code make helping easier... Your checkbox code looks correct, so... Have you looked at the page source once it's generated to see if any of your variables have introduced any erroneous characters which are exiting quotes prematurely? (even though I see your using htmlspecialchars()) p.s. it looks like you can dl the source code from the publishers site: http://www.sitepoint.com/books/phpmysql4/?historicredirect=phpmysql1 Link to comment https://forums.phpfreaks.com/topic/182262-cant-get-the-checkbox-to-check/#findComment-961948 Share on other sites More sharing options...
siric Posted November 20, 2009 Share Posted November 20, 2009 I would include print statements on $chcat[$i] and $cid before the comparision to see if there are any instances where the same. If not, then backtrack with print statement, until you find where the problem lies. Link to comment https://forums.phpfreaks.com/topic/182262-cant-get-the-checkbox-to-check/#findComment-962149 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.