Jump to content

[SOLVED] help with SQL insert (posted data) problem


markosaurus

Recommended Posts

Hi there folks,

 

First of all, sorry for my ineptitude, I'm new to SQL, although I have been using PHP for a while it is also relatively new to me, apologies in advance for the nastiness of my code.

 

I'm trying to handle form data and insert it into a table called 'users'

 

FORM CODE;

<form action="" method="POST">
<label for="name">Name</label><input name="name" type="text" />
	<label for="username">Username</label><input name="username" type="text" />
	<label for="password">Password</label><input name="password" type="text" />
	<label for="email">Email</label><input name="email" type="text" />
<input name="submit" type="submit" value="submit" class="submit" />
</form>

 

PHP;

//make query
$sqlquery="INSERT INTO users (id, username, password, role, approved, name, email, joined) VALUES (NULL , '$_POST[username]', '$_POST[password]', 'user', '0', '$name', '$_POST[email]', NOW( ));";
mysql_query($sqlquery) or die (mysql_error());

 

It has connected fine, my data doesn't seem to be posted for some reason as when I try to test this using;

 

echo $_POST['name'];

 

The code is all on one page, I'm scanning for $_POST['submit'] being set and either outputting a form or inserting data depending on whether submit is set or not.

 

I get no response, is there something very silly I've done here?  It's driving me mad.

 

Any thanks would be appreciated muchly.

 

Mark

Testing using...

 

echo $_POST['name'];

 

does nothing because you have no form filed named name.

 

Is your call to mysql_error() showing any errors? You have a variable ($name) in your query that appears from nowehere. You also ought really clean any user input prior to using it in any query, take a look at mysql_real_escape_string.

<label for="name">Name</label><input name="name" type="text" />

 

The first form field is named 'name' ???

 

I'm confused now.

 

Sorry, I see what you meant now, I have changed the query to;

 

	$sqlquery="INSERT INTO users VALUES (NULL , '$_POST[username]', '$_POST[password]', 'user', '0', '$_POST[name]', '$_POST[email]', NOW( ));";
$result=mysql_query($sqlquery) or die (mysql_error());

 

It still doesn't insert the value

I've sorted it thanks,

 

I knew it would be something stupid I was doing;

 

I was checking the submit state and then running a loop to check values were'nt empty, except I had escaped two loops instead of one after  that, so I was escaping inserting the values.

 

Stupid boy  ::)

 

Thanks for your help

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.