-
<html>
-
<head>
-
<title>Add New MySQL User</title>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
-
</head>
-
<body>
-
<?
-
if(isset($_POST['add']))
-
{
-
-
include 'c:\sokkit\config.php';
-
include 'c:\sokkit\opendb.php';
-
$ip=$_SERVER['REMOTE_ADDR'];
-
$username = $_POST['username'];
-
$password = $_POST['password'];
-
$country = $_POST['mydropdown'];
-
-
$query = "INSERT INTO account (username, password, location, forumip) VALUES ('$username', '$password', '$country', '$ip')";
-
mysql_query($query) or die('Error, insert query failed');
-
-
$query = "FLUSH PRIVILEGES";
-
mysql_query($query) or die('Error, insert query failed');
-
-
include 'c:\sokkit\closedb.php';
-
echo "New MySQL user added";
-
}
-
else
-
{
-
?>
-
<form method="post">
-
<div align="left">
-
<select name="mydropdown">
-
<option value="Zimbabwe">Zimbabwe</option>
-
</select>
-
</div>
-
<table width="400" border="0" cellspacing="1" cellpadding="2">
-
<tr>
-
<td width="100">Username</td>
-
<td><input name="username" type="text" id="username"></td>
-
</tr>
-
<tr>
-
<td width="100">Password</td>
-
<td><input name="password" type="text" id="password"></td>
-
</tr>
-
<tr>
-
<td width="100"> </td>
-
<td> </td>
-
</tr>
-
<tr>
-
<td width="100"> </td>
-
<td><input name="add" type="submit" id="add" value="Add New User"></td>
-
</tr>
-
</table>
-
</form>
-
<?
-
}
-
?>
-
</body>
-
</html>
-
this is an account creation I am trying to put together. Totally newbie in php and html i am no better, so please bare with me. So far everything works good. When I submit, it writes everything including the ip in mysql database. As you can see $ip is the variable for ip address. What I am looking to do is prevent anyone from doing multiple creates from same ip. I have learned how to write to the mysql database, but not learned yet how to read. I will need to read from the "account" table and "forumip" field to see if the ip exists.
kinda like this:
select * from `account` where forumip=$ip';
So basically when someone presses submit, if ip in database already to error rather then submitting the info.
Any help in this area is greatly appreciated.
2 6677 Atli 5,058
Recognized Expert Expert
Hi.
There is a very simple way of querying the database and counting the rows it returns. In your case, if it returns more than 0 rows you would not add the new user.
Like this:
[PHP]
// Create the query and execute it
$QUERY = "SELECT field FROM table WHERE field = value";
$RESULT = mysql_query($QU ERY) or die(mysql_error ());
// Check how many rows the query returned
if(mysql_num_ro ws($RESULT) > 0) {
echo "The value already exists";
}
else {
// Put your insert code here.
}
[/PHP]
This is very simple and will do fine in your case, because your design only allows for one row to be return.
However, if you were to use this method to count, for example, every single row in your table, this would return a lot of data you dont need.
In that kind of a situation I would rather reccomend an approach like this:
[PHP]
// Create query and execute
$QUERY = "SELECT COUNT(field) AS 'count' FROM table WHERE field = value";
$RESULT = mysql_query($QU ERY) or die(mysql_error ());
// Read the firs row
$row = mysql_fetch_ass oc($RESULT);
// Check how many rows MySQL counted
if($row['count'] > 0) {
echo "value already exists";
}
else {
// Put input code here
}
[/PHP]
This way MySQL only returns one row no matter how many it counts, which will lighten the load on the server on a busy day, especially if the MySQL server is on another server.
Hope this helps.
Thanks Atli. That titbit of code worked great. :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: sathia |
last post by:
Hi, I have this table:
CREATE TABLE `osservatorio` (
`id` int(10) unsigned NOT NULL auto_increment,
`testo` varchar(255) NOT NULL default '',
`parent` int(11) default NULL,
`allegato` varchar(255) default NULL,
`descrizione` varchar(255) default NULL,
`online` enum('true','false') NOT NULL default 'true',
PRIMARY KEY (`id`),
|
by: MLH |
last post by:
A programmer developed an AMP (Apache/MySQL/PHP) application
for me. When he was done, he sent me the PHP files and the MySQL
dump file. Now, when I connect to the application on my LAN using
http://192.168.1.106/~mlh/credifree/index.php the AMP app still
thinks the data resides somewhere else. It runs fine - as long as I
leave my LAN's external internet connection up. But if I unplug my
LAN from the world, my app locks up.
Before I...
|
by: MLH |
last post by:
Using MS Access, I have attached to MySQL servers in other states
and other countries on the other side of my router. But when I use the
MySQL ODBC driver 3.51 to connect to a MySQL server on my own LAN,
the driver tells me it cannot make the connection. Here are the ODBC
driver connection parms:
Data Source Name: (free field - name my "my linux box" will do nicely)
Host/Server Name (or IP) - something like MSQLUserName@ServerName.net...
|
by: eholz1 |
last post by:
Hello Newsgroup,
I have redhat 3.0 Enterprise, and mysql (3.58.xxx more or less)
installed from a redhat rpm,
and php 4.3.9 (installed from an rpm) - I can access data from my mysql
db using php and the "pear" Db .php. I recently upgraded my Apache to
2.2 and installed (more or less) PHP 5.2 as well - neither of these
installs were from a Redhat rpm, I downloaded the tarballs, extracted,
configed, make, etc.
Apache seems to work fine...
|
by: gunimpi |
last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431
********************************************************
VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help
wanted
********************************************************
For this teeny job, please refer to:
http://feeds.reddit.com/feed/8fu/?o=25
| |
by: Bruce A. Julseth |
last post by:
I have the following code
$Host = "localhost";
$User = "Fred";
$Database = "house";
$Password = "mypw"
echo "before mysqli<br />Host: " . $Host . "<br />" . $User . "<br />" .
$Database;
|
by: George Lft |
last post by:
ok, first of all, i built my register page using dreamweaver tool which the codes haven been out of control. Now i'm thinking that turning over everything - by using this another set of codes. And these new sets of codes have overwhelmed me a bit. Here's the new code:
CREATE TABLE `users` (
`ID` int(11) NOT NULL auto_increment,
`Username` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
`Temp_pass` varchar(55)...
|
by: mike |
last post by:
hi.
trying to set up a new site with some interactivity between the pages. need
a coder to set up the initial user database where visitors create their
personal accounts. i'm a green do-it-yourselfer as far as web design goes
and definately won't be coding my own php or mysql in this lifetime. i
generate alot of questions too. can pay on a per instance basis for the
project or for answers to questions if you are cool with being a...
|
by: Nike1984 |
last post by:
I'm fairly new to Javascript and it's more of a guessing game for me...
I'm trying to build an app for Google Maps and just had some issues recently.
First off I just wanted to say that everything works fine in FF and IE. It's Chrome I'm having issues with.
I understand that Chrome is still somewhat in beta stages, so some bugs might occur. However this seems like something I might have done.
So...
I used a code that I found on Econym as...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |