Solved

Adding database entries with PHP and mSQL

Posted on 2000-03-03
3
185 Views
Last Modified: 2009-07-29
(Note: This is mSQL, rather than MySQL)..


I am getting SOOOOoooo depressed about this, it's not true!!!

All I want to do is to add an entry to a table with data entered into a form. The exact coding is below, but what happenes is that the "Submit" button is recognised (i.e. the "Data entered.." line comes up), but nothing is actually added to the table.

Please, please, please can somebody help???

Anyway, here's the exact code I'm trying to use..

<form>
<table border=0>
<tr>
<td>Country:</td>
<td><input type=text name=country size=20></td>
</tr>
<tr>
<td>Port:</td>
<td><input type=text name=port size=20></td>
</tr>
<tr>
<td>Country ID:</td>
<td><input type=text name=country_id size=20></td>
</tr>
<tr>
<td align=center><input type=submit name="submit" value="Add destination"></td>
<td align=center><input type=reset value="Cancel"></td>
</tr>
</table>
</form>
</center>

<?php
if ($submit=="Add destination") {

msql_connect ("");


$sql="INSERT INTO countries VALUES ($country,$port,$country_id)";

$result=msql ("1sitetest",$sql);

echo "Information added. Refresh this page to update table above\n";
}
?>


</form>
0
Comment
Question by:PoRL
3 Comments
 
LVL 1

Accepted Solution

by:
sereda earned 100 total points
ID: 2580018
You need to specify
<form action="<?echo $PHP_SELF;?>" method="get">

instead of simply <form>.

Also, advice - do not test value of $submit button. For example, if user enters values then presses ENTER button, then $submit will be empty but the form is still submitted.
Instead, use something like

<input type="hidden" name="formaction" value="submit">

<?
  if ($formaction == "submit") {
....
  }
?>

0
 
LVL 2

Expert Comment

by:maxkir
ID: 2581536
Good comment from sereda :-)

My addition - I'd rewrite your database code in the following way:
Note die() calls, $db connection identifier, 's in $sql statement.

<?php
 if ($submit=="Add destination") {

   // Get link identifier
   $db = msql_connect () or die("Unable to connect to database");

    $sql="INSERT INTO countries VALUES ('$country','$port','$country_id')";

   // Use link identifier in request  
   msql ($db, $sql) or die ("Error inserting data to database. Query: $sql");

    echo "Information added. Refresh this page to update table above\n";
}
?>
0
 

Author Comment

by:PoRL
ID: 2581925
Hmmmm... I thought I'd accepted this answer earlier on!?!

Basically, just after I posted the original question, I got a phone call out of the blue from an old friend in South Africa, and he guided me to where I wanted to be.

I felt the point about the Submit button was worth all the points, though!!!

Thanks for the help...

Paul
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question