Trying to connect to the database can involve setting up your database. Make
sure that you have a valid login/password that is recognized by MySQL.
Please keep in mind that MySQL works on permission by hosts. So your host IP
must be matched with the username/password on the database for a successful
authentication. One way to know that you can connect successfully to your
remote database is to actually test it. Download MySQL Workbench from
Mysql.com and then try to connect to remote from the same host that your php
application is sitting at. If it works, thumbs up. If it does not then you
have a permission issue there. Add your username/host appropriately.

If you can connect without a hitch then you are doing something wrong on
your code. Use mysql_connect(), mysql_select_db() and then send an statement
and use the resource to see if it returns TRUE or FALSE. At this point, on
FALSE it means that you have a bad written statement.

There is so much that can go wrong. Debug step by step.

Ravi.


On Thu, Dec 16, 2010 at 9:26 PM, Phred White <phpl...@planetphred.com>wrote:

> It seems like there are several questions emerging, but ...
>
> Try echoing your query to the page by putting echo $query in your code
> before you call mysql, then copy it and run it in phpmyadmin. If it runs
> then you know your problem is somewhere else like the connection. This can
> really help you find typos that can cause mysterious results.
>
> If you want to use the same page to process the form (my preference) then
> put a hidden field in your form like:
>
>    <input type="hidden" name="phpaction" id="phpaction" value="process" />
>
> and wrap the form processing code like so:
>
> if (isset($_POST['phpaction'])) {
>        //process submitted form data
> } else {
>        //processing for initial form entry
> }
>
> When the form is initially loaded it will ignore the first part
> There are a 1000 ways to do this, but this is pretty straightforward.
>
> On Dec 15, 2010, at 1:34 PM, Gary wrote:
>
> >
> > "Steve Staples" <sstap...@mnsi.net> wrote in message
> > news:1292440837.5460.8.ca...@webdev01...
> >> 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
> >
> >
> >
> > __________ 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
> >
>
>

Reply via email to