Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding database entries with PHP and mSQL

Posted on 2000-03-03
3
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
sereda earned 400 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

670 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