Solved

Adding database entries with PHP and mSQL

Posted on 2000-03-03
3
182 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
creating 2 user ID's 5 25
Phone Dialer 5 36
Why does my array not "dump?" 5 16
showing numeric numbers 2 8
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now