PHP - Storing An Unchecked Checkbox Value In Mysql
Hi
I have an array of checkboxes whose values if checked can be updated in mysql. The code I have below accomplishes that just fine. On my form I have: print "<form method='post' action='update.php'>\n"; /////// mysQL query here $myID = $itemRow['myID']; $chk = $itemRow['item_shipped'] == 1 ? 'checked' : ''; echo "<input type=checkbox name=cbox[] value='{$itemRow['myID']}' $chk>"; echo"</form>"; The above code displays various items with a checkbox next to them. So if that checkbox is checked the code below stores that in mysql. On my update.php page I have: if(sizeof($_POST['cbox'])) { //means if at least one check box is selected foreach($_POST['cbox'] AS $id) { mysql_query("UPDATE om SET item_shipped ='1' WHERE myID=$id"); } //end foreach } //end IF The problem is though i can check a checkbox and store that value '1' in mysql, I have no way of unchecking an already checked checkbox and storing the '0' in mysql. Can anyone help? Thanks in advance Similar TutorialsHi, I just cannot seem to get this to work, have been testing and searching all morning and not getting anywhere. I have a form with a series of checkboxes which populates the MySqL database, this works fine. Trying to print the results from the form if the row is empty show an unchecked checkbox, if there is a value show a checked checkbox. I have tested my if statement and can echo correctly wether empty or not empty, just cannot get the checkbox to be unticked if there is no value in the row Code: [Select] while ($row = mysql_fetch_array($qry, MYSQL_BOTH)) { // show total fields for debugging // $totalfields = mysql_num_fields($qry); // print ($totalfields) ; // if ( ! empty ( $row ['acid_wash'] ['neutralise'] ['clean'] ) ) { if ( ! empty ( $row ['clean'] ) ) { ///if it's 1 (true) then we add 'checked' to a variable we will use in the input //to cause it to be checked $checked = 'checked'; // test if row has data echo "full"; }else{ //if the value is 0 (false) we set the variable to be empty, not checked $checked = ''; // test for no value echo "empty" ; } To Display Result: <tr> <td>High Pressure Clean <input style='font-size:10px;' name='clean[]' type='checkbox' checked='$checked'></td> </tr> The empty and full test echos do seem to work, showing me the if statement works. The problem is even when the row is empty the checkbox shows checked.... Thanks. Script:
<?php // For example, the entries in the table. $entry1 = "text1"; $entry2 = "text2"; $entry3 = "text3"; // For example, the ID numbers in the table. $value1 = "140"; $value2 = "141"; $value3 = "142"; echo "<form method='POST' action='" . $_SERVER['PHP_SELF'] . "'>"; echo "<input type='checkbox' name='hashtag_modify_checkbox[]' value='" . $value1 . "' />"; echo "Modify"; echo "<input type='text' name='hashtag_name[]' value='" . $entry1 . "' />"; echo "<input type='checkbox' name='hashtag_modify_checkbox[]' value='" . $value2 . "' />"; echo "Modify"; echo "<input type='text' name='hashtag_name[]' value='" . $entry2 . "' />"; echo "<input type='checkbox' name='hashtag_modify_checkbox[]' value='" . $value3 . "' />"; echo "Modify"; echo "<input type='text' name='hashtag_name[]' value='" . $entry3 . "' />"; echo "<input type='submit' name='submit' />"; echo "</form>"; // Here, only store those hashtag names where the "modify" checkbox is checked. ?>This is used for the admin panel. It is a form where the input fields hold the text ("hashtags") already inside of them. Basically, one would have to check the "modify checkbox" to modify a hashtag, then only the modified hashtags should get stored into an array. How to have those hashtags stored in the array with the checked checkbox as the factor? The suggestions are much appreciated. Edited by glassfish, 23 October 2014 - 10:06 AM. I need to find a way to store a password for connection to a remote database. I'm writing a program that will create a database on whatever server the user is using. Obviously, they will have to provide their username and password in order to create the database and have access to it. I need my program to get the information once (when they use the setup utility), then be able to store it so they can automatically connect to the database whenever my program is used. I plan using a php file for storage and include() to gain access to the info. I just don't have an idea for getting the information into the php file in the first place. Thanks for any ideas! Hey, I am a bit of a noob at php/mysql and trying to be clever(emphasis on trying). I want to pull some php/html code from a database in mysql, whilst it does display the html, the php does not. Can this even be done? Is it going to cause me issues later on? Code: [Select] [php] <?php $query = "SELECT * FROM pages"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)){ $body = $row["body"]; echo $body; }?> [/php] Any help would really be appreciated. Ell I have a database with all users.. Within that database all users have an id. How would I make it so users can "friend" other users? Would I need to make a new table for that? Im sorry for the dumb question.. I havent dealt with php or mysql in about two years because I was busy getting engaged and all.. But now that I have time again I think im gonna take up one of my old projects. Granted I probably could have answered this question myself back then but now im finding some trouble taking up programming again. :\ Here is my code... Code: [Select] <?php mysql_connect("localhost", "user", "pass")or die("cannot connect"); mysql_select_db("database")or die("cannot select DB"); $myemail = mysql_real_escape_string($_POST['myemail']); $mypassword = mysql_real_escape_string($_POST['mypassword']); $mypassword = md5($mypassword); $myemail = stripslashes($myemail); $mypassword = stripslashes($mypassword); $sql="SELECT * FROM users WHERE email='$myemail' and password='$mypassword'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1){ session_start(); $_SESSION["myemail"]= "$myemail"; header("location:home.php"); } else { header("location:fail.php"); } ?> What can I do to this code so that it will also store first name from the database inside a session? Hey, i need help storing an image in my database via the URL(image location) at the moment my php code is storing the image in a folder on the directory called upload. here is the code: <?php // Where the file is going to be placed $target_path = "upload /"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $target_path = "upload/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> Click <a href="products.php">HERE</a> to go back to form if someone could help me i'd be very grateful I have insert image using this following code but i want to know where it is storing i want file path of the imahe what i have inserted Code: [Select] <html> <head><title>File Insert</title></head> <body> <h3>Please Choose a File and click Submit</h3> <form enctype="multipart/form-data" action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <input name="userfile" type="file" /> <input type="submit" value="Submit" /> </form> </body> <?php // check if a file was submitted if(!isset($_FILES['userfile'])) { echo '<p>Please select a file</p>'; } else { try { upload(); //this will upload your image echo '<p>Thank you for submitting</p>'; //Message after uploading image } catch(Exception $e) { echo $e->getMessage(); echo 'Sorry, could not upload file'; } } // the upload function function upload(){ include "file_constants.php"; $maxsize = $_POST['MAX_FILE_SIZE']; if(is_uploaded_file($_FILES['userfile']['tmp_name'])) { // check the file is less than the maximum file size if( $_FILES['userfile']['size'] < $maxsize) { // prepare the image for insertion $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name'])); // put the image in the db... // database connection mysql_connect($host, $user, $pass) OR DIE (mysql_error()); // select the db mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error()); // our sql query $sql = "INSERT INTO test_image (image, name) VALUES ('{$imgData}', '{$_FILES['userfile']['name']}');"; // insert the image mysql_query($sql) or die("Error in Query: " . mysql_error()); } } else { // if the file is not less than the maximum allowed, print an error echo '<div>File exceeds the Maximum File limit</div> <div>Maximum File limit is '.$maxsize.'</div> <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size']. ' bytes</div> <hr />'; } } ?> </html> I've got an array like this: $firstquarter = array('January', 'February', 'March'); Then I'm adding it to a MySQL DB as follows: $insert = "INSERT INTO sometable (months) VALUES ('$firstquarter') "; $insertresult = mysql_query($insert) or die ('Error in Insert query: ' . mysql_error()); When I use PHPMyAdmin I see a this value stored in the DB: 'Array' Can I retrieve the values, by index, from this database field?? I am currently looking to insert images into a database and then display them in php. I am thinking of storing the images as a VARCHAR data type but what would be a more suitable type? I need some help with this. A user fills out a form, one of the fields is a zip code field. I need to retrieve that value from MySQL store as a session var and set that value as a variable to use with a weather display API. The ID is being stored from the form page. Here is what I have so far, after the values are submitted into the DB. <?php session_start(); $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Profile WHERE id='{$_SESSION['id']}"); while ($row = mysql_fetch_assoc($result)) { $_SESSION['id'] = $row['id']; $_SESSION['zip'] = $row['zip']; } mysql_close($con); ?> and then for the weather API, I need to set the stored variable to something $zip = 'stored zip code value'; Hello! I am designing a user registration form which has some radio buttons for people to select. I am confused as to how I should store the value selected by the user into the database. Here is my code, just in case. <li id="li_2" > <label class="description" for="element_2">I am a </label> <span> <input id="element_2_1" name="element_2" class="element radio" type="radio" value="1" /> <label class="choice" for="element_2_1">Student</label> <input id="element_2_2" name="element_2" class="element radio" type="radio" value="2" /> <label class="choice" for="element_2_2">Staff</label> <input id="element_2_3" name="element_2" class="element radio" type="radio" value="3" /> <label class="choice" for="element_2_3">Member</label> <input id="element_2_4" name="element_2" class="element radio" type="radio" value="4" /> <label class="choice" for="element_2_4">Other</label> </span> </li> I am working on a kind of CMS for my own website which no one else will be using but me as a way of improving my php skills, and am having problems with retrieving data from the database that holds both text and php code. I have searched the web and found that i should be using eval() for the code to be executed before it is send to the browser but cannot get it to work and can't find my mistake(s). the php code will always be the same, and is supposed to retrieve the id number of a page to use in a link (and works fine when tested by loading the code directly without retrieving it from the database) this is an example of data stored in the database Code: [Select] The <a href="page_builder.php?id=<?php echo $page->id('mines_DwarvenMines') ?>">Dwarven Mines</a> have a great selection of Ores,... Of course when I leave it like this, hovering over the link in my page will show exactly that and lead to nowhere Code: [Select] localhost/page_builder.php?id=<?php echo $page->id('mines_DwarvenMines') ?> Most of these links appear in tips given at the end of the page and are processed as followed Code: [Select] $questtips = $quest->getQuestTips(); $tips = ""; if ($questtips == "none") { $tips = "/"; } else { foreach($questtips as $tip) { $tips .= "<li>"; $tips .= $tip->getTip(); $tips .= "</li>"; } } and finally put on screen by the presentation layer as followed Code: [Select] <h2>Tips & Extra Info</h2> <div class="tipsList"> <ul> <?php echo $tips ?> </ul> </div> I have tried all sorts to get the code to be executed when retrieved from the database before being send to the browser so that this particular link would say "localhost/page_builder.php?id=57" but I cannot get it to work, though I suspect it is fairly easy. I suspect I would have to store the data in a different format in the database? And how exactly do I use the eval() function in my case? Could someone please adjust my code so that it does work? Thanks Hello everyone,
I have a card database and everything works perfectly besides one thing..
I can't store
'in my database via my form, i however do can store them into the database via PHPMyAdmin. I dont know what i've been doing wrong and it really bothers me. If any of you guys could help me out. Here's all the code you would need to find the issue. This is the form file <!doctype html> <html> <head> <meta charset="utf-8"> <title>Edit Page</title> </head> <body> <h1 align="center"> Add Cards</h1> <form action="insert.php" method="POST"> <input type="text" name="name" placeholder="Name" /> <input type="text" name="color" placeholder="Color" /> <input type="text" name="type" placeholder="Type" /> <input type="text" name="subtype" placeholder="Sub Type" /> <input type="text" name="power" placeholder="Power" /> <input type="text" name="toughness" placeholder="Toughness" /> <br> <input type="text" name="manacost" placeholder="Converted Mana Cost" /> <input type="text" name="rarity" placeholder="Rarity" /> <input type="text" name="expansion" placeholder="Expansion" /> <input type="text" name="foil" placeholder="Foil" /> <input type="text" name="stock" placeholder="Stock" /> <input type="submit" value="Save" /> </form> </body> </html>This inserts it into my database. <?php ($GLOBALS["___mysqli_ston"] = mysqli_connect("", "", "", , ))or die("cannot connect"); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE e_industries"))or die("cannot select DB"); $name = $_POST['name']; $color = $_POST['color']; $type = $_POST['type']; $subtype = $_POST['subtype']; $power = $_POST['power']; $toughness = $_POST['toughness']; $manacost = $_POST['manacost']; $rarity = $_POST['rarity']; $expansion = $_POST['expansion']; $foil = $_POST['foil']; $stock = $_POST['stock']; $sql="INSERT INTO Osiris (Name, Color, Type, Subtype, Power, Toughness, Manacost, Rarity, Expansion, Foil, Stock) VALUES ('$name', '$color', '$type', '$subtype', '$power', '$toughness', '$manacost', '$rarity', '$expansion', '$foil', '$stock')"; $result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); if($result){ echo "Successful"; echo "<BR>"; echo "<a href='add.html'>Back to main page</a>"; } else { echo "ERROR"; } ?>If anyone could help me out that would be great! Edited by OsirisElKeleni, 05 October 2014 - 12:29 PM. helllo dear php-commmunity new to Ruby - i need some advices - i plan to do some requests in osm-files. (openstreetmap) Question - how can i store the results on a Database - eg mysql or - (if you prefer postgresql) - note: my favorite db - at least at the moment is mysql here the code require 'open-uri' require "net/http" require 'rexml/document' def query_overpass(object_type, left,bottom,right,top, key, value) base_url = "http://www.overpass-api.de/api/xapi?" query_string = "#{object_type}[bbox=#{left},#{bottom},#{right},#{top}][#{key}=#{value}]" url = "#{base_url}#{URI.encode(query_string)}" resp = Net::HTTP.get_response(URI.parse(url)) data = resp.body return data end overpass_result = REXML::Document.new(query_overpass("node", 7.1,51.2,7.2,51.3,"amenity","restaurant|pub|ice_cream|food_court|fast_food|cafe|biergarten|bar|bakery|steak|pasta|pizza|sushi|asia|nightclub")) overpass_result.elements.each('osm/node') {|x| if !x.elements["tag[@k='name']"].nil? print x.elements["tag[@k='name']"].attributes["v"] end print " | " if !x.elements["tag[@k='addr:postcode']"].nil? print x.elements["tag[@k='addr:postcode']"].attributes["v"] print ", " end if !x.elements["tag[@k='addr:city']"].nil? print x.elements["tag[@k='addr:city']"].attributes["v"] print ", " end if !x.elements["tag[@k='addr:street']"].nil? print x.elements["tag[@k='addr:street']"].attributes["v"] print ", " end if !x.elements["tag[@k='addr:housenumber']"].nil? print x.elements["tag[@k='addr:housenumber']"].attributes["v"] end print " | " print x.attributes["lat"] print " | " print x.attributes["lon"] print " | " if !x.elements["tag[@k='website']"].nil? print x.elements["tag[@k='website']"].attributes["v"] end print " | " if !x.elements["tag[@k='amenity']"].nil? print x.elements["tag[@k='amenity']"].attributes["v"] print " | " end puts } look forward to hear from you again - i would love to store it on a mysql - database - if possible. If you would prefer postgresql - then i would takte this one.... ;-) look forward to hear from you again - i would love to store it on a mysql - database - if possible. If you would prefer postgresql - then i would takte this one.... ;-) well - i guess that the answer to this will be the same no matter what language we are using. If the db is a sql database we need to design the database schema and create the tables in the database. The first step in accessing a db in our code is to get a connection to it. If ruby is our choice of language, a search for "ruby sql connector" will give us lots of options to read about. Well - we also can do it in PHP. What do you think!? Next, based on the schema we have designed, we need to create queries suitable for storing the data. We will likely need to consider our transactional model. Again, searching "ruby sql transactional model" will give us plenty of food for thought. Finally, we may want or need to close the connection to the database. i have a dictionary - THIS IS obviously a python dictionary an this has approx 8 000 lines with records [/CODE] $ python printer.py {'url': 'http://www.site1_com' 'cname': 'butcher', 'name': 'cheng', 'email': 'mail1@hotmail.com'} {'url': 'http://www.site2_com' 'cname': 'dilbert', 'name': 'James', 'email': 'mail2@hotmail.com'} [code=auto:0]i have a mysql-db up and runing in my opensuse there i have created a db with the fields url cname name i use the import MySQLdb i studied this documentation he http://stackoverflow...abase-in-python but i think this goes a bit over my head. - well how can i get the data ( in other words the dictionary) into the database? love to hear from you greetings I created a simple search box which will query my table and match the input value to one of my columns. two of these columns store comma separated values. if i query a column other than a column which stores my csv i can see my search results. if i query a column which stores my csv i will not see results unless the search value matches the first value within the column. how would i be able to get say the second or third or forth value. here is the code i am using to query the table any help would be appreciated thanks. Code: [Select] $q = $this->db->query("SELECT * FROM table WHERE col1 LIKE 'searchvalue'". " OR col2 LIKE 'searchvalue'". " OR col3 LIKE 'searchvalue'". " OR col4 LIKE 'searchvalue'". " OR FIND_IN_SET('searchvalue', col5) > 0 ". " OR FIND_IN_SET('searchvalue', col6) > 0"); Hey Guys,
I'm really struggling to find whats wrong with this code. The form before submit has several checkboxes as part of an array which is below
The next page then goes to this and I get the error "Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /homepages/39/d837976904/htdocs/Scheduler/add_job3.php:10 Stack trace: #0 {main} thrown in /homepages/39/d837976904/htdocs/Scheduler/add_job3.php on line 10"
<?php $process = $_GET["process"];
include "Includes/config.php";
$checkBox = implode(',', $_POST['engineers']);
if(isset($_POST['submit'])) mysql_query($query1) or die (mysql_error() ); echo "Complete"; }
?> Can anyone help with the code please Hello, I have been spending over 6 hours trying to square this away and I have looked at some tutorials from PHPFreaks... Maybe I'm missing something. Basically I rebuilding the form from bottom up and I thought some check boxes would be easier for some part of the form instead of drop-down menu. Basically this form is submitting query to MySQL: article_add.php (Basically has the form) article_insert.php (Basically insert the data to MySQL) Here is one part of the form where the check box is located: Here is article_add.php <form action="article_insert.php" method="post"> <-- OTHER INPUT BOXES --> <div class="inputfieldtb"><label for="news_cat">News Categories:</label> <? echo "<table>"; echo "<tr>"; $counter = 1; while ($nc = mysql_fetch_array($news_cats)) { $ncat_id = $nc['ncat_id']; $ncat_name = $nc['ncat_label']; echo "<td><div><input type=\"checkbox\" name=\"category[]\" value=\"$ncat_id\" ''/>$ncat_name</div></td>\n"; if (($counter % 4) == 0) { echo "</tr><tr>"; } $counter ++; } echo "</tr></table>"; echo "<br />"; ?> </div> <-- MORE FORMS --> </form> Here is article_insert.php // CONNECT TO DATABASE include("../mods/connect.php"); <--- OTHER DATA INSERT IN MYSQL --> $news_cat = mysql_real_escape_string(implode(',', $_POST['category'])); // <-- Checkbox Data <--- MORE DATA INSERT IN MYSQL --> $query = "INSERT INTO news_articles VALUES ('<--- MORE CONTENT BEING INSERT INTO MYSQL -->,'$news_cat','$news_content',<--- MORE CONTENT BEING INSERT INTO MYSQL -->'')"; mysql_query($query) or die ('Error inserting new data into the data to the database: ' . mysql_error() . ''); The error I'm getting is "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '','','3','0','','','')' at line 1"; So what am I doing wrong? Once I get this resolved, I would like to learn how to display the checkbox / updated the checkbox as well. Thank you in advance for your time and your help with this problem. |