Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help inserting into MySQL database

Status
Not open for further replies.

shawndb

Technical User
Jul 4, 2006
3
US
What's wrong with this? I'm not able to add anything to the database via the form. I'm a newbie so be easy.


Form code
Code:
<form action="submit.php"  method="post" >
    <td width="34%"><span class="style6">Company name: </span></td>
    <td width="66%"><label>
      <input name="company" type="text" class="style6" size="35" maxlength="35" />
    </label></td>
  </tr>
  <tr>
    <td class="style6">Address:</td>
    <td><input name="street" type="text" class="style6" size="35" maxlength="35" /></td>
  </tr>
  <tr>
    <td class="style6">City:</td>
    <td><input name="city" type="text" class="style6" size="35" maxlength="35" /></td>
  </tr>
  <tr>
    <td class="style6">State:</td>
    <td><input name="state" type="text" class="style6" size="35" maxlength="35" /></td>
  </tr>
  <tr>
    <td class="style6">Zip:</td>
    <td><input name="zip" type="text" class="style6" size="35" maxlength="35" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="style6">Phone:</td>
    <td><input name="phone" type="text" class="style6"  size="35" maxlength="35" /></td>
  </tr>
  <tr>
    <td class="style6">Website:</td>
    <td><input name="web" type="text" class="style6"  size="35" maxlength="35"/></td>
  </tr>
  
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="style6">Latitude:</td>
    <td><input name="lat" type="text" class="style6" size="35" maxlength="35" /></td>
  </tr>
  <tr>
    <td class="style6">Longitude:</td>
    <td><input name="long" type="text" class="style6" size="35" maxlength="35" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
      <input type="submit" name="submit"  />
    </div></td>
     </form>

php code
Code:
<?php

$dbh = mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database");

$sql="INSERT INTO database_name (lat,long,company,street,city,state,zip,phone,web) 
VALUES ('$lat', '$long', '$company', '$street', '$city', '$state', '$zip', '$phone', '$web')";

$result = mysql_query($sql); 


header("Location: [URL unfurl="true"]http://www.yahoo.com");[/URL]

exit();

?>
 
How does it not work? Does MySQL return an error? You can check that by examining the return value of mysql_query, or a mysql_error call.
 
Might be a typo or not, but this line:

$sql="INSERT INTO database_name ...

should probably be INSERT INTO [red]table_name [red]...

The table to which you wish to insert has to be named there, not the DB.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita actually it is the table name and not database name.

TonyGroves, no error or anything it just add to the db.

Has my head boggled though.

 
Another thing I've noticed is that you don't specify where variables such as $company , $street , $city etc.. come from in your insert statement.

On old versions of PHP the directive "register_globals" was set to On by default this allowed form element names, to be automatically converted to variables for use in the PHP script.

This is no longer the case in newer versions of PHP. To access form elements you have to make use of the superglobal variable $_POST if yout form method is post or $_GET if it is get.

For moe info read this FAQ: faq434-2999.

In any case try changing your variables from $company for example to $_POST['company']. I would also echo out the query before executing it, just to be sure that it is as it is supposd to . echo $sql;



Code:




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
try running your query directly in mysql (after substituting the php variables)

this will tell you if there's actually a mysql error or whether you should be in the php forum ;-)

r937.com | rudy.ca
 
r937 the query works perfectly if I run the sql statement in phpmyadmin.


 
try adding the mysql_error function to mysql_query and see if it comes back with any errors.

Code:
$result = mysql_query($sql)[red]or die(mysql_error())[/red];

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top