Re: Error Querying Database

From: Date: Wed, 15 Dec 2010 20:35:35 +0000
Subject: Re: Error Querying Database
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-310057@lists.php.net to get a copy of this message
On Wed, Dec 15, 2010 at 3:11 PM, Gary <gpaul@paulgdesigns.com> wrote: > > Sent: Wednesday, December 15, 2010 2:44 PM > Subject: Re: [PHP] Error Querying Database > >>> > On Wed, 2010-12-15 at 13:42 -0500, Gary wrote: >>> >> I cant seem to get this to connect.  This is to my local testing >>> >> server, >>> >> which is on, so we need not worry that I have posted the UN/PW. >>> >> >>> >> This is a duplicate of a script I have used countless times and it >>> >> worked. >>> >> The error message is 'Error querying database.' >>> >> >>> >> Some one point out the error of my ways? >>> >> >>> >> Gary >>> >> >>> >> >>> >> <form action="<?php echo $_SERVER["PHP_SELF"]; >>> >> ?>" method="post"> >>> >> <tr> >>> >> <td> >>> >> <label>Name of Beer</label></td><td><input >>> >> name="beername" type="text" >>> >> /> >>> >> </td> >>> >> </tr> >>> >> <tr> >>> >> <td> >>> >> <label>Maker of Beer</label></td><td><input >>> >> name="manu" type="text" /> >>> >> </td> >>> >> </tr> >>> >> <tr> >>> >> <td> >>> >> <label>Type of Beer</label></td> >>> >> <td><select name="type" size="1" >>> >> id="type"> >>> >>   <option>Imported</option> >>> >>   <option>Domestic</option> >>> >>   <option>Craft</option> >>> >>   <option>Light</option> >>> >> </select> >>> >> <!--<select name="avail" size="1" >>> >> id="avail"> >>> >>   <option>Available</option> >>> >>   <option>Sold</option> >>> >> </select>--> >>> >> </td> >>> >> </tr> >>> >> <tr> >>> >> <td><label>Sold in</label> >>> >> </td><td><input type="checkbox" >>> >> name="singles" value="Yes" /> >>> >> Singles<br >>> >> /> >>> >> <input type="checkbox" name="six" value="Yes" >>> >> /> Six Packs <br /> >>> >> <input type="checkbox" name="can" value="Yes" >>> >> /> Cans<br /> >>> >> <input type="checkbox" name="bottles" >>> >> value="Yes" /> Bottles <br /> >>> >> <input type="checkbox" name="tap" value="Yes" >>> >> /> Draft <br /> >>> >> <tr> >>> >> <td> >>> >> <label>Size</label></td><td><input >>> >> name="size" type="text" /> >>> >> </td></tr> >>> >> <tr><td> >>> >> <label>Description</label></td><td><textarea >>> >> name="desc" cols="40" >>> >> rows="5"></textarea> >>> >> </td></tr> >>> >> <tr><td> >>> >> <input name="submit" type="submit" >>> >> value="Submit" /></td></tr> >>> >> </form> >>> >> </table> >>> >> </div> >>> >> <div id="list"> >>> >> <?php >>> >> $beername = $_POST['beername']; >>> >> $manu = $_POST['manu']; >>> >> $type = $_POST['type']; >>> >> $singles = $_POST['singles']; >>> >> $six = $_POST['six']; >>> >> $can = $_POST['can']; >>> >> $bottles = $_POST['bottles']; >>> >> $tap = $_POST['tap']; >>> >> $size = $_POST['size']; >>> >> $desc = $_POST['desc']; >>> >> $ip= $_SERVER['REMOTE_ADDR']; >>> >> >>> >> $dbc = >>> >> mysqli_connect('localhost','root','','rr')or die('Error >>> >> connecting >>> >> with MySQL Database'); >>> >> >>> >> $query = "INSERT INTO beer (beername, manu, type, singles, six, can, >>> >> bottles, tap, size, desc, ip )"." VALUES ('$beername', >>> >> '$manu', >>> >> '$type', >>> >> '$singles', '$six', '$can', >>> >> '$bottles', '$tap', '$size', '$desc', >>> >> '$ip' )"; >>> >> >>> >> $result = mysqli_query($dbc, $query) >>> >> or die('Error querying database.'); >>> >> >>> >> >>> >> mysqli_close($dbc); >>> >> >>> >> >>> >> >>> >> -- >>> >> Gary >>> > >>> > >>> > Read Ash's reply...   but basically, you're running the query with POST >>> > variables, and inserting them on page display as well as on form >>> > submit. >>> > >>> > can you ensure that you can connect from the command line? >>> > >>> > >>> > if you may take some criticism, you should rethink your database >>> > design, >>> > as well as the page flow/design... you should either post the form to a >>> > new page, or if it is back to itself, you should check to see that you >>> > have in fact posted it before just blindly inserting into the database >>> > (as currently, every time you view the page, you will insert into the >>> > database, even if completely empty values). >>> > >>> >>> Steve >>> >>> Thank you for your reply. >>> >>> I did not see a reply from Ashley, but I would love to read it. >>> >>> I always welcome criticism, however this form is for the owner of a bar >>> where he will inputing his list of beer that he sells.  The rest of the >>> code >>> that is not there is I will have the list then echo to screen below the >>> form.  This is an internal list only, no customers will be seeing >>> it....if >>> that makes any difference to your suggestion. >>> >>> On your one point >>> >>> <<(as currently, every time you view the page, you will insert into the >>> database, even if completely empty values).>> >>> >>> Is this always the case when you process a form onto itself?  Or is there >>> a >>> fix? >>> >>> I did just create a new page, inserted the script onto it, and got the >>> same >>> error message. >>> >>> Again, thank you for your help. >>> >>> Gary >> >> >> Gary >> >> the line: >> <input name="submit" type="submit" value="Submit" /> >> >> is the submit part... if you encapsulate the DB part of the code, >> within: >> if($_POST['submit'] == 'Submit') >> { >>    # do db stuff in here and value sanitizing >> } >> >> basically, what that does is the submit button is name $_POST['submit'] >> and has the value "Submit" (the 'value' of the button) which will not >> be >> set, until you submit the form.   Personally, I do it another way, but >> but is how most people check to see if a form is submitted (i think?) >> >> but it seems as if your issue stems from the lack of being able to >> connect to the database itself, so either your login credentials are >> wrong, or you dont have the mysqli connector enabled with your php in >> your development box. >> >> check your phpinfo() to ensure it is enabled. >> >> Steve >> > Steve > > Again thank you, here is my phpini information, so it would seem mysqli is > enabled > > >      MysqlI Support enabled >      Client API library version  5.1.41 >      Active Persistent Links  0 >      Inactive Persistent Links  0 >      Active Links  13 >      Client API header version  5.1.41 >      MYSQLI_SOCKET  MySQL > >      Directive Local Value Master Value >      mysqli.allow_local_infile On On >      mysqli.allow_persistent On On >      mysqli.default_host no value no value >      mysqli.default_port 3306 3306 >      mysqli.default_pw no value no value >      mysqli.default_socket MySQL MySQL >      mysqli.default_user no value no value >      mysqli.max_links Unlimited Unlimited >      mysqli.max_persistent Unlimited Unlimited >      mysqli.reconnect Off Off > > > > > So you are saying that if I (preferably on a form that is working), this > will stop the double entries? > >  if($_POST['submit'] == 'Submit') >  { >    $dbc = > mysqli_connect('localhost','root','','rr')or die('Error > connecting with MySQL Database MySQL said: '.mysql_error()); > > $query = "INSERT INTO beer (beername, manu, type, singles, six, can, > bottles, tap, size, desc, ip )VALUES ('$beername', '$manu', > '$type', > '$singles', '$six', '$can', '$bottles', > '$tap', '$size', '$desc', '$ip' )"; > > $result = mysqli_query($dbc, $query) > or die('Error querying database.  MySQL said: '.mysql_error()); >  } > > Thank you > > Gary > > PS. Steve, sorry, but it appears I hit the reply instead of reply to group, > I was not intending to send you an email direct.. > > Gary > > > __________ Information from ESET Smart Security, version of virus signature > database 5706 (20101215) __________ > > The message was checked by ESET Smart Security. > > http://www.eset.com > > > > > > __________ Information from ESET Smart Security, version of virus signature database 5706 > (20101215) __________ > > The message was checked by ESET Smart Security. > > http://www.eset.com > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > FWIW I tend to break things up into function If(isset($_POST['submit'])){ processForm(); }else{ showForm($data, $errors); } function showForm($data, $errors) { //for code goes here } function processForm() { // form processing code goes in here } Then you shouldn't have that problem as much since you can test for the submission and or data being duplicated (say if beer name is a primary key) Then you can pass back the data and any error messages back to the form for display / adjustment -- Bastien Cat, the other other white meat

« previous php.general (#310057) next »